Skip to content

Commit 02ba6a6

Browse files
Rich-Harriselliott-with-the-longest-name-on-githubgtm-nayan
authored
Replace data-sveltekit-prefetch/prefetch(...)/prefetchRoutes(...) (#7776)
* rename prefetch -> preload and prefetchRoutes -> prepare, tweak prepare API * rename data-sveltekit-prefetch to data-sveltekit-preload * update migration guide * various * shuffle some things around * refactor a tiny bit * fix * implement data-sveltekit-prepare * make it slightly more self-contained * keyboard events * named constants * handle malformed URLs * rename * use data-sveltekit-preload-code and data-sveltekit-preload-data * rename stuff * fix * docs * missed a spot * changesets * clarification * fix migration guide * tidy up * feat: Typing improvements to preload PR (#7841) * feat: Improve typings on data-sveltekit attributes * feat: improved constants * Update packages/kit/src/runtime/client/utils.js Co-authored-by: gtmnayan <[email protected]> * remove dev guard from validateAttributeValue * snake_case * dot notation * shorter name so prettier doesnt butcher the code * shorter name * get typechecking working * neaten * oops * tweak Co-authored-by: Rich Harris <[email protected]> Co-authored-by: gtmnayan <[email protected]> Co-authored-by: Rich Harris <[email protected]> * Update documentation/docs/30-advanced/30-link-options.md * move up explanation of mobile behaviour * clarify * Update packages/kit/src/runtime/client/client.js Co-authored-by: S. Elliott Johnson <[email protected]> * change "page" to "eager" * make side-effecty * oops * fix docs Co-authored-by: S. Elliott Johnson <[email protected]> Co-authored-by: gtmnayan <[email protected]>
1 parent 191583c commit 02ba6a6

File tree

38 files changed

+384
-209
lines changed

38 files changed

+384
-209
lines changed

Diff for: .changeset/cold-shoes-cheat.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/kit': patch
3+
'create-svelte': patch
4+
---
5+
6+
[breaking] Replace `data-sveltekit-prefetch` with `-preload-code` and `-preload-data`

Diff for: .changeset/many-schools-live.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[breaking] Rename `prefetch` to `preloadData` and `prefetchRoutes` to `preloadCode`

Diff for: documentation/docs/10-getting-started/10-introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ title: Introduction
1212

1313
SvelteKit is a framework for building extremely high-performance web apps.
1414

15-
Building an app with all the modern best practices is fiendishly complicated. Those practices include [build optimizations](https://vitejs.dev/guide/features.html#build-optimizations), so that you load only the minimal required code; [offline support](/docs/service-workers); [prefetching](/docs/link-options#data-sveltekit-prefetch) pages before the user initiates navigation; and [configurable rendering](/docs/page-options) that allows you to render your app [on the server](/docs/glossary#ssr) or [in the browser](/docs/glossary#csr-and-spa) at runtime or [at build-time](/docs/glossary#prerendering). SvelteKit does all the boring stuff for you so that you can get on with the creative part.
15+
Building an app with all the modern best practices is fiendishly complicated. Those practices include [build optimizations](https://vitejs.dev/guide/features.html#build-optimizations), so that you load only the minimal required code; [offline support](/docs/service-workers); [preloading](/docs/link-options#data-sveltekit-preload-data) pages before the user initiates navigation; and [configurable rendering](/docs/page-options) that allows you to render your app [on the server](/docs/glossary#ssr) or [in the browser](/docs/glossary#csr-and-spa) at runtime or [at build-time](/docs/glossary#prerendering). SvelteKit does all the boring stuff for you so that you can get on with the creative part.
1616

1717
It uses [Vite](https://vitejs.dev/) with a [Svelte plugin](https://github.com/sveltejs/vite-plugin-svelte) to provide a lightning-fast and feature-rich development experience with [Hot Module Replacement (HMR)](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#hot), where changes to your code are reflected in the browser instantly.
1818

Diff for: documentation/docs/30-advanced/30-link-options.md

+45-12
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,53 @@ In SvelteKit, `<a>` elements (rather than framework-specific `<Link>` components
66

77
You can customise the behaviour of links with `data-sveltekit-*` attributes. These can be applied to the `<a>` itself, or to a parent element.
88

9-
### data-sveltekit-prefetch
9+
### data-sveltekit-preload-data
1010

11-
To get a head start on importing the code and fetching the page's data, use the `data-sveltekit-prefetch` attribute, which will start loading everything as soon as the user hovers over the link (on a desktop) or touches it (on mobile), rather than waiting for the `click` event to trigger navigation. Typically, this buys us an extra couple of hundred milliseconds, which is the difference between a user interface that feels laggy, and one that feels snappy.
11+
Before the browser registers that the user has clicked on a link, we can detect that they've hovered the mouse over it (on desktop) or that a `touchstart` or `mousedown` event was triggered. In both cases, we can make an educated guess that a `click` event is coming.
1212

13-
To apply this behaviour across the board, add the attribute to a parent element (or even the `<body>` in your `src/app.html`):
13+
SvelteKit can use this information to get a head start on importing the code and fetching the page's data, which can give us an extra couple of hundred milliseconds — the difference between a user interface that feels laggy and one that feels snappy.
14+
15+
We can control this behaviour with the `data-sveltekit-preload-data` attribute, which can have one of two values:
16+
17+
- `"hover"` means that preloading will start if the mouse comes to a rest over a link. On mobile, preloading begins on `touchstart`
18+
- `"tap"` means that preloading will start as soon as a `touchstart` or `mousedown` event is registered
19+
20+
The default project template has a `data-sveltekit-preload-data="hover"` attribute applied to the `<body>` element in `src/app.html`, meaning that every link is preloaded on hover by default:
21+
22+
```html
23+
<body data-sveltekit-preload-data="hover">
24+
<div style="display: contents">%sveltekit.body%</div>
25+
</body>
26+
```
27+
28+
Sometimes, calling `load` when the user hovers over a link might be undesirable, either because it's likely to result in false positives (a click needn't follow a hover) or because data is updating very quickly and a delay could mean staleness.
29+
30+
In these cases, you can specify the `"tap"` value, which causes SvelteKit to call `load` only when the user taps or clicks on a link:
1431

1532
```html
16-
/// file: src/routes/+layout.svelte
17-
<main data-sveltekit-prefetch>
18-
<slot />
19-
</main>
33+
<a data-sveltekit-preload-data="tap" href="/stonks">
34+
Get current stonk values
35+
</a>
2036
```
2137

22-
> You can also programmatically invoke `prefetch` from `$app/navigation`.
38+
> You can also programmatically invoke `preloadData` from `$app/navigation`.
39+
40+
Data will never be preloaded if the user has chosen reduced data usage, meaning [`navigator.connection.saveData`](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData) is `true`.
41+
42+
### data-sveltekit-preload-code
43+
44+
Even in cases where you don't want to preload _data_ for a link, it can be beneficial to preload the _code_. The `data-sveltekit-preload-code` attribute works similarly to `data-sveltekit-preload-data`, except that it can take one of four values, in decreasing 'eagerness':
45+
46+
- `"eager"` means that links will be preloaded straight away
47+
- `"viewport"` means that links will be preloaded once they enter the viewport
48+
- `"hover"` - as above, except that only code is preloaded
49+
- `"tap"` - as above, except that only code is preloaded
50+
51+
Note that `viewport` and `eager` only apply to links that are present in the DOM immediately following navigation — if a link is added later (in an `{#if ...}` block, for example) it will not be preloaded until triggered by `hover` or `tap`. This is to avoid performance pitfalls resulting from aggressively observing the DOM for changes.
52+
53+
> Since preloading code is a prerequisite for preloading data, this attribute will only have an effect if it specifies a more eager value than any `data-sveltekit-preload-data` attribute that is present.
54+
55+
As with `data-sveltekit-preload-data`, this attribute will be ignored if the user has chosen reduced data usage.
2356

2457
### data-sveltekit-reload
2558

@@ -50,14 +83,14 @@ In certain cases, you may wish to disable this behaviour. Adding a `data-sveltek
5083
To disable any of these options inside an element where they have been enabled, use the `"off"` value:
5184

5285
```html
53-
<div data-sveltekit-prefetch>
54-
<!-- these links will be prefetched -->
86+
<div data-sveltekit-preload-data>
87+
<!-- these links will be preloaded -->
5588
<a href="/a">a</a>
5689
<a href="/b">b</a>
5790
<a href="/c">c</a>
5891

59-
<div data-sveltekit-prefetch="off">
60-
<!-- these links will NOT be prefetched -->
92+
<div data-sveltekit-preload-data="off">
93+
<!-- these links will NOT be preloaded -->
6194
<a href="/d">d</a>
6295
<a href="/e">e</a>
6396
<a href="/f">f</a>

Diff for: documentation/docs/60-appendix/10-migrating.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Your custom error page component should be renamed from `_error.svelte` to `+err
8585

8686
#### Imports
8787

88-
The `goto`, `prefetch` and `prefetchRoutes` imports from `@sapper/app` should be replaced with identical imports from [`$app/navigation`](/docs/modules#$app-navigation).
88+
The `goto`, `prefetch` and `prefetchRoutes` imports from `@sapper/app` should be replaced with `goto`, `preloadData` and `preloadCode` imports respectively from [`$app/navigation`](/docs/modules#$app-navigation).
8989

9090
The `stores` import from `@sapper/app` should be replaced — see the [Stores](/docs/migrating#pages-and-layouts-stores) section below.
9191

@@ -133,7 +133,7 @@ This caused problems and is no longer the case in SvelteKit. Instead, relative U
133133

134134
#### &lt;a&gt; attributes
135135

136-
- `sapper:prefetch` is now `data-sveltekit-prefetch`
136+
- `sapper:prefetch` is now `data-sveltekit-preload-data`
137137
- `sapper:noscroll` is now `data-sveltekit-noscroll`
138138

139139
### Endpoints

Diff for: packages/create-svelte/templates/default/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width" />
77
%sveltekit.head%
88
</head>
9-
<body data-sveltekit-prefetch>
9+
<body data-sveltekit-preload-data="hover">
1010
<div style="display: contents">%sveltekit.body%</div>
1111
</body>
1212
</html>

Diff for: packages/create-svelte/templates/skeleton/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width" />
77
%sveltekit.head%
88
</head>
9-
<body>
9+
<body data-sveltekit-preload-data="hover">
1010
<div style="display: contents">%sveltekit.body%</div>
1111
</body>
1212
</html>

Diff for: packages/kit/src/runtime/app/navigation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const disableScrollHandling = ssr
1717
export const goto = ssr ? guard('goto') : client.goto;
1818
export const invalidate = ssr ? guard('invalidate') : client.invalidate;
1919
export const invalidateAll = ssr ? guard('invalidateAll') : client.invalidateAll;
20-
export const prefetch = ssr ? guard('prefetch') : client.prefetch;
21-
export const prefetchRoutes = ssr ? guard('prefetchRoutes') : client.prefetch_routes;
20+
export const preloadData = ssr ? guard('preloadData') : client.preload_data;
21+
export const preloadCode = ssr ? guard('preloadCode') : client.preload_code;
2222
export const beforeNavigate = ssr ? () => {} : client.before_navigate;
2323
export const afterNavigate = ssr ? () => {} : client.after_navigate;

0 commit comments

Comments
 (0)