Skip to content
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/wild-coins-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly navigate when hash router is enabled and the browser encodes extra hashes
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export async function start(_app, _target, hydrate) {
if (hydrate) {
await _hydrate(target, hydrate);
} else {
goto(location.href, { replaceState: true });
goto(app.hash ? decodeURIComponent(location.href) : location.href, { replaceState: true });
}

_start_router();
Expand Down Expand Up @@ -2395,7 +2395,7 @@ function _start_router() {
// (surprisingly!) mutates `current.url`, allowing us to
// detect it and trigger a navigation
if (current.url.hash === location.hash) {
navigate({ type: 'goto', url: current.url });
navigate({ type: 'goto', url: new URL(decodeURIComponent(current.url.href)) });
}
}
});
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/test/apps/hash-based-routing/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ test.describe('hash based navigation', () => {
await expect(page.locator('p')).toHaveText('a');
});

test('navigation works with URL encoded characters', async ({ page }) => {
await page.goto('/#/%23test');
await expect(page.locator('p')).toHaveText('home');
// hashchange event
await page.goto('/#/a%23test');
await expect(page.locator('p')).toHaveText('a');
});

test('route id and params are correct', async ({ page }) => {
await page.goto('/#/b/123');
await expect(page.locator('p[data-data]')).toHaveText('{"slug":"123"} /b/[slug] /#/b/123');
Expand Down
Loading