Skip to content

Commit c43fd92

Browse files
authored
fix: only decode URL hash instead of entire URL (#13332)
followup to #13321 / #13316 This PR ensures only the hash of the URL is decoded so that any encoded hash character in the query parameters remains encoded.
1 parent 388d441 commit c43fd92

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ export async function start(_app, _target, hydrate) {
306306
if (hydrate) {
307307
await _hydrate(target, hydrate);
308308
} else {
309-
goto(app.hash ? decodeURIComponent(location.href) : location.href, { replaceState: true });
309+
goto(app.hash ? decode_hash(new URL(location.href)) : location.href, {
310+
replaceState: true
311+
});
310312
}
311313

312314
_start_router();
@@ -2432,7 +2434,7 @@ function _start_router() {
24322434
// (surprisingly!) mutates `current.url`, allowing us to
24332435
// detect it and trigger a navigation
24342436
if (current.url.hash === location.hash) {
2435-
navigate({ type: 'goto', url: new URL(decodeURIComponent(current.url.href)) });
2437+
navigate({ type: 'goto', url: decode_hash(current.url) });
24362438
}
24372439
}
24382440
});
@@ -2826,6 +2828,17 @@ function clone_page(page) {
28262828
};
28272829
}
28282830

2831+
/**
2832+
* @param {URL} url
2833+
* @returns {URL}
2834+
*/
2835+
function decode_hash(url) {
2836+
const new_url = new URL(url);
2837+
// Safari, for some reason, does change # to %23, when entered through the address bar
2838+
new_url.hash = decodeURIComponent(url.hash);
2839+
return new_url;
2840+
}
2841+
28292842
if (DEV) {
28302843
// Nasty hack to silence harmless warnings the user can do nothing about
28312844
const console_warn = console.warn;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ test.describe('hash based navigation', () => {
5656
});
5757

5858
test('navigation works with URL encoded characters', async ({ page }) => {
59-
await page.goto('/#/%23test');
59+
await page.goto('/?query=%23abc#/%23test');
6060
await expect(page.locator('p')).toHaveText('home');
6161
// hashchange event
62-
await page.goto('/#/a%23test');
62+
await page.goto('/?query=%23abc#/a%23test');
6363
await expect(page.locator('p')).toHaveText('a');
6464
});
6565

0 commit comments

Comments
 (0)