next.js
2ce362f3 - Replay same-document traversals that happen before hydration (#95682)

Commit
3 days ago
Replay same-document traversals that happen before hydration (#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.
Author
Parents
Loading