Skip to content

Commit 3c0df50

Browse files
fix: ensure hash router back/forward navigation works (#13492)
- ensure modifying the hash manually results in a reload, which also aligns with the pathname behavior. Fixes #13322 - since we now handle hash manipulation reloads, we can remove code elsewhere that was supposed to handle this but introduced bugs with back/forward navigation. Fixes #13460
1 parent f9458e3 commit 3c0df50

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

.changeset/angry-peaches-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correct navigation history with hash router and ensure load functions are rerun on user changes to URL hash

packages/kit/src/runtime/client/client.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,12 @@ function _start_router() {
24352435
if (!hash_navigating) {
24362436
const url = new URL(location.href);
24372437
update_url(url);
2438+
2439+
// if the user edits the hash via the browser URL bar, trigger a full-page
2440+
// reload to align with pathname router behavior
2441+
if (app.hash) {
2442+
location.reload();
2443+
}
24382444
}
24392445
}
24402446
});
@@ -2453,13 +2459,6 @@ function _start_router() {
24532459
'',
24542460
location.href
24552461
);
2456-
} else if (app.hash) {
2457-
// If the user edits the hash via the browser URL bar, it
2458-
// (surprisingly!) mutates `current.url`, allowing us to
2459-
// detect it and trigger a navigation
2460-
if (current.url.hash === location.hash) {
2461-
void navigate({ type: 'goto', url: decode_hash(current.url) });
2462-
}
24632462
}
24642463
});
24652464

packages/kit/test/apps/hash-based-routing/src/routes/+layout.svelte

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<a href="/">/</a>
1111
<a href="/#/a">/#/a</a>
12+
<a href="/#/b">/#/b</a>
1213
<a href="/#/a#b">/#/a#b</a>
1314
<a href="/#/b/123">/#/b/123</a>
1415
<a href="/#/b/456">/#/b/456</a>

packages/kit/test/apps/hash-based-routing/test/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,20 @@ test.describe('hash based navigation', () => {
9999
const url = new URL(page.url());
100100
expect(url.hash).toBe('#/anchor#test');
101101
});
102+
103+
test('navigation history works', async ({ page }) => {
104+
await page.goto('/');
105+
106+
await page.locator('a[href="/#/a"]').click();
107+
await page.waitForURL('/#/a');
108+
109+
await page.locator('a[href="/#/b"]').click();
110+
await page.waitForURL('/#/b');
111+
112+
await page.goBack();
113+
expect(page.locator('p')).toHaveText('a');
114+
115+
await page.goForward();
116+
expect(page.locator('p')).toHaveText('b');
117+
});
102118
});

0 commit comments

Comments
 (0)