Skip to content

Commit

Permalink
only normalise internal URLs - fixes #5087
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 21, 2022
1 parent 28419a3 commit b4cec97
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-carrots-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Only normalise internal URLs
19 changes: 11 additions & 8 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export function create_client({ target, session, base, trailing_slash }) {
return false; // unnecessary, but TypeScript prefers it this way
}

// use the normalized URL from here on out
url = /** @type {import('./types').NavigationIntent} */ (intent).url;

// abort if user navigated during update
if (token !== current_token) return false;

Expand Down Expand Up @@ -891,9 +894,12 @@ export function create_client({ target, session, base, trailing_slash }) {
const params = route.exec(path);

if (params) {
const id = normalize_path(url.pathname, trailing_slash) + url.search;
const normalized = new URL(
url.origin + normalize_path(url.pathname, trailing_slash) + url.search + url.hash
);
const id = normalized.pathname + normalized.search;
/** @type {import('./types').NavigationIntent} */
const intent = { id, route, params: decode_params(params), url };
const intent = { id, route, params: decode_params(params), url: normalized };
return intent;
}
}
Expand Down Expand Up @@ -930,22 +936,19 @@ export function create_client({ target, session, base, trailing_slash }) {
return;
}

const pathname = normalize_path(url.pathname, trailing_slash);
const normalized = new URL(url.origin + pathname + url.search + url.hash);

update_scroll_positions(current_history_index);

accepted();

if (started) {
stores.navigating.set({
from: current.url,
to: normalized
to: url
});
}

await update(
normalized,
url,
redirect_chain,
false,
{
Expand All @@ -954,7 +957,7 @@ export function create_client({ target, session, base, trailing_slash }) {
details
},
() => {
const navigation = { from, to: normalized };
const navigation = { from, to: url };
callbacks.after_navigate.forEach((fn) => fn(navigation));

stores.navigating.set(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<script>
import { page } from '$app/stores';
</script>

<a href="/routing/">/routing/</a>
<a href="/routing/?">/routing/?</a>
<a href="/routing/?foo=bar">/routing/?foo=bar</a>
<a href="/routing/?foo=bar">/routing/?foo=bar</a>
<a href="http://localhost:{$page.url.searchParams.get('port')}/with-slash/">external</a>
18 changes: 17 additions & 1 deletion packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@playwright/test';
import { test } from '../../../utils.js';
import { start_server, test } from '../../../utils.js';

/** @typedef {import('@playwright/test').Response} Response */

Expand Down Expand Up @@ -544,6 +544,22 @@ test.describe('Routing', () => {
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
});

test('does not normalize external path', async ({ page }) => {
const urls = [];

const { port, close } = await start_server((req, res) => {
urls.push(req.url);
res.end('ok');
});

await page.goto(`/routing/slashes?port=${port}`);
await page.click(`a[href="http://localhost:${port}/with-slash/"]`);

expect(urls).toEqual(['/with-slash/']);

await close();
});
});

test.describe('Shadow DOM', () => {
Expand Down

0 comments on commit b4cec97

Please sign in to comment.