Skip to content

fix(app-router): reuse mounted layouts for runtime-dynamic routes and full prefetches - #5042

Closed
lyzno1 wants to merge 1 commit into
cloudflare:mainfrom
lyzno1:fix/mounted-layouts-dynamic-and-prefetch
Closed

lyzno1 wants to merge 1 commit into
cloudflare:mainfrom
lyzno1:fix/mounted-layouts-dynamic-and-prefetch

Conversation

@lyzno1

@lyzno1 lyzno1 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Follows up on #4276 (X-Vinext-Mounted-Layouts) to close two production gaps where shared layouts still re-rendered unnecessarily on App Router soft navigations and full prefetches:

1. Respect X-Vinext-Mounted-Layouts for manifest-excluded and runtime-dynamic static-pattern routes in production

  • Manifest-excluded paths (isNeverStoredPath): In packages/vinext/src/server/app-page-dispatch.ts, isCacheCandidate and isStoredRender already excluded paths with no Workers Cache manifest admission state, but isUncacheableRender omitted isNeverStoredPath. Including it lets manifest-excluded dynamic paths skip already-mounted layouts on the first navigation.
  • Runtime-dynamic static-pattern routes without a manifest (e.g. Node vinext start / Workers runtime admission): Static-pattern routes without export const dynamic = "force-dynamic" whose shared layout or page calls dynamic APIs (cookies(), headers(), fetch(..., { cache: "no-store" })) previously fell into a worst-of-both-worlds loop in production: treated as cache candidates before render (dropping X-Vinext-Mounted-Layouts), then skipped from ISR caching after observing dynamic usage, causing shared layouts to re-run on every soft navigation.
    • Track query-free pathnames that observe dynamic usage during a full cache-candidate render in a bounded per-route WeakMap<AppPageDispatchRoute, Set<string>> (cleared if a later full render stores into cache; never poisoned by query-param renders).
    • Subsequent cache-miss RSC navigations/prefetches carrying X-Vinext-Mounted-Layouts skip already-mounted layouts, bypass shared caching (isPartialPayload → private, no-cache, no-store), and preserve X-Vinext-Dynamic-Stale-Time even when the dynamic API lived only inside a skipped parent layout.
    • Genuinely static routes remain completely unchanged: their first cache-miss render stays whole, populates the shared ISR/CDN cache, and serves subsequent navigations as cache HITs.

2. Send X-Vinext-Mounted-Layouts on navigation-reusable prefetches (<Link prefetch={true}> / router.prefetch(..., { kind: "full" }))

  • Matches Next.js 16's FetchStrategy.Full, which diffs the target tree against the current router tree (diffRouteTreeAgainstCurrent in segment-cache/scheduler.ts) and sends Next-Router-State-Tree so walkTreeWithFlightRouterState skips shared mounted layouts during full prefetches.
  • Exposes getMountedLayoutsHeader() and holdsSkippedLayouts() through NavigationRuntimeFunctions (including initial hydration state before the first router commit, released once committed) and attaches X-Vinext-Mounted-Layouts to navigation-reusable prefetches in link.tsx and app-router-navigation.ts (never on loading-shell, dynamic-shell, route-tree, or intercepted prefetches).
  • Validates mounted-layout compatibility before reusing or consuming cached prefetch entries (isPrefetchCacheEntryCompatibleWithMountedLayouts), and falls back cleanly to a whole RSC refetch in app-browser-entry.ts if a consumed partial prefetch's skipped layouts are no longer held by the current router state.

Verification

  • vp run vinext#build — passed
  • vp check on all modified/added files — passed (0 warnings/errors)
  • vp test run across 11 unit test suites (app-page-dispatch, app-page-render, app-page-route-wiring, app-rsc-handler, app-rsc-cache-busting, link, link-navigation, prefetch-cache, app-browser-entry, cloudflare-cdn-worker, cloudflare-response-store-worker) — 2,115 passed
  • PLAYWRIGHT_PROJECT=app-router-isr-prod pnpm exec playwright test tests/e2e/app-router-prod/partial-layout-prefetch.spec.ts tests/e2e/app-router-prod/soft-nav-rsc-cache.spec.ts — 3 passed (both new production E2E tests and static soft-nav RSC cache regression test)

@clanky-the-clanker

Copy link
Copy Markdown

This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting clanky review (or clanker review or @clanky-the-clanker review), optionally followed by instructions (e.g. clanky review focus on the retry logic).

@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vinext/cloudflare

npm i https://pkg.pr.new/@vinext/cloudflare@5042

create-vinext-app

npm i https://pkg.pr.new/create-vinext-app@5042

@vinext/types

npm i https://pkg.pr.new/@vinext/types@5042

vinext

npm i https://pkg.pr.new/vinext@5042

@cloudflare/workers-response-store

npm i https://pkg.pr.new/@cloudflare/workers-response-store@5042

commit: 9c7c580

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared 9c7c580 against base ba17aac using alternating same-runner rounds. Next.js was unchanged and skipped.

0 improved · 0 regressed · 7 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 157.7 KB 158.0 KB ⚫ +0.2%
Client entry size (gzip) vinext 145.9 KB 146.2 KB ⚫ +0.2%
Dev server cold start vinext 2.90 s 2.94 s ⚫ +1.2%
Dev server cold start (large Pages Router app) vinext 23.09 s 22.88 s ⚫ -0.9%
Production build time vinext 3.45 s 3.46 s ⚫ +0.4%
RSC entry closure size (gzip) vinext 155.5 KB 155.7 KB ⚫ +0.2%
Server bundle size (gzip) vinext 268.5 KB 269.0 KB ⚫ +0.2%

View detailed results and traces

🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head

@james-elicx

Copy link
Copy Markdown
Member

Thanks for digging into this. There are three changes in here, so here's where each one stands:

1. Paths the manifest never stores (isNeverStoredPath): this was a clear bug. I filed it as #5046, split out of this PR. #5047 fixes it with the same one-line change to isUncacheableRender, plus a dispatch test covering paths the manifest omits, paths it lists, and the no-manifest case. We'll land the fix through that PR.

2. Mounted layouts on full prefetches: we're not planning to do this. It's the same case declined on #4814. Next.js does diff the prefetch tree against the current page. However, it stores the result per segment, so any page can reuse it later. vinext's prefetch cache holds one whole payload per URL. Sending the header would tie that entry to whichever page the user was on when the prefetch ran, and a click from anywhere else has to refetch. That's the slow path prefetching is meant to avoid. Doing this properly needs a per-segment prefetch cache first.

3. Remembering which paths turned out dynamic: this might be worth doing, but please open an issue for it rather than a PR. In the issue, describe the problem: on deployments without a manifest, a static-eligible route that uses a dynamic API renders its shared layouts again on every navigation. Include a repro and the approach you have in mind. Two things we'd want the issue to cover:

  • what happens to a path that is dynamic on one render and static on a later one;
  • what it gains on Workers, since each isolate starts with an empty map.

In general, for changes like 2 and 3, which change how responses are cached or reused, please start with an issue that describes the problem and the approach you have in mind. That way we can agree on the design before anyone writes code, and you don't end up building something we decline. It's also much easier for us to review one change per PR than several bundled together.

I'll close this one in favour of #5047. Thanks again, this was a good find.

@james-elicx james-elicx closed this Dec 2, 2026
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.

2 participants