Unverified Commit 845007af authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[lts-active] Fix Turbopack lazy dynamic-import chunk-list collisions (#100211)

Backports [Fix Turbopack lazy dynamic-import chunk-list collisions
(#99940)](https://github.com/vercel/next.js/pull/99940) to
`releases/lts/active`. The original PR reproduced the bug on
`next@16.4.0`. The cherry-pick applied cleanly.

## Verification

- `pnpm test-dev-turbo` on the new `lazy-dynamic-chunk-list-collision`
test and the existing `lazy-dynamic-imports` suite, using a native
binding built from this branch: 13/13 tests passed.

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarJimmy Miller <james.miller@vercel.com>
parent 6a79d8aa
'use client'
import dynamic from 'next/dynamic'
const Target = dynamic(() => import('../target'))
export default function Page() {
return <Target />
}
import type { ReactNode } from 'react'
export default function Root({
children,
parallel,
}: {
children: ReactNode
parallel: ReactNode
}) {
return (
<html>
<body>
{children}
{parallel}
</body>
</html>
)
}
import { useEffect, useState } from 'react'
export default function Leaf() {
const [hydrated, setHydrated] = useState(false)
useEffect(() => setHydrated(true), [])
return <p data-hydrated={hydrated}>Lazy leaf</p>
}
'use client'
import dynamic from 'next/dynamic'
const Target = dynamic(() => import('./target'))
export default function Page() {
return <Target />
}
'use client'
import { lazy, Suspense } from 'react'
const Leaf = lazy(() => import('./leaf'))
export default function Target() {
return (
<Suspense fallback="Loading">
<Leaf />
</Suspense>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry, waitForNoRedbox } from 'next-test-utils'
// @force-gate dev && turbopack
describe('lazy-dynamic-chunk-list-collision', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('renders a shared dynamic target with nested lazy imports from parallel branches', async () => {
expect((await next.fetch('/')).status).toBe(200)
const browser = await next.browser('/')
await retry(async () => {
expect(
await browser.elementsByCss('[data-hydrated="true"]')
).toHaveLength(2)
})
await waitForNoRedbox(browser)
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
turbopackLazyDynamicImports: true,
},
}
export default nextConfig
...@@ -114,8 +114,21 @@ impl ManifestAsyncModule { ...@@ -114,8 +114,21 @@ impl ManifestAsyncModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn hmr_chunk_list(self: Vc<Self>) -> Result<Vc<OutputAssets>> { async fn hmr_chunk_list(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?; let this = self.await?;
let ident = self.ident();
// The same import can have different chunks depending on which modules its parent
// already provides. Keep those chunk lists distinct without hashing their contents,
// so their paths remain stable when chunks are added or removed during HMR.
let ident = if let Some(availability_ident) = this.availability_info.ident().await? {
ident
.owned()
.await?
.with_modifier(availability_ident)
.into_vc()
} else {
ident
};
Ok(this.chunking_context.hmr_chunk_list( Ok(this.chunking_context.hmr_chunk_list(
self.ident(), ident,
*self.chunk_group().await?.assets, *self.chunk_group().await?.assets,
HmrChunkListSource::Dynamic, HmrChunkListSource::Dynamic,
)) ))
......
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