Skip to content

Commit

Permalink
Fix client component imports are not being correctly tracked (#30853)
Browse files Browse the repository at this point in the history
* ensure client components are correctly tracked in the client compilation

* fix test
  • Loading branch information
shuding committed Nov 3, 2021
1 parent 3aff5d4 commit 0fa233b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ export default async function getBaseWebpackConfig(
},
]
: []),
...(webServerRuntime && hasServerComponents
...(hasServerComponents && webServerRuntime
? [
{
...codeCondition,
Expand Down
22 changes: 12 additions & 10 deletions packages/next/build/webpack/loaders/next-flight-server-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ async function parseImportsInfo(
// When importing from a server component, ignore
const importSource = node.source.value

if (
!(
isClientComponent(importSource, pageExtensions) ||
isNextComponent(importSource) ||
isImageImport(importSource)
)
) {
continue
}

// For the client compilation, we have to always import the component to
// ensure that all dependencies are tracked.
if (!isClientCompilation) {
if (
!(
isClientComponent(importSource, pageExtensions) ||
isNextComponent(importSource) ||
isImageImport(importSource)
)
) {
continue
}
transformedSource += source.substr(
lastIndex,
node.source.start - lastIndex
Expand Down Expand Up @@ -104,5 +105,6 @@ export default async function transformSource(
const defaultExportNoop = isClientCompilation
? `\nexport default function Comp(){}\nComp.__next_rsc__=1`
: ''

return transformed + noop + defaultExportNoop
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foo from './foo.client'

export default function Bar() {
return (
<div>
bar.server.js: <Foo />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Bar from '../components/bar.server'

export default function Multi() {
return <Bar />
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ async function runBasicTests(context) {

expect(imageTag.attr('src')).toContain('data:image')
})

it('should support multi-level server component imports', async () => {
const html = await renderViaHTTP(context.appPort, '/multi')
expect(html).toContain('bar.server.js:')
expect(html).toContain('foo.client')
})
}

function runSuite(suiteName, env, { runTests, before, after }) {
Expand Down

0 comments on commit 0fa233b

Please sign in to comment.