Skip to content
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

only normalise internal URLs #5645

Merged
merged 4 commits into from Jul 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-carrots-notice.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Only normalise internal URLs
20 changes: 12 additions & 8 deletions packages/kit/src/runtime/client/client.js
Expand Up @@ -238,6 +238,10 @@ export function create_client({ target, session, base, trailing_slash }) {
return false; // unnecessary, but TypeScript prefers it this way
}

// if this is an internal navigation intent, use the normalized
// URL for the rest of the function
url = intent?.url || url;

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

Expand Down Expand Up @@ -891,9 +895,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 +937,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 +958,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
@@ -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>
20 changes: 19 additions & 1 deletion packages/kit/test/apps/basics/test/client.test.js
@@ -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,24 @@ 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');
});

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

expect(urls).toEqual(['/with-slash/']);
} finally {
await close();
}
});
});

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