diff --git a/.changeset/shiny-groups-relax.md b/.changeset/shiny-groups-relax.md new file mode 100644 index 000000000000..5dcc18cd3156 --- /dev/null +++ b/.changeset/shiny-groups-relax.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly set URL when navigating during an ongoing navigation diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 4a4ac7777ea2..6e027cb57677 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -537,8 +537,7 @@ function get_navigation_result_from_branch({ url, params, branch, status, error, } url.pathname = normalize_path(url.pathname, slash); - - // eslint-disable-next-line + // eslint-disable-next-line no-self-assign url.search = url.search; // turn `/?` into `/` /** @type {import('./types.js').NavigationFinished} */ @@ -1563,13 +1562,6 @@ async function navigate({ navigation_result.props.page.state = state; if (started) { - current = navigation_result.state; - - // reset url before updating page store - if (navigation_result.props.page) { - navigation_result.props.page.url = url; - } - const after_navigate = ( await Promise.all( Array.from(on_navigate_callbacks, (fn) => @@ -1592,6 +1584,13 @@ async function navigate({ }); } + current = navigation_result.state; + + // reset url before updating page store + if (navigation_result.props.page) { + navigation_result.props.page.url = url; + } + root.$set(navigation_result.props); update(navigation_result.props.page); has_navigated = true; diff --git a/packages/kit/test/apps/basics/src/routes/routing/long-navigation/+page.svelte b/packages/kit/test/apps/basics/src/routes/routing/long-navigation/+page.svelte new file mode 100644 index 000000000000..765cd414800f --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/routing/long-navigation/+page.svelte @@ -0,0 +1,7 @@ + + +Go to routing diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js index 8e494bb82d0c..eebaa4f95d16 100644 --- a/packages/kit/test/apps/basics/test/client.test.js +++ b/packages/kit/test/apps/basics/test/client.test.js @@ -1634,3 +1634,13 @@ test.describe('binding_property_non_reactive warn', () => { expect(is_warning_thrown).toBeFalsy(); }); }); + +test.describe('routing', () => { + test('navigating while navigation is in progress sets the correct URL', async ({ page }) => { + await page.goto('/routing/long-navigation'); + await page.click('a[href="/routing"]'); + await page.click('a[href="/routing"]'); + await expect(page.locator('h1')).toHaveText('Great success!'); + await expect(page).toHaveURL((url) => url.pathname === '/routing'); + }); +});