From 9bc6e627186b9256d132ab6d815193bb229fe23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kole=C5=BCy=C5=84ski?= <1st.cubix@gmail.com> Date: Mon, 9 Oct 2023 22:53:50 +0200 Subject: [PATCH] fix: `popstate` navigation wasn't cancelling ongoing `navigation()` calls when landing on the same page (#10727) --- .changeset/neat-chicken-turn.md | 5 +++++ packages/kit/src/runtime/client/client.js | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/neat-chicken-turn.md 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 174b2413bd12..f788b8b917e6 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 @@ -1695,7 +1696,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