diff --git a/.changeset/dirty-rats-divide.md b/.changeset/dirty-rats-divide.md new file mode 100644 index 000000000000..f02a208e96e0 --- /dev/null +++ b/.changeset/dirty-rats-divide.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: respect `replaceState` on external navigation diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 25cd2c89b528..b0013315b030 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -143,9 +143,15 @@ function clear_onward_history(current_history_index, current_navigation_index) { * Returns a `Promise` that never resolves (to prevent any * subsequent work, e.g. history manipulation, from happening) * @param {URL} url + * @param {Object} [opts] Options related to the navigation + * @param {boolean} [opts.replace_state] If `true`, will replace the current `history` entry rather than creating a new one with `pushState` */ -function native_navigation(url) { - location.href = url.href; +function native_navigation(url, opts = {}) { + if (opts.replace_state) { + location.replace(url.href); + } else { + location.href = url.href; + } return new Promise(() => {}); } @@ -1375,7 +1381,7 @@ async function navigate({ 404 ); } else { - return await native_navigation(url); + return await native_navigation(url, { replace_state }); } } else { navigation_result = await server_fallback( @@ -1423,7 +1429,7 @@ async function navigate({ if (updated) { // Before reloading, try to update the service worker if it exists await update_service_worker(); - await native_navigation(url); + await native_navigation(url, { replace_state }); } }