Skip to content

Commit

Permalink
fix(5457): normalize path on prefetch (#5458)
Browse files Browse the repository at this point in the history
* fix(5457): normalize path on prefetch

* Create new-geckos-vanish.md

* add test

* oops

* hmm

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
GauBen and Rich-Harris authored Jul 11, 2022
1 parent 59d03e2 commit d22fbcb
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-geckos-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Normalize paths on prefetch (fixes #5457)
9 changes: 2 additions & 7 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,14 +898,9 @@ 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;
/** @type {import('./types').NavigationIntent} */
const intent = {
id: url.pathname + url.search,
route,
params,
url
};

const intent = { id, route, params, url };
return intent;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script context="module">
let load_calls = 0;
export const load = () => {
load_calls += 1;
return { props: { calls: load_calls } };
};
</script>

<script>
import { onMount } from 'svelte';
import { page } from '$app/stores';
export let calls;
const modal_contents = {
'please-dont-show-me': {
title: 'Oopsie'
},
'please-dont-show-me-jr': {
title: 'Oopsie Jr.'
}
};
let modal = undefined;
const show_modal = () => {
const hash = $page.url.hash.substring(1);
modal = modal_contents[hash];
};
onMount(show_modal);
</script>

<svelte:window on:popstate={show_modal} />

<h1>{modal?.title ?? ''}</h1>
<p>Loaded {calls} times.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function get() {
return { body: JSON.stringify('prefetched') };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const message = await fetch('/routing/prefetched.json').then(r => r.json());
return { props: { message } };
}
</script>

<script>
/** @type {string} */
export let message;
</script>

<h1>{message}</h1>
23 changes: 23 additions & 0 deletions packages/kit/test/apps/options/source/pages/__layout.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import {
goto,
invalidate,
prefetch,
prefetchRoutes,
beforeNavigate,
afterNavigate
} from '$app/navigation';
if (typeof window !== 'undefined') {
Object.assign(window, {
goto,
invalidate,
prefetch,
prefetchRoutes,
beforeNavigate,
afterNavigate
});
}
</script>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a sveltekit:prefetch href="/path-base/prefetching/prefetched">click me</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function get() {
return {
body: {
message: 'prefetched'
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @type {string} */
export let message;
</script>

<h1>{message}</h1>
32 changes: 32 additions & 0 deletions packages/kit/test/apps/options/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ test.describe('trailingSlash', () => {
expect(r.url()).toBe(`${baseURL}/path-base/page-endpoint/__data.json`);
expect(await r.json()).toEqual({ data: 'hi' });
});

test('accounts for trailingSlash when prefetching', async ({
app,
baseURL,
page,
javaScriptEnabled
}) => {
if (!javaScriptEnabled) return;

await page.goto('/path-base/prefetching');

/** @type {string[]} */
let requests = [];
page.on('request', (r) => requests.push(r.url()));

// also wait for network processing to complete, see
// https://playwright.dev/docs/network#network-events
await app.prefetch('/path-base/prefetching/prefetched');

// svelte request made is environment dependent
if (process.env.DEV) {
expect(requests.filter((req) => req.endsWith('.svelte')).length).toBe(1);
} else {
expect(requests.filter((req) => req.endsWith('.js')).length).toBeGreaterThan(0);
}

expect(requests.includes(`${baseURL}/path-base/prefetching/prefetched/__data.json`)).toBe(true);

requests = [];
await app.goto('/path-base/prefetching/prefetched');
expect(requests).toEqual([]);
});
});

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

0 comments on commit d22fbcb

Please sign in to comment.