Skip to content

fix(turbopack): include nested next/dynamic CSS in SSR HTML - #98949

Open
Sumit-Ks1 wants to merge 1 commit into
vercel:canaryfrom
Sumit-Ks1:fix/turbopack-nested-dynamic-css
Open

Sumit-Ks1 wants to merge 1 commit into
vercel:canaryfrom
Sumit-Ks1:fix/turbopack-nested-dynamic-css

Conversation

@Sumit-Ks1

Copy link
Copy Markdown

What?

Fix nested next/dynamic CSS being missing from server-rendered HTML, causing FOUC and layout shift.

Why?

When a next/dynamic component (level 1) itself declares another next/dynamic component (level 2), the level-2 CSS chunk is emitted but not linked in the SSR HTML. This causes a flash of unstyled content (FOUC) with a reported CLS of ~0.38.

The DFS traversal in get_next_dynamic_imports_for_endpoint (crates/next-api/src/module_graph.rs) returns GraphTraversalAction::Skip for DynamicEntry nodes. This prevents the traversal from descending into children of a dynamic entry, so any nested next/dynamic declarations are never discovered and never added to react-loadable-manifest.json.

How?

Changed GraphTraversalAction::SkipGraphTraversalAction::Continue for DynamicEntry nodes. This allows the traversal to descend into dynamic entry children and discover nested dynamic imports at any depth.

This is safe because:

  • This traversal only discovers dynamic entries — chunk availability is determined separately in collect_next_dynamic_chunks
  • Each nested entry gets correctly associated with its parent client reference via the state_map
  • The FxIndexMap in DynamicImportedChunks prevents duplicate entries

Regression test added: A two-level nested next/dynamic component chain with CSS Modules, asserting both stylesheet <link> tags appear in the SSR HTML and computed styles are correct in the browser.

Checklist

  • Related issues linked using fixes #number
  • Tests added
  • Errors have a helpful link attached

Fixes #98804

When a next/dynamic component itself declares another next/dynamic, the
inner component's CSS was missing from the server-rendered HTML because
the DFS in get_next_dynamic_imports_for_endpoint returned Skip on
DynamicEntry nodes, preventing discovery of nested entries.

Change Skip to Continue so the traversal descends into dynamic entry
children and discovers nested dynamic imports at any depth. This is safe
because chunk availability is determined separately in
collect_next_dynamic_chunks, not by this discovery traversal.

Added a regression test with a two-level nested dynamic import chain
that asserts both CSS stylesheet links appear in the SSR HTML.

Fixes vercel#98804
Copilot AI lite review requested due to automatic review settings September 19, 2026 23:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Regression assertions should distinguish both CSS chunks and verify SSR links before hydration.

Review effort: Lite
Findings: None

What changed in this PR

Fixes missing SSR CSS links for nested next/dynamic components, preventing FOUC and layout shift.

Changes:

  • Traverse nested dynamic entries.
  • Add nested component and CSS fixtures.
  • Add end-to-end regression coverage.
File Summary
test/​e2e/​app-dir/​next-dynamic-css/​next-dynamic-css.test.ts Adds nested dynamic CSS regression assertions.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​widget.tsx Adds the nested widget fixture.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​widget.module.css Adds widget styles.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​variant.tsx Adds the nested variant fixture.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​variant.module.css Adds variant styles.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​resolver.tsx Defines the dynamic import resolver.
test/​e2e/​app-dir/​next-dynamic-css/​app/​nested/​page.tsx Adds the nested test route.
crates/​next-api/​src/​module_graph.rs Continues traversal through dynamic entries.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turbopack: nested next/dynamic gets no SSR stylesheet link, causing FOUC and layout shift (App Router)

2 participants