Conversation
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
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Fix nested
next/dynamicCSS being missing from server-rendered HTML, causing FOUC and layout shift.Why?
When a
next/dynamiccomponent (level 1) itself declares anothernext/dynamiccomponent (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) returnsGraphTraversalAction::SkipforDynamicEntrynodes. This prevents the traversal from descending into children of a dynamic entry, so any nestednext/dynamicdeclarations are never discovered and never added toreact-loadable-manifest.json.How?
Changed
GraphTraversalAction::Skip→GraphTraversalAction::ContinueforDynamicEntrynodes. This allows the traversal to descend into dynamic entry children and discover nested dynamic imports at any depth.This is safe because:
collect_next_dynamic_chunksstate_mapFxIndexMapinDynamicImportedChunksprevents duplicate entriesRegression test added: A two-level nested
next/dynamiccomponent chain with CSS Modules, asserting both stylesheet<link>tags appear in the SSR HTML and computed styles are correct in the browser.Checklist
fixes #numberFixes #98804