Skip to content

fix: record a history traversal before resolving its route - #16959

Merged
Rich-Harris merged 1 commit into
version-3from
popstate-shallow-race
Aug 27, 2026
Merged

fix: record a history traversal before resolving its route#16959
Rich-Harris merged 1 commit into
version-3from
popstate-shallow-race

Conversation

@Nic-Polumeyv

Copy link
Copy Markdown
Contributor

Replaces #16954.

The popstate handler in client.js awaits route resolution (get_navigation_intent, and again inside navigate before accept) before recording that the browser has moved. Under server-side route resolution that is a network round-trip, during which current_history_index still points at the entry we left. A popstate arriving in that window is swallowed by the cancellation guard, and a goto pushes an entry with a duplicate historyIndex, so later scroll and snapshot restores read the wrong entry. This is the flake in https://github.com/sveltejs/kit/actions/runs/33085846048.

Record the traversal synchronously: capture the source entry's scroll and snapshots and move the indices before anything async, restore them in block() before history.go(-delta), and capture the navigation token up front so a superseded popstate stops instead of taking over the newer navigation's token. navigate skips its own capture for popped navigations.

Repro: a 120 ms delay after the get_navigation_intent await in the handler fails Preserves scroll and focus across popstate... 4/4 in test:server-side-route-resolution:dev before this change and passes 4/4 after it.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 3fc84c0:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/3fc84c094041f98110f396ce144e4c204c55dc4d

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16959

@changeset-bot

changeset-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3fc84c0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice sleuthing

@Rich-Harris
Rich-Harris merged commit b12bb78 into version-3 Aug 27, 2026
40 checks passed
@Rich-Harris
Rich-Harris deleted the popstate-shallow-race branch August 27, 2026 20:09
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