diff --git a/.changeset/neat-chicken-turn.md b/.changeset/neat-chicken-turn.md new file mode 100644 index 000000000000..d5cfcf449bb0 --- /dev/null +++ b/.changeset/neat-chicken-turn.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: cancel ongoing navigation when the browser back button is hit to prevent an incorrect page from being rendered diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index e9b660f2cd36..8d476da26df0 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1662,6 +1662,7 @@ export function create_client(app, target) { }); addEventListener('popstate', async (event) => { + token = {}; if (event.state?.[INDEX_KEY]) { // if a popstate-driven navigation is cancelled, we need to counteract it // with history.go, which means we end up back here, hence this check @@ -1693,7 +1694,8 @@ export function create_client(app, target) { history.go(-delta); }, type: 'popstate', - delta + delta, + nav_token: token }); } else { // since popstate event is also emitted when an anchor referencing the same