Skip to content

Commit

Permalink
Fix tracking of ContextModule (#52795)
Browse files Browse the repository at this point in the history
When doing dynamic imports like `import(variable)`, Webpack _tries_ to statically analyze it and creates a regex like context module for it (which includes all possible modules). This `ContextModule` doesn't have a resource path so we need to use the identifier to track it.

Tested with @alexkirsz's repro here #50243 (comment) and confirmed that it fixes the problem.

Closes #50243.
  • Loading branch information
shuding authored Jul 17, 2023
1 parent 80572b3 commit 7e9627a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,14 @@ export class FlightClientEntryPlugin {
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs.
const modRequest: string | undefined =
let modRequest: string | undefined =
mod.resourceResolveData?.path + mod.resourceResolveData?.query

// Context modules don't have a resource path, we use the identifier instead.
if (mod.constructor.name === 'ContextModule') {
modRequest = (mod as any)._identifier
}

if (!modRequest || visited.has(modRequest)) return
visited.add(modRequest)

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/app-dir/rsc-basic/app/dynamic/_dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use client'

import { useState } from 'react'

export default function Dynamic() {
const [data] = useState('dynamic data!')
return <h1>{data}</h1>
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/rsc-basic/app/dynamic/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default async function Page() {
const dynamic = '_dynamic'
const { default: Component } = await import(`./${dynamic}.js`)
return <Component />
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/rsc-basic/rsc-basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ createNextDescribe(
expect(content).toMatchInlineSnapshot('"next_streaming_data"')
})

it('should track client components in dynamic imports', async () => {
const html = await next.render('/dynamic')
expect(html).toContain('dynamic data!')
})

it('should support next/link in server components', async () => {
const $ = await next.render$('/next-api/link')
const linkText = $('body a[href="/root"]').text()
Expand Down

0 comments on commit 7e9627a

Please sign in to comment.