next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Turbopack dev: next/dynamic preloads a chunk that is never emitted (404) when the component has an import()

Open AdzerKI opened this issue 2 months ago • 7 comments

Link to the code that reproduces this issue

https://github.com/AdzerKI/next-dynamic-async-import-repro

To Reproduce

  1. npm install
  2. npm run dev
  3. In a second terminal: npm run check — it loads / (App Router) and /legacy (Pages Router) and requests every chunk the HTML references

The component loaded with next/dynamic (app/target.js) has a plain import('./lazy') in a click handler. That's all it takes.

Current vs. Expected behavior

Current: both pages reference a chunk the dev server never emits:

/ 404 /_next/static/chunks/app_lazy_1i0vu8m4k93tk.js
/legacy 404 /_next/static/chunks/app_lazy_16oebmc7g56ke.js

App Router renders it as <link rel="preload" as="script">, Pages Router as <script src>, so every page load logs a 404. The chunk that is actually emitted for the loader of ./lazy has a different hash, and clicking the button works.

Expected: every chunk in the HTML exists. next build and next start are fine.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #31~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Aug 10 09:38:02 UTC 2
  Available memory (MB): 96549
  Available CPU cores: 16
Binaries:
  Node: 24.21.0
  npm: 12.0.2
  Yarn: 1.22.22
  pnpm: 10.33.0
Relevant Packages:
  next: 16.4.0-canary.42 // Latest available version is detected (16.4.0-canary.42).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack, Lazy Loading

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Also happens on 16.3.6. It's the same bug as #87680: that issue was closed by #88775, which was reverted in #89073, and the re-apply #89121 is still a draft.

collect_next_dynamic_chunks rebuilds the async loader of the next/dynamic import with the parent chunk group's availability info as is, while make_chunk_group gives the loaders it creates availability_info.in_async_module(). Without nested async availability (the dev default) that flag decides which availability the loaders inside the dynamic chunk group get, so the loader of ./lazy ends up with a different ident, and a different chunk name, in react-loadable-manifest.json than on disk. I'll open a PR with a fix.

AdzerKI avatar Sep 24 '26 10:09 AdzerKI

This also reaches next build on 16.4.0 (it didn't on 16.3.6 for the same app).

Pages Router, Turbopack, a page-level dynamic(() => import('components/ListLinks')) where the loaded component has no nested import() itself. Two pages that render the same dynamic component get different chunk names for it in their react-loadable-manifest.json:

  • server/pages/<page>/[slug]/react-loadable-manifest.json → static/chunks/0jqoppuuk1kl6.js (emitted, and the one the client loader requests)
  • server/pages/<page>/react-loadable-manifest.json → static/chunks/3690cbsnqceeb.js (never emitted, referenced by nothing but the manifests)

Same module id (loadableGenerated: { modules: [456658] }) in both. The CSS file of the entry exists, only the JS name differs, so the index page's HTML gets a <script src> that 404s while the component itself still loads through the right chunk.

PhilMeyr avatar Oct 12 '26 13:10 PhilMeyr

@PhilMeyr I couldn't reproduce that on 16.4.0 with next build: two Pages Router pages (pages/shared/index.js and pages/shared/[slug].js) with the same page-level dynamic(() => import(...)) of a component with a CSS module, also with the [slug] page importing a module of that component and with an import() in that module. Both react-loadable-manifest.json files reference emitted chunks only. Could you share a repro, or the imports of the two pages, _app and the dynamic component, plus your next.config?

AdzerKI avatar Oct 12 '26 15:10 AdzerKI

@AdzerKI thanks for trying. My description was wrong on two points: the dynamic() call isn't in the page itself but in a shared registry module that _app imports too, and the page that breaks statically imports a module that the dynamic component also imports. Here is a minimal repro (Pages Router, no config, no CSS):

// lib/constants.js
export const KEY = 'links'

// lib/registry.js
import dynamic from 'next/dynamic'
export const components = { list: dynamic(() => import('../components/ListLinks')) }
export function Render({ name }) { const C = components[name]; return <C /> }

// components/ListLinks.js
import { KEY } from '../lib/constants'
export default function ListLinks() { return <div>{KEY}</div> }

// pages/_app.js
import { Render } from '../lib/registry'
export default function App({ Component, pageProps }) {
  return <><Component {...pageProps} />{pageProps.global && <Render name={pageProps.global} />}</>
}

// pages/shared/index.js
import { Render } from '../../lib/registry'
import { KEY } from '../../lib/constants'
export default function Index() { return <><p>{KEY}</p><Render name="list" /></> }
export const getServerSideProps = () => ({ props: {} })

// pages/shared/[slug].js
import { Render } from '../../lib/registry'
export default function Slug() { return <Render name="list" /> }
export const getServerSideProps = () => ({ props: {} })

After next build, check that every file in the loadable manifests exists:

cd .next && for m in $(find server/pages -name react-loadable-manifest.json); do python3 -c "import json,os,sys; [print('MISSING', sys.argv[1], f) for e in json.load(open(sys.argv[1])).values() for f in e['files'] if not os.path.exists(f)]" "$m"; done
  • 16.3.6: nothing missing
  • 16.4.0 and 16.5.0-canary.8: MISSING server/pages/shared/react-loadable-manifest.json static/chunks/3xrtdv4jbr3jv.js

Both ingredients are needed: if _app doesn't import the registry, or if pages/shared/index.js doesn't import lib/constants, every chunk exists.

In our real app, the 16.3.6 build emitted a separate chunk for that page's variant of the dynamic chunk group, without the module the page already loads. 16.4.0 still writes that variant's name into the page's manifest but no longer emits it, so the page gets a <script src> that 404s. It looks like the same availability mismatch you described, reached through _app instead of a nested import(). Happy to open a separate issue if you'd rather keep this one about dev.

PhilMeyr avatar Oct 12 '26 20:10 PhilMeyr

@PhilMeyr thanks, that reproduces, and it is a second path to the same kind of mismatch. /_app is chunked before the page and seeds its availability, so lib/registry lands in the /_app chunk group and the loader of ListLinks is created there, without lib/constants in its availability. The manifest of the page resolved the same import with the availability of the page, which already has lib/constants, so it named a variant that is never emitted. I pushed a fix for it to #99150 with your setup as a test, so no need for a separate issue.

AdzerKI avatar Oct 12 '26 21:10 AdzerKI

Still reproduces on 16.4.5 (App Router, next dev, Turbopack). next build + next start of the same app is fine: its manifest lists only the dynamic component's own chunk, and that chunk exists.

Two data points that may be useful for the fix or its tests:

1. The import() doesn't have to be in the next/dynamic component itself. One anywhere in its static subtree is enough:

// app/p/page.tsx
import Client from "../../components/Client";
export default function Page() { return <Client />; }

// components/Client.tsx
"use client";
import dynamic from "next/dynamic";
const Shell = dynamic(() => import("./Shell"));
export default function Client() { return <Shell />; }

// components/Shell.tsx — no import() here
"use client";
import { run } from "./lazy";
export default function Shell() { return <button onClick={() => run()}>go</button>; }

// components/lazy.ts
export async function run() { const d = await import("./deeper"); d.go(); }

// components/deeper.ts
export function go() { console.log("deeper ran"); }

(plus a minimal root layout). GET /p renders <link rel="preload" href="/_next/static/chunks/…components_deeper_ts_15evmb7xzsblk._.js" as="script" fetchPriority="low"/>, and that URL is a 404. On disk, .next/dev/static/chunks has the loader for ./deeper under a different hash. The route's react-loadable-manifest.json lists the unemitted name under the Shell.tsx [app-client] (ecmascript, next/dynamic entry) key. Clicking the button works.

2. React.lazy + <Suspense> in place of next/dynamic gives no preload and no 404, as you'd expect, since it never goes through the loadable manifest. It's a workaround for anyone whose tests assert a clean console under next dev. The trade-off is losing the SSR preload hint.

shtsai avatar Nov 18 '26 05:11 shtsai

@shtsai thanks for the details. 16.4.5 does not have the fix yet, #99150 is still open. It gives the manifest the same availability info that the next/dynamic chunk group creates its async loaders with, and an import() anywhere in the static subtree of the dynamic component gets its loader in that chunk group, so your components/lazy.ts case is fixed the same way as an import() in the component itself.

AdzerKI avatar Nov 19 '26 13:11 AdzerKI

This issue was closed by a maintainer. If you think it was closed by mistake, you can request that it be reopened within the next 14 days.

Reply with Reopen: <reason> and explain why the issue is still relevant. If you opened this issue or commented before it was closed, the issue will be reopened automatically.

After 14 days, please open a new issue with up-to-date details and a reproduction, when applicable.

github-actions[bot] avatar Nov 20 '26 21:11 github-actions[bot]