Skip to content

fix: correctly set the URL when navigating during a navigation #14004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-groups-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly set URL when navigating during an ongoing navigation
17 changes: 8 additions & 9 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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) =>
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { onNavigate } from '$app/navigation';

onNavigate(() => new Promise((resolve) => setTimeout(resolve, 3000)));
</script>

<a href="/routing">Go to routing</a>
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Loading