Skip to content

feat: track previousNextUrl for intercepted App Router entries#755

Draft
NathanDrake2406 wants to merge 25 commits intocloudflare:mainfrom
NathanDrake2406:feat/layout-persistence-pr-5-previous-next-url
Draft

feat: track previousNextUrl for intercepted App Router entries#755
NathanDrake2406 wants to merge 25 commits intocloudflare:mainfrom
NathanDrake2406:feat/layout-persistence-pr-5-previous-next-url

Conversation

@NathanDrake2406
Copy link
Copy Markdown
Contributor

@NathanDrake2406 NathanDrake2406 commented Apr 2, 2026

Summary

Part of #726 (PR 5). This builds on PR 4's interception-aware payload IDs and cache keys by tracking previousNextUrl in the App Router browser state. Intercepted navigations now remember the URL they came from, so refresh/back-forward behavior can distinguish soft interception from direct loads.

The practical effect is that /feed -> /photos/42 modal navigations now behave correctly across the three important cases: browser reload breaks out to the full page, back/forward restores the intercepted modal view from history state, and router.refresh() keeps the intercepted modal instead of silently switching to the direct page tree.

What changed

  • Browser router stateapp-browser-state now carries previousNextUrl alongside elements, interceptionContext, routeId, and rootLayoutTreePath, with helpers for persisting and reading it from history state
  • History state persistence — App Router entries now store __vinext_previousNextUrl instead of the old interception-context field, preserving the URL active before a soft navigation while keeping the existing scroll restoration state intact
  • Navigation request context — soft navigate records the current URL as previousNextUrl, traverse reconstructs interception context from event.state, and refresh reconstructs it from committed router state so refresh preserves interception without changing hard-load behavior
  • Hard-load normalization — boot clears any carried-over previousNextUrl from the active history entry so full document reloads still break out of interception even though browsers preserve history.state across reloads
  • Redirect recursion — redirected RSC navigations carry the original previousNextUrl forward so replacement navigations do not lose the source-route provenance
  • Fixtures and tests — adds unit coverage for previousNextUrl state/history helpers plus E2E coverage for reload, back/forward restoration, and router.refresh() on intercepted routes. The modal fixture now exposes a small router.refresh() button to exercise the public API
  • Comment cleanup — refreshes a few stale app-browser-entry comments so they match the current dispatch/control-flow names after the refactor

What this does NOT do

Server action interception-request parity is still intentionally deferred. This PR preserves the browser-side previousNextUrl state during server-action merges, but it does not add interception headers to the action POST itself.

Test plan

  • vp test run tests/app-browser-entry.test.ts
  • vp test run tests/app-router.test.ts
  • vp run test:e2e tests/e2e/app-router/advanced.spec.ts --grep 'hard reload after intercepted navigation renders the full page|back then forward restores intercepted modal view|router.refresh preserves intercepted modal view'
  • git commit pre-commit hook (vp check --fix)

- Fix stale closure on readBrowserRouterState by using a useRef updated
  synchronously during render instead of a closure captured in
  useLayoutEffect. External callers (navigate, server actions, HMR) now
  always read the current router state.

- Restore GlobalErrorBoundary wrapping that was dropped when switching
  from buildPageElement to buildAppPageElements. Apps with
  app/global-error.tsx now get their global error boundary back.

- Add exhaustive default case to routerReducer so new action types
  produce a compile error and a runtime throw instead of silent undefined.

- Remove dead code: createRouteNodeSnapshot, AppRouteNodeSnapshot,
  AppRouteNodeValue were defined but never imported.

- Remove deprecated buildAppPageRouteElement and its test — no
  production callers remain after the flat payload cutover.

- Short-circuit normalizeAppElements when no slot keys need rewriting
  to avoid unnecessary allocation on every payload.

- Align test data in error boundary RSC payload test (matchedParams
  slug: "post" -> "missing" to match requestUrl /posts/missing).
createFromReadableStream() returns a React thenable whose .then()
returns undefined (not a Promise). Chaining .then(normalizeAppElements)
broke SSR by assigning undefined to flightRoot.

Fix: call use() on the raw thenable, then normalize synchronously
after resolution. Also widen renderAppPageLifecycle element type to
accept flat map payloads.
The SSR entry always expects a flat Record<string, ReactNode> with
__route and __rootLayout metadata from the RSC stream. Three paths
were still producing bare ReactNode payloads:

1. renderAppPageBoundaryElementResponse only created the flat map for
   isRscRequest=true, but HTML requests also flow through RSC→SSR
2. buildPageElements "no default export" early return
3. Server action "Page not found" fallback

All three now produce the flat keyed element map, fixing 17 test
failures across 404/not-found, forbidden/unauthorized, error boundary,
production build, rewrite, and encoded-slash paths.
- Update renderElementToStream mock to extract the route element from
  the flat map before rendering to HTML (mirrors real SSR entry flow)
- Update entry template snapshots for the buildPageElements changes
createFromReadableStream() returns a React Flight thenable whose
.then() returns undefined instead of a new Promise. The browser
entry's normalizeAppElementsPromise chained .then() on this raw
thenable, producing undefined — which crashed use() during hydration
with "An unsupported type was passed to use(): undefined".

Wrapping in Promise.resolve() first converts the Flight thenable
into a real Promise, making .then() chains work correctly.

The same fix was already applied to the SSR entry in 5395efc but
was missed in the browser entry.
React 19.2.4's use(Promise) during hydration triggers "async Client
Component" because native Promises lack React's internal .status
property (set only by Flight thenables). When use() encounters a
Promise without .status, it suspends — which React interprets as the
component being async, causing a fatal error.

Fix: store resolved AppElements directly in ElementsContext and
router state instead of Promise<AppElements>. The navigation async
flow (createPendingNavigationCommit) awaits the Promise before
dispatching, so React state never holds a Promise.

- ElementsContext: Promise<AppElements> → AppElements
- AppRouterState.elements: Promise<AppElements> → AppElements
- mergeElementsPromise → mergeElements (sync object spread)
- Slot: useContext only, no use(Promise)
- SSR entry: pass resolved elements to context
- dispatchBrowserTree: simplified, no async error handler

Also fix flaky instrumentation E2E test that read the last error
entry instead of finding by path.
- Remove Promise wrappers from ElementsContext test values
- mergeElementsPromise → mergeElements (sync)
- Replace Suspense streaming test with direct render test
- Remove unused createDeferred helper and Suspense import
- Update browser state test assertions (no longer async)
P1a: mergeElements preserves previous slot content when the new payload
marks a parallel slot as unmatched. On soft navigation, unmatched slots
keep their previous subtree instead of triggering notFound().

P1b: renderNavigationPayload now receives navId and checks for
superseded navigations after its await. Stale payloads are discarded
instead of being dispatched into the React tree.

P2: The catch block in renderNavigationPayload only calls
commitClientNavigationState() when activateNavigationSnapshot() was
actually reached, preventing counter underflow.

P3: The no-default-export fallback in buildPageElements now derives
the root layout tree path from route.layoutTreePositions and
route.routeSegments instead of hardcoding "/".
Prove the flat keyed map architecture works end-to-end:
- Layout state persists across sibling navigation (counter survives)
- Template remounts on segment boundary change, persists within segment
- Error boundary clears on navigate-away-and-back
- Back/forward preserves layout state through history
- Parallel slots persist on soft nav, show default.tsx on hard nav

Zero production code changes — test fixtures and Playwright specs only.
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 2, 2026

Open in StackBlitz

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

commit: 7f22e74

@NathanDrake2406 NathanDrake2406 changed the title docs: refresh stale app browser entry comments feat: track previousNextUrl for intercepted App Router entries Apr 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.

1 participant