Skip to content

Commit d7fc288

Browse files
author
Rich Harris
committed
universal load function example
1 parent 5955e9f commit d7fc288

File tree

19 files changed

+129
-10
lines changed

19 files changed

+129
-10
lines changed

content/tutorial/04-advanced-sveltekit/04-advanced-loading/01-universal-load-functions/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
title: Universal load functions
33
---
44

5-
> Coming soon
6-
7-
In the meantime, read the [documentation](https://kit.svelte.dev/docs/load#shared-vs-server) to learn more about the distinction between server `load` functions and universal `load` functions.
8-
9-
<!--
10-
In the previous two exercises we loaded data from the server using `+page.server.js` and `+layout.server.js` files. This is very convenient if you need to do things like getting data directly from a database, or reading cookies.
5+
In the [previous section on loading](page-data) we loaded data from the server using `+page.server.js` and `+layout.server.js` files. This is very convenient if you need to do things like getting data directly from a database, or reading cookies.
116

127
Sometimes it doesn't make sense to load data from the server when doing a client-side navigation. For example:
138

@@ -16,9 +11,27 @@ Sometimes it doesn't make sense to load data from the server when doing a client
1611
- You want to delay navigation until an image has been preloaded, to avoid pop-in
1712
- You need to return something from `load` that can't be serialized (SvelteKit uses [devalue](https://github.com/Rich-Harris/devalue) to turn server data into JSON), such as a component or a store
1813

19-
In this example, we're loading data from an external API in `src/routes/+page.server.js` and `src/routes/item/[id]/+page.server.js`. That means that every time we navigate from one page to another, we're making a request to our server, which in turn makes a request to the API. That's an unnecessary detour that slows requests down and increases load on our server.
14+
In this exercise, we're dealing with the latter case. The server `load` functions in `src/routes/red/+page.server.js`, `src/routes/green/+page.server.js` and `src/routes/blue/+page.server.js` return a `component` constructor, which can't be serialized like data. If you navigate to `/red`, `/green` or `/blue`, you'll see a 'Data returned from `load` ... is not serializable' error in the terminal.
15+
16+
To turn the server `load` functions into universal `load` functions, rename each `+page.server.js` file to `+page.js`. Now, the functions will run on the server during server-side rendering, but will also run in the browser when the app hydrates or the user performs a client-side navigation.
17+
18+
We can now use the `component` returned from these `load` functions like any other value, including in `src/routes/+layout.svelte`:
19+
20+
```svelte
21+
/// file: src/routes/+layout.svelte
22+
<nav
23+
class:has-color={!!$page.data.color}
24+
style:background={$page.data.color ?? 'var(--bg-2)'}
25+
>
26+
<a href="/">home</a>
27+
<a href="/red">red</a>
28+
<a href="/green">green</a>
29+
<a href="/blue">blue</a>
2030
21-
Let's cut out the middleman: rename both `+page.server.js` files to `+page.js`.
31+
+++ {#if $page.data.component}
32+
<svelte:component this={$page.data.component} />
33+
{/if}+++
34+
</nav>
35+
```
2236

23-
Now, the `load` functions will run on the server during server-side rendering, but will run in the browser for subsequent client-side navigations. The trade-off is that we no longer have access to things that need a server (databases, cookies, private environment variables and so on), but in this case we don't need those things. Read the [documentation](https://kit.svelte.dev/docs/load#shared-vs-server) to learn more about the distinction between server `load` functions and universal `load` functions.
24-
-->
37+
Read the [documentation](https://kit.svelte.dev/docs/load#shared-vs-server) to learn more about the distinction between server `load` functions and universal `load` functions, and when to use which.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
</script>
4+
5+
<nav
6+
class:has-color={!!$page.data.color}
7+
style:background={$page.data.color ?? 'var(--bg-2)'}
8+
>
9+
<a href="/">home</a>
10+
<a href="/red">red</a>
11+
<a href="/green">green</a>
12+
<a href="/blue">blue</a>
13+
</nav>
14+
15+
<slot />
16+
17+
<style>
18+
nav.has-color,
19+
nav.has-color a {
20+
color: white;
21+
}
22+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>pick a colour</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './blue.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'blue',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>blue</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<marquee>hello from blue.svelte</marquee>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './green.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'green',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>green</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<marquee>hello from green.svelte</marquee>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './red.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'red',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>red</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<marquee>hello from red.svelte</marquee>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
</script>
4+
5+
<nav
6+
class:has-color={!!$page.data.color}
7+
style:background={$page.data.color ?? 'var(--bg-2)'}
8+
>
9+
<a href="/">home</a>
10+
<a href="/red">red</a>
11+
<a href="/green">green</a>
12+
<a href="/blue">blue</a>
13+
14+
{#if $page.data.component}
15+
<svelte:component this={$page.data.component} />
16+
{/if}
17+
</nav>
18+
19+
<slot />
20+
21+
<style>
22+
nav.has-color,
23+
nav.has-color a {
24+
color: white;
25+
}
26+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './blue.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'blue',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__delete
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './green.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'green',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__delete
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import component from './red.svelte';
2+
3+
export function load() {
4+
return {
5+
color: 'red',
6+
component
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__delete

0 commit comments

Comments
 (0)