Repository navigation
Replay same-document traversals that happen before hydration - #95682
Conversation
Stats from current PRWarning No stats were collected for Turbopack because its stats job did not complete (it failed, was cancelled, or timed out). The results below only cover the bundlers that finished. 🟢 2 improvements
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (21 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsfailed to diffapp-page-exp..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page.runtime.dev.jsfailed to diffapp-page.runtime.prod.jsfailed to diffapp-route-ex..ntime.dev.jsDiff too large to display app-route-ex..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route.runtime.dev.jsDiff too large to display app-route.ru..time.prod.jsDiff too large to display server.runtime.prod.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display 📎 Tarball URLCommit: a917a47 |
Tests PassedCommit: a917a47 |
05fe589 to
755457e
Compare
| return | ||
| } | ||
| didCheckForTraversalBeforeHydration = true | ||
| if (typeof window.navigation === 'undefined') { |
There was a problem hiding this comment.
Tried without depending on it first but couldn't figure out how to detect this reliably without regressing on other things
0f0ff9a to
e4bb9d1
Compare
238fb95 to
096f4cc
Compare
…tent Session history entries created by pushState remain same-document siblings of a reloaded document. If the user presses Back while the reloaded page is still loading (easy to hit with a hard reload), the browser performs an instant same-document traversal: the URL changes and popstate fires, but no router is attached yet, so the content does not change. When hydration then completes, the router assumes the current URL matches its payload and stamps the payload tree onto the traversed entry via replaceState. From then on the URL bar and the rendered content permanently disagree, and subsequent traversals update the URL without ever changing the content. Covers a path-only traversal and one that differs only in search params, plus pre-hydration history changes that are NOT missed traversals and must keep behaving as before: a third-party pushState, in-page anchor jumps (on a fresh load and between a reload and hydration), a pushState followed by back, and a traversal onto a third-party entry. The tests make the race deterministic by stalling static scripts so the reloaded document commits but cannot hydrate until released.
096f4cc to
6d54d09
Compare
Session history entries created by pushState remain same-document siblings of a reloaded document, so a Back/Forward press while the reloaded page is still loading is an instant same-document traversal whose popstate fires before the router's code has run. The router then hydrated assuming the current entry matched its payload, stamped the payload tree onto the traversed entry, and the URL bar and content permanently disagreed. Since the popstate itself is unobservable, detect the traversal at boot via the Navigation API: the current entry no longer matches the entry the document was activated on (entry keys are stable across replaceState but not across traversals). The check also requires the current entry to be app-router-owned (history.state.__NA): a foreign entry means an early third-party pushState or an anchor jump from a stateless entry, where there is nothing to replay and replaying would spuriously reload. When a missed traversal is detected, the router's first history write is skipped (one-shot, decided together with the detection) so the traversed entry's state stays intact, and once the popstate listener is attached the missed popstate is replayed against the live history state through the same handler. In browsers without the Navigation API the traversal remains unhandled, as before.
6d54d09 to
a917a47
Compare
…#95853) Reverts #95682 This breaks CC (#95848) due to PPR codepath doing something different. Although Claude says da4d63d can work around it, I think it's safest to revert this given that I don't know what the right full fix is. I also think we need more test coverage because the navigation test suite is off in CC mode. I'll send a follow-up with all the failing tests we know of (from #95682 and new) so we can fix this for good after a closer look.
…-hydration tests Every scenario now runs against two fixture shapes: pages with no Suspense boundary above them, and pages below a Suspense-wrapped layout. These exercise different recovery codepaths: without a boundary, a suspended traversal holds the transition until its data is ready; with one, it can commit the boundary's fallback and then depends on React retrying when the data resolves. Under cacheComponents the boundary variant regressed into a permanently blank page (#95848) while the boundary-less variant recovered. On top of the revert, the 'reconciles ...' tests fail in both variants (URL/content desync); the 'other history changes' tests describe pre-#95682 behavior and should pass.
…tree (#37135) Closes vercel/next.js#95848. Fixes a hang: if an update changes what's inside a server-rendered Suspense or Activity boundary before that boundary has hydrated, and the affected content is hidden, React stops committing. The new content renders once its data arrives, but the render is discarded every time, nothing is scheduled, and nothing ever pings — the update never lands and the app appears frozen. "Hidden" means either of two things, and there's a test for each: - the update itself hides a dehydrated `<Activity>` (while mounting new sibling content that suspends), or - the dehydrated boundary is inside the primary tree of a parent boundary that just suspended and is showing its fallback. This is how we found it in practice. With vercel/next.js#95682, pressing Back before hydration finishes made the router replay the missed navigation from its first effect. This worked fine outside of Cache Components, but in Cache Components mode (which turns on Activity), the old page's Activity (still dehydrated) gets hidden, the new page's content suspends inside the layout's Suspense boundary, and after the data arrives the page stays blank forever. As a result, vercel/next.js#95682 got reverted. If we fix this, we can unrevert it. ## Why it happens When an update changes a dehydrated boundary, we schedule a render at a higher priority to hydrate it before the update applies. If we already tried that, we give up and client render, but mark the render as suspended so it doesn't commit while the hydration attempt might still finish first. Both steps assume the attempt can actually run. Inside a hidden tree it can't, because updates in hidden trees are deferred until the tree is revealed. The scheduled attempt never runs but still consumes the retry lane, which sends every later render into the give-up path — and the give-up path keeps discarding finished renders, waiting for a hydration attempt that isn't in flight. Once the last piece of data resolves there's nothing left to ping us awake. The root ends up with `pendingLanes === suspendedLanes`, `pingedLanes` empty, and no callback scheduled. The update doesn't need to be sync or discrete: a plain setState from an effect is enough. Wrapping the same update in `startTransition` avoids it, which is probably why this went unnoticed. ## The fix If the boundary is inside a hidden tree (`isCurrentTreeHidden()`), skip the hydration attempt and client render right away. There's nothing visible to protect: replacing hidden server HTML doesn't show, and the replacement children render when the tree is revealed. One behavior note: this discards the hidden server HTML instead of preserving it for later hydration on reveal, same as the existing give-up path. Keeping it dehydrated and hydrating at reveal would be a nicer follow-up, but needs commit-phase support that doesn't exist today. ## How did you test this change? The first commit adds failing tests for both boundary types; the fix makes them pass. `startTransition` variants of the same scenarios are included as passing controls. Ran the Activity, partial/selective hydration, Fizz, Suspense, and Offscreen suites in both release channels.
…tree (#37135) Closes vercel/next.js#95848. Fixes a hang: if an update changes what's inside a server-rendered Suspense or Activity boundary before that boundary has hydrated, and the affected content is hidden, React stops committing. The new content renders once its data arrives, but the render is discarded every time, nothing is scheduled, and nothing ever pings — the update never lands and the app appears frozen. "Hidden" means either of two things, and there's a test for each: - the update itself hides a dehydrated `<Activity>` (while mounting new sibling content that suspends), or - the dehydrated boundary is inside the primary tree of a parent boundary that just suspended and is showing its fallback. This is how we found it in practice. With vercel/next.js#95682, pressing Back before hydration finishes made the router replay the missed navigation from its first effect. This worked fine outside of Cache Components, but in Cache Components mode (which turns on Activity), the old page's Activity (still dehydrated) gets hidden, the new page's content suspends inside the layout's Suspense boundary, and after the data arrives the page stays blank forever. As a result, vercel/next.js#95682 got reverted. If we fix this, we can unrevert it. ## Why it happens When an update changes a dehydrated boundary, we schedule a render at a higher priority to hydrate it before the update applies. If we already tried that, we give up and client render, but mark the render as suspended so it doesn't commit while the hydration attempt might still finish first. Both steps assume the attempt can actually run. Inside a hidden tree it can't, because updates in hidden trees are deferred until the tree is revealed. The scheduled attempt never runs but still consumes the retry lane, which sends every later render into the give-up path — and the give-up path keeps discarding finished renders, waiting for a hydration attempt that isn't in flight. Once the last piece of data resolves there's nothing left to ping us awake. The root ends up with `pendingLanes === suspendedLanes`, `pingedLanes` empty, and no callback scheduled. The update doesn't need to be sync or discrete: a plain setState from an effect is enough. Wrapping the same update in `startTransition` avoids it, which is probably why this went unnoticed. ## The fix If the boundary is inside a hidden tree (`isCurrentTreeHidden()`), skip the hydration attempt and client render right away. There's nothing visible to protect: replacing hidden server HTML doesn't show, and the replacement children render when the tree is revealed. One behavior note: this discards the hidden server HTML instead of preserving it for later hydration on reveal, same as the existing give-up path. Keeping it dehydrated and hydrating at reveal would be a nicer follow-up, but needs commit-phase support that doesn't exist today. ## How did you test this change? The first commit adds failing tests for both boundary types; the fix makes them pass. `startTransition` variants of the same scenarios are included as passing controls. Ran the Activity, partial/selective hydration, Fizz, Suspense, and Offscreen suites in both release channels. DiffTrain build for [9a81195](9a81195)
…tree (#37135) Closes vercel/next.js#95848. Fixes a hang: if an update changes what's inside a server-rendered Suspense or Activity boundary before that boundary has hydrated, and the affected content is hidden, React stops committing. The new content renders once its data arrives, but the render is discarded every time, nothing is scheduled, and nothing ever pings — the update never lands and the app appears frozen. "Hidden" means either of two things, and there's a test for each: - the update itself hides a dehydrated `<Activity>` (while mounting new sibling content that suspends), or - the dehydrated boundary is inside the primary tree of a parent boundary that just suspended and is showing its fallback. This is how we found it in practice. With vercel/next.js#95682, pressing Back before hydration finishes made the router replay the missed navigation from its first effect. This worked fine outside of Cache Components, but in Cache Components mode (which turns on Activity), the old page's Activity (still dehydrated) gets hidden, the new page's content suspends inside the layout's Suspense boundary, and after the data arrives the page stays blank forever. As a result, vercel/next.js#95682 got reverted. If we fix this, we can unrevert it. ## Why it happens When an update changes a dehydrated boundary, we schedule a render at a higher priority to hydrate it before the update applies. If we already tried that, we give up and client render, but mark the render as suspended so it doesn't commit while the hydration attempt might still finish first. Both steps assume the attempt can actually run. Inside a hidden tree it can't, because updates in hidden trees are deferred until the tree is revealed. The scheduled attempt never runs but still consumes the retry lane, which sends every later render into the give-up path — and the give-up path keeps discarding finished renders, waiting for a hydration attempt that isn't in flight. Once the last piece of data resolves there's nothing left to ping us awake. The root ends up with `pendingLanes === suspendedLanes`, `pingedLanes` empty, and no callback scheduled. The update doesn't need to be sync or discrete: a plain setState from an effect is enough. Wrapping the same update in `startTransition` avoids it, which is probably why this went unnoticed. ## The fix If the boundary is inside a hidden tree (`isCurrentTreeHidden()`), skip the hydration attempt and client render right away. There's nothing visible to protect: replacing hidden server HTML doesn't show, and the replacement children render when the tree is revealed. One behavior note: this discards the hidden server HTML instead of preserving it for later hydration on reveal, same as the existing give-up path. Keeping it dehydrated and hydrating at reveal would be a nicer follow-up, but needs commit-phase support that doesn't exist today. ## How did you test this change? The first commit adds failing tests for both boundary types; the fix makes them pass. `startTransition` variants of the same scenarios are included as passing controls. Ran the Activity, partial/selective hydration, Fizz, Suspense, and Offscreen suites in both release channels. DiffTrain build for [9a81195](9a81195)
…tree (react#37135) Closes vercel/next.js#95848. Fixes a hang: if an update changes what's inside a server-rendered Suspense or Activity boundary before that boundary has hydrated, and the affected content is hidden, React stops committing. The new content renders once its data arrives, but the render is discarded every time, nothing is scheduled, and nothing ever pings — the update never lands and the app appears frozen. "Hidden" means either of two things, and there's a test for each: - the update itself hides a dehydrated `<Activity>` (while mounting new sibling content that suspends), or - the dehydrated boundary is inside the primary tree of a parent boundary that just suspended and is showing its fallback. This is how we found it in practice. With vercel/next.js#95682, pressing Back before hydration finishes made the router replay the missed navigation from its first effect. This worked fine outside of Cache Components, but in Cache Components mode (which turns on Activity), the old page's Activity (still dehydrated) gets hidden, the new page's content suspends inside the layout's Suspense boundary, and after the data arrives the page stays blank forever. As a result, vercel/next.js#95682 got reverted. If we fix this, we can unrevert it. ## Why it happens When an update changes a dehydrated boundary, we schedule a render at a higher priority to hydrate it before the update applies. If we already tried that, we give up and client render, but mark the render as suspended so it doesn't commit while the hydration attempt might still finish first. Both steps assume the attempt can actually run. Inside a hidden tree it can't, because updates in hidden trees are deferred until the tree is revealed. The scheduled attempt never runs but still consumes the retry lane, which sends every later render into the give-up path — and the give-up path keeps discarding finished renders, waiting for a hydration attempt that isn't in flight. Once the last piece of data resolves there's nothing left to ping us awake. The root ends up with `pendingLanes === suspendedLanes`, `pingedLanes` empty, and no callback scheduled. The update doesn't need to be sync or discrete: a plain setState from an effect is enough. Wrapping the same update in `startTransition` avoids it, which is probably why this went unnoticed. ## The fix If the boundary is inside a hidden tree (`isCurrentTreeHidden()`), skip the hydration attempt and client render right away. There's nothing visible to protect: replacing hidden server HTML doesn't show, and the replacement children render when the tree is revealed. One behavior note: this discards the hidden server HTML instead of preserving it for later hydration on reveal, same as the existing give-up path. Keeping it dehydrated and hydrating at reveal would be a nicer follow-up, but needs commit-phase support that doesn't exist today. ## How did you test this change? The first commit adds failing tests for both boundary types; the fix makes them pass. `startTransition` variants of the same scenarios are included as passing controls. Ran the Activity, partial/selective hydration, Fizz, Suspense, and Offscreen suites in both release channels. DiffTrain build for [9a81195](react@9a81195)
…tree (react#37135) Closes vercel/next.js#95848. Fixes a hang: if an update changes what's inside a server-rendered Suspense or Activity boundary before that boundary has hydrated, and the affected content is hidden, React stops committing. The new content renders once its data arrives, but the render is discarded every time, nothing is scheduled, and nothing ever pings — the update never lands and the app appears frozen. "Hidden" means either of two things, and there's a test for each: - the update itself hides a dehydrated `<Activity>` (while mounting new sibling content that suspends), or - the dehydrated boundary is inside the primary tree of a parent boundary that just suspended and is showing its fallback. This is how we found it in practice. With vercel/next.js#95682, pressing Back before hydration finishes made the router replay the missed navigation from its first effect. This worked fine outside of Cache Components, but in Cache Components mode (which turns on Activity), the old page's Activity (still dehydrated) gets hidden, the new page's content suspends inside the layout's Suspense boundary, and after the data arrives the page stays blank forever. As a result, vercel/next.js#95682 got reverted. If we fix this, we can unrevert it. ## Why it happens When an update changes a dehydrated boundary, we schedule a render at a higher priority to hydrate it before the update applies. If we already tried that, we give up and client render, but mark the render as suspended so it doesn't commit while the hydration attempt might still finish first. Both steps assume the attempt can actually run. Inside a hidden tree it can't, because updates in hidden trees are deferred until the tree is revealed. The scheduled attempt never runs but still consumes the retry lane, which sends every later render into the give-up path — and the give-up path keeps discarding finished renders, waiting for a hydration attempt that isn't in flight. Once the last piece of data resolves there's nothing left to ping us awake. The root ends up with `pendingLanes === suspendedLanes`, `pingedLanes` empty, and no callback scheduled. The update doesn't need to be sync or discrete: a plain setState from an effect is enough. Wrapping the same update in `startTransition` avoids it, which is probably why this went unnoticed. ## The fix If the boundary is inside a hidden tree (`isCurrentTreeHidden()`), skip the hydration attempt and client render right away. There's nothing visible to protect: replacing hidden server HTML doesn't show, and the replacement children render when the tree is revealed. One behavior note: this discards the hidden server HTML instead of preserving it for later hydration on reveal, same as the existing give-up path. Keeping it dehydrated and hydrating at reveal would be a nicer follow-up, but needs commit-phase support that doesn't exist today. ## How did you test this change? The first commit adds failing tests for both boundary types; the fix makes them pass. `startTransition` variants of the same scenarios are included as passing controls. Ran the Activity, partial/selective hydration, Fizz, Suspense, and Offscreen suites in both release channels. DiffTrain build for [9a81195](react@9a81195)
Relands #95682 (reverted in #95853 due to #95848). This contains: - A bunch of tests (some failing and some passing before this PR; all passing after this PR). - The fix from #95682. In short, we use Navigation API to detect during hydration that we're on a different history entry than we started, and in that case, replay the missed traversal with similar logic to what we do for `onPopState`. The issue with the original PR was that under Activity, React would sometimes hang applying the state update. This was fixed in react/react#37135 and synced, so we can do this fix again now. ## Test Plan Old and new tests.
…95682) Fixes a race you can observe if you: 1. Open page A 2. Navigate to page B 3. Cmd+Shift+R while on page B... 4. ...and then navigate Back super fast In that situation, the router gets confused and shows page B's content with page A's URL. From then on, going Back and Forward changes the URL (and tab title) but keeps showing B's content. See the new tests. ## Why This Happens Navigating from A to B client-side creates B's history entry via `pushState`. Reloading B replaces that document with B's fresh server-rendered HTML. Pressing Back at that point fires `popstate` to A, but nothing listens to it yet. Then JS loads, and we happily hydrate B's content onto B's HTML despite technically being on A route. ## The Fix Use Navigation API to detect that we’re on a different entry than we started. Once the router mounts, replay the missed traversal the same way the `onPopState` handler would have handled it. (I originally tried to avoid using Navigation API but wasn’t sure how to detect this reliably.) ## Test Plan New tests are red before the fix.
…vercel#95853) Reverts vercel#95682 This breaks CC (vercel#95848) due to PPR codepath doing something different. Although Claude says vercel@da4d63d can work around it, I think it's safest to revert this given that I don't know what the right full fix is. I also think we need more test coverage because the navigation test suite is off in CC mode. I'll send a follow-up with all the failing tests we know of (from vercel#95682 and new) so we can fix this for good after a closer look.
Relands vercel#95682 (reverted in vercel#95853 due to vercel#95848). This contains: - A bunch of tests (some failing and some passing before this PR; all passing after this PR). - The fix from vercel#95682. In short, we use Navigation API to detect during hydration that we're on a different history entry than we started, and in that case, replay the missed traversal with similar logic to what we do for `onPopState`. The issue with the original PR was that under Activity, React would sometimes hang applying the state update. This was fixed in react/react#37135 and synced, so we can do this fix again now. ## Test Plan Old and new tests.
Fixes a race you can observe if you:
In that situation, the router gets confused and shows page B's content with page A's URL. From then on, going Back and Forward changes the URL (and tab title) but keeps showing B's content. See the new tests.
Why This Happens
Navigating from A to B client-side creates B's history entry via
pushState. Reloading B replaces that document with B's fresh server-rendered HTML. Pressing Back at that point firespopstateto A, but nothing listens to it yet. Then JS loads, and we happily hydrate B's content onto B's HTML despite technically being on A route.The Fix
Use Navigation API to detect that we’re on a different entry than we started. Once the router mounts, replay the missed traversal the same way the
onPopStatehandler would have handled it.(I originally tried to avoid using Navigation API but wasn’t sure how to detect this reliably.)
Test Plan
New tests are red before the fix.