Unverified Commit 7e9627a3 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix tracking of ContextModule (#52795)

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 https://github.com/vercel/next.js/issues/50243#issuecomment-1628675346 and confirmed that it fixes the problem.

Closes #50243.
parent 80572b37
...@@ -586,9 +586,14 @@ export class FlightClientEntryPlugin { ...@@ -586,9 +586,14 @@ export class FlightClientEntryPlugin {
// We have to always use the resolved request here to make sure the // 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 // server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs. // the server compiler and client compiler have different resolve configs.
const modRequest: string | undefined = let modRequest: string | undefined =
mod.resourceResolveData?.path + mod.resourceResolveData?.query 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 if (!modRequest || visited.has(modRequest)) return
visited.add(modRequest) visited.add(modRequest)
......
'use client'
import { useState } from 'react'
export default function Dynamic() {
const [data] = useState('dynamic data!')
return <h1>{data}</h1>
}
export default async function Page() {
const dynamic = '_dynamic'
const { default: Component } = await import(`./${dynamic}.js`)
return <Component />
}
...@@ -193,6 +193,11 @@ createNextDescribe( ...@@ -193,6 +193,11 @@ createNextDescribe(
expect(content).toMatchInlineSnapshot('"next_streaming_data"') 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 () => { it('should support next/link in server components', async () => {
const $ = await next.render$('/next-api/link') const $ = await next.render$('/next-api/link')
const linkText = $('body a[href="/root"]').text() const linkText = $('body a[href="/root"]').text()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment