fix(app-router): isolate page CSS chunks in production#1738
fix(app-router): isolate page CSS chunks in production#1738NathanDrake2406 wants to merge 5 commits into
Conversation
commit: |
16feaf7 to
14a7a5e
Compare
Production App Router builds statically imported every page module into the generated RSC manifest. That let Vite/Rolldown concatenate page-level global CSS from sibling routes into shared CSS assets, so a sibling hash page with scroll-padding-top changed native hash scroll offsets on unrelated pages. The manifest now emits cached dynamic loaders for page modules and loads the matched page module at the route dispatch boundary. Page generateStaticParams sources use the same loader with an explicit missing-export sentinel so prerender still distinguishes absent exports from malformed results.
14a7a5e to
a9852d2
Compare
|
/bigbonk review |
There was a problem hiding this comment.
Review: isolate page CSS chunks in production
Nice, well-structured change. The core idea — moving page modules from static manifest imports to cached dynamic import() loaders so a route only pulls in its own CSS chunk — is sound and matches Next.js's per-route CSS scoping. I traced the request, action, intercept, and prerender paths and the lazy-load gating is consistent.
Correctness — verified
- Request path:
app-rsc-handler.ts:664-666loads the matched route vialoadRouteModulesbefore dispatch, sohandlePageRequest(route.page?.default, segment config,generateStaticParams) reads a populatedroute.page. ✓ - Action path:
handleServerActionRequestis nowasyncand awaits__loadRouteMatch(matchRoute(...))before reading__actionMatch.route.page; the innermatchRouteoption is also wrapped, so action rerenders/redirect targets load first. ✓ - Intercept path:
findInterceptreturns a per-request shallow copy ({ ...entry, matchedParams }inapp-rsc-route-matching.ts:129), so theintercept.page = await intercept.__pageLoader()mutation inapp-page-request.ts:336does not mutate the shared lookup entry. ✓ - Prerender parity: the switch from
mod?.generateStaticParams(filtered out when absent) to an always-present__createLazyGenerateStaticParamsSource(...)that returns theMISSING_GENERATE_STATIC_PARAMSsentinel is handled equivalently downstream. The build prerender path already treats a function-returning-nullthe same as a missing function (build/prerender.ts:1096vs:1139), so theoutput: 'export'error andno-static-paramsskip behaviors are preserved. ✓
Notes (non-blocking)
-
No direct unit test for the new helper module. Per AGENTS.md ("If you move behavior out of a template string, add a focused unit test for the new helper module"),
app-route-module-loader.tshas meaningful logic (WeakMap dedup, shared-promise reuse, page mutation) but is only covered indirectly viaapp-router.test.ts. A small focused test would lock in the dedup/caching contract. -
Rejected loads are cached permanently. See inline comment — both
createRouteModuleLoaderandloadRouteModulescache the in-flight promise, so a transientimport()failure poisons the route for the lifetime of the worker instance with no retry. Likely acceptable for built chunks, but worth a comment documenting the intent. -
Minor inefficiency: intercept route modules are keyed in the
loadRouteStatesWeakMap by the per-request{ ...entry }copy, so the route-level load cache is not reused across requests for intercepts (the underlyingcreateRouteModuleLoaderpromise cache still dedups the actualimport(), so this is harmless).
Nothing blocking from my read.
| await promise; | ||
| state.loaded = true; | ||
| return route; | ||
| } |
There was a problem hiding this comment.
Rejected loads are cached forever. If loadLazyModules(route) rejects, state.promise holds the rejected promise and state.loaded stays false, so every later loadRouteModules(route) call falls into the state.promise branch (line 47-50) and re-awaits the same rejection — the route is permanently broken within this worker instance with no retry. createRouteModuleLoader (line 34-40) has the same behavior. This is probably fine for built page chunks (a failure means a genuinely missing/broken chunk), but it's a deliberate-looking tradeoff that isn't documented. Consider a short comment noting the no-retry caching is intentional, or clearing state.promise on rejection if transient retries are desired.
| return MISSING_GENERATE_STATIC_PARAMS; | ||
| } | ||
| return generateStaticParams(input); | ||
| }; |
There was a problem hiding this comment.
Behavioral note (not a bug): because lazy sources are now always functions, a dynamic route with a page that has no generateStaticParams now eagerly import()s the page module during prerender enumeration just to discover the export is absent (returning the sentinel). Previously the absent export was filtered out at resolver-build time without loading the module. This is fine since prerender runs at build time, but it does mean more module loads during generateStaticParams discovery. Worth confirming this doesn't measurably slow large App Router prerender builds.
|
Review posted successfully to PR #1738. SummaryI reviewed the PR, which isolates page CSS chunks in production by moving App Router page modules from static manifest imports to cached dynamic Verdict: well-structured, nothing blocking. I traced all four affected paths and confirmed the lazy-load gating is consistent:
Non-blocking notes I flagged:
|
Address the non-blocking review note from PR cloudflare#1738: per AGENTS.md, behavior moved out of a template string should get a focused helper test. Cover createRouteModuleLoader (promise dedup, rejection propagation), loadRouteModules (page assignment, concurrent dedup, no-op cases, missing `page` key) and loadRouteMatch (null short-circuit, match load). Also cover createLazyGenerateStaticParamsSource (delegation, missing sentinel for null/empty/non-function exports). Document the rejected-load cache intent and the per-request intercept copy in the WeakMap so future readers don't try to "fix" them as bugs.
|
Thanks for the review! Addressed all three non-blocking notes in 1e33ba5:
CI is green on a9852d2 — let me know if you want any further tweaks. |
|
Agreed — a positive case proving the modal CSS is emitted when the intercept is active is a good follow-up, but not blocking. The runtime already loads the intercept page before render (the |
|
/bigbonk review |
There was a problem hiding this comment.
Re-review: isolate page CSS chunks in production
I independently traced all four request paths and re-ran the targeted suites. The change is sound and the three follow-ups from the prior review are addressed. Nothing blocking.
Verified
- Request path —
app-rsc-handler.ts:663-666awaitsloadRouteModules(matchedRoute)beforedispatchMatchedPagereadsroute.page?.default(app-rsc-entry.ts:595). ✓ - Action path —
handleServerActionRscRequestisasync;matchRouteis wired to__loadRouteMatch(matchRoute(...))andgetSourceRouteto__loadRouteModules(routes[i]), soroute.pageis populated before reads atapp-server-action-execution.ts:645(canRenderActionRedirectTarget) andapp-rsc-entry.ts:797. ✓ - Intercept path —
findInterceptreturns a fresh shallow copy per call (app-rsc-route-matching.ts:129), so theintercept.page = await intercept.__pageLoader()mutation (app-page-request.ts:335-336) never touches the sharedinterceptLookupentry. The shared__pageLoaderclosure still dedups the underlyingimport(). ✓ - Prerender parity — the always-present lazy source returns
MISSING_GENERATE_STATIC_PARAMS, and the newresolvedThisSource/resolvedAnySourcetracking makes a layout-only pattern (page absent) behave identically to the oldlength === 1branch. The sentinel is converted tonullat the resolver boundary (lines 37, 78), which the build prerender path already treats the same as a missing function (build/prerender.ts:1096,:1117,:1135-1149) — sooutput: 'export'errors andno-static-paramsskips are preserved. ✓
Checks I ran locally
vp test run tests/app-route-module-loader.test.ts tests/app-prerender-endpoints.test.ts tests/app-page-request.test.ts tests/entry-templates.test.ts→ 75 passedvp test run tests/app-router.test.ts -t "global CSS isolated"→ both isolation tests pass (sibling-route and intercepted-modal)vp checkon the changed server modules + new test → clean (format, lint, types)
Notes (non-blocking, already acknowledged)
- Rejected
import()results are cached for the worker lifetime in bothcreateRouteModuleLoaderandloadRouteModules(a rejection poisons the route with no retry). The added comment documents this as intentional for stable built chunks; fine. - The intercept-modal test only asserts the negative direction (modal CSS absent on direct
/feedvisit). A positive guard (modal CSS present when the intercept is active) would lock in the lazy-load-on-intercept path as a regression guard — good follow-up, not blocking.
LGTM.
|
Review posted successfully to PR #1738. SummaryI independently re-reviewed the PR, which isolates App Router page CSS chunks in production by moving page modules from static manifest imports to cached dynamic Verdict: well-structured, nothing blocking. I traced all four affected paths against the actual code (not just the diff) and confirmed lazy-load gating is consistent:
Verification I ran locally: 75 unit tests pass, both CSS-isolation integration tests pass, and The two non-blocking notes (permanent rejection caching, missing positive intercept-CSS assertion) were already acknowledged by the author and prior reviewer. |
…-isolation # Conflicts: # tests/app-router.test.ts
|
/bigbonk review for issues |
|
UnknownError: "Overloaded" |
|
@james-elicx Bonk workflow failed. Check the logs for details. View workflow run · To retry, trigger Bonk again. |
|
/bigbonk review for issues |
|
UnknownError: "Overloaded" |
|
/bigbonk review for issues |
There was a problem hiding this comment.
Review for issues: isolate page CSS chunks in production
I traced all four lazy-load paths against the actual code (not just the diff) and ran the targeted suites locally. No blocking issues. The change is correct, the dedup/cache contract is sound, and prerender parity holds.
Verified correct
- Request path —
app-rsc-handler.ts:670-673awaitsloadRouteModules(matchedRoute)beforedispatchMatchedPagereadsroute.page?.default(app-rsc-entry.ts:597). ✓ - Action path —
handleServerActionRscRequestisasync;matchRouteis wrapped with__loadRouteMatchandgetSourceRoutewith__loadRouteModules, soroute.pageis populated beforecanRenderActionRedirectTargetreads it (app-server-action-execution.ts:645) and before the segment-config read atapp-rsc-entry.ts:799. ✓ - Intercept path —
findInterceptreturns a per-request shallow copy (app-rsc-route-matching.ts:129) carrying the shared__pageLoader, so theintercept.page = await intercept.__pageLoader()mutation (app-page-request.ts:335-336) never corrupts the shared lookup entry, and the shared loader closure still dedups theimport(). ✓ - Prerender parity — the always-present lazy source returning
MISSING_GENERATE_STATIC_PARAMS, combined withresolvedThisSource/resolvedAnySource, makes a layout-GSP + missing-page-GSP route behave identically to the old filtered single-source path. The resolver'snullreturn is treated equivalently to a missing function by the build path (build/prerender.ts:1096,:1117,:1139), sooutput: 'export'errors andno-static-paramsskips are preserved. ✓
Checks I ran
vp test run tests/app-route-module-loader.test.ts tests/app-prerender-endpoints.test.ts→ 28 passedvp test run tests/entry-templates.test.ts tests/app-page-request.test.ts→ 48 passedvp test run tests/app-router-production-server.test.ts→ 42 passed (both CSS-isolation tests green)
One new observation (non-blocking, scope note)
Parallel-slot page modules (slot.pagePath, app-rsc-manifest.ts:163) are still statically imported (imports.getImportVar), unlike the matched route page and the intercepting-route page on line 169 (which now use getLazyImportVar). A non-intercepting parallel-route page that imports route-global CSS would therefore still contribute its CSS to the shared manifest chunk — the same class of leak this PR fixes for the main page. This is consistent with the PR's stated scope, and the current fixtures don't exercise it (the @modal slot only has an intercepting page), so it's not a regression. Worth tracking as a follow-up so the remaining gap is explicit.
The other two known notes (permanent rejection caching, missing positive intercept-CSS assertion) were already raised and acknowledged. LGTM.
| if (slot.errorPath) imports.getImportVar(slot.errorPath); | ||
| for (const ir of slot.interceptingRoutes) { | ||
| imports.getImportVar(ir.pagePath); | ||
| imports.getLazyImportVar(ir.pagePath); |
There was a problem hiding this comment.
This intercepting-route page is now lazy (getLazyImportVar), but the parallel-slot page on line 163 (if (slot.pagePath) imports.getImportVar(slot.pagePath);) is still a static import. A non-intercepting parallel-route page that imports route-global CSS would still leak its CSS into the shared manifest chunk — the same class of leak this PR fixes for the main page. Not a regression and out of this PR's scope (current fixtures only have an intercepting modal page), but worth a follow-up so the remaining gap is tracked.
|
Review posted successfully to PR #1738. SummaryI reviewed the PR for issues, tracing all four lazy-load paths against the actual source and running the targeted test suites locally. Verdict: no blocking issues. The change is correct. What I verified
Tests run locally (all green)
One new observation (non-blocking)Parallel-slot page modules ( The two previously-known notes (permanent rejection caching, missing positive intercept-CSS assertion) were already acknowledged by the author and prior reviews. |
Overview
import()loaders instead of static importsgenerateStaticParams, and intercepted/source-route renderspackages/vinext/src/entries/app-rsc-manifest.ts,packages/vinext/src/entries/app-rsc-entry.ts,packages/vinext/src/server/app-rsc-handler.ts,tests/app-router.test.tsscroll-padding-topinto unrelated hash pagesWhy
Native hash scrolling uses the browser's layout state. Next.js calls
scrollIntoView()for hash fragments, so a 20px-short scroll offset means unrelated CSS is present, not that hash math is wrong.generateStaticParamsmust remain distinguishable from malformed return valuesWhat changed
/hashwith sibling/hash-with-scroll-offsetscroll-padding-top:20pxfrom the sibling pagegenerateStaticParamsValidation
vp test run tests/app-router.test.ts -t "keeps production route-level global CSS isolated"vp test run tests/app-router.test.ts tests/app-prerender-endpoints.test.ts tests/app-page-request.test.ts tests/app-page-dispatch.test.ts tests/app-server-action-execution.test.ts tests/app-rsc-handler.test.ts tests/entry-templates.test.tsvp checkvp check, and knipvp env exec --node 24 ./scripts/run-nextjs-deploy-suite.sh /Users/nathan/Projects/vinext/.refs/nextjs-v16.2.6 --retries 0 -c 1 --debug test/e2e/app-dir/navigation/navigation.test.ts/hash-link-back-to-same-pagescroll-offset failure now passes./hash-with-scroll-offsetpasses.useParamslog capture, hashwith-query-paramRSC request detection, redirect-with-loading duplicate render, nested navigation, Promise proxy navigation, and scroll restoration.Risk / compatibility
References
scrollIntoView()