You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorial/04-advanced-sveltekit/04-advanced-loading/01-universal-load-functions/README.md
+23-10Lines changed: 23 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,7 @@
2
2
title: Universal load functions
3
3
---
4
4
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.
11
6
12
7
Sometimes it doesn't make sense to load data from the server when doing a client-side navigation. For example:
13
8
@@ -16,9 +11,27 @@ Sometimes it doesn't make sense to load data from the server when doing a client
16
11
- You want to delay navigation until an image has been preloaded, to avoid pop-in
17
12
- 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
18
13
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`:
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
+
```
22
36
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.
0 commit comments