Skip to content

Commit 826f39e

Browse files
author
Rich Harris
authored
make prefetching work (#620)
* make prefetching work - fixes #545 * tidy up
1 parent 13f23e1 commit 826f39e

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

.changeset/proud-sloths-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Make prefetching work

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Renderer {
6565
this.caches = new Map();
6666

6767
this.prefetching = {
68-
href: null,
68+
id: null,
6969
promise: null
7070
};
7171

@@ -212,6 +212,8 @@ export class Renderer {
212212
}
213213

214214
dispatchEvent(new CustomEvent('sveltekit:navigation-end'));
215+
this.prefetching.promise = null;
216+
this.prefetching.id = null;
215217
}
216218

217219
/**
@@ -220,15 +222,12 @@ export class Renderer {
220222
*/
221223
async prefetch(url) {
222224
const info = this.router.parse(url);
225+
223226
if (info) {
224-
if (url.href !== this.prefetching.href) {
225-
this.prefetching = {
226-
href: url.href,
227-
promise: this._get_navigation_result(info)
228-
};
229-
}
227+
this.prefetching.promise = this._get_navigation_result(info);
228+
this.prefetching.id = info.id;
230229

231-
return this.prefetching.promise;
230+
return await this.prefetching.promise;
232231
} else {
233232
throw new Error(`Could not prefetch ${url.href}`);
234233
}
@@ -239,6 +238,10 @@ export class Renderer {
239238
* @returns {Promise<import('./types').NavigationResult>}
240239
*/
241240
async _get_navigation_result(info) {
241+
if (this.prefetching.id === info.id) {
242+
return this.prefetching.promise;
243+
}
244+
242245
for (let i = 0; i < info.routes.length; i += 1) {
243246
const route = info.routes[i];
244247
const [pattern, parts, params] = route;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,10 @@ export class Router {
142142
const routes = this.routes.filter(([pattern]) => pattern.test(path));
143143

144144
if (routes.length > 0) {
145-
return {
146-
routes,
147-
path,
148-
query: new URLSearchParams(url.search)
149-
};
145+
const query = new URLSearchParams(url.search);
146+
const id = `${path}?${query}`;
147+
148+
return { id, routes, path, query };
150149
}
151150
}
152151

packages/kit/src/runtime/client/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CSRComponent, CSRRoute, Page } from '../../../types.internal';
22

33
export type NavigationInfo = {
4+
id: string;
45
routes: CSRRoute[];
56
path: string;
67
query: URLSearchParams;

packages/kit/test/apps/basics/src/routes/routing/__tests__.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ export default function (test) {
115115

116116
test('prefetches programmatically', '/routing/a', async ({ base, capture_requests, app, js }) => {
117117
if (js) {
118-
const requests = await capture_requests(() => app.prefetch('b'));
118+
const requests1 = await capture_requests(() => app.prefetch('/routing/prefetched'));
119+
assert.equal(requests1.length, 2, requests1.join(','));
120+
assert.equal(requests1[1], `${base}/routing/prefetched.json`);
119121

120-
assert.equal(requests.length, 2);
121-
assert.equal(requests[1], `${base}/routing/b.json`);
122+
const requests2 = await capture_requests(() => app.goto('/routing/prefetched'));
123+
assert.equal(requests2, []);
122124
}
123125
});
124126

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function get() {
2+
return { body: JSON.stringify('prefetched') };
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script context="module">
2+
/** @type {import('../../../../../../../types').Load} */
3+
export async function load({ fetch }) {
4+
const message = await fetch('/routing/prefetched.json').then(r => r.json());
5+
return { props: { message } };
6+
}
7+
</script>
8+
9+
<script>
10+
/** @type {string} */
11+
export let message;
12+
</script>
13+
14+
<h1>{message}</h1>

0 commit comments

Comments
 (0)