Skip to content

tutorial #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 195 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
195 commits
Select commit Hold shift + click to select a range
c1db80f
start of routing tutorial
dummdidumm Oct 17, 2022
d4080dd
more chapters
dummdidumm Oct 17, 2022
85a3643
slots
dummdidumm Oct 17, 2022
4581ff3
chapter skeletons
dummdidumm Oct 18, 2022
f392697
mutating data section
dummdidumm Oct 21, 2022
99e07e1
Apply suggestions from code review
dummdidumm Oct 21, 2022
b3112ad
errors and redirects chapter
dummdidumm Oct 21, 2022
4f7b0df
Merge branch 'tutorial-routing' of https://github.com/sveltejs/learn.…
dummdidumm Oct 21, 2022
c46b15b
invalidation chapter
dummdidumm Oct 25, 2022
5401f33
render options section
dummdidumm Oct 25, 2022
db092ba
add missing meta.json files
dummdidumm Oct 25, 2022
ddde6b4
advanced routing wip
dummdidumm Nov 15, 2022
b498e13
fix link
dummdidumm Nov 18, 2022
16178e4
Merge branch 'main' into tutorial-routing
dummdidumm Nov 23, 2022
bfaad11
Merge branch 'main' into tutorial-routing
dummdidumm Nov 25, 2022
0593cd9
matching, groups
dummdidumm Nov 25, 2022
441c00d
Merge branch 'main' into tutorial-routing
dummdidumm Nov 25, 2022
fc83dfd
use new capabilities to adjust tutorials
dummdidumm Nov 25, 2022
d785916
remove duplicate layout from page
dummdidumm Nov 25, 2022
33abc42
more sections
dummdidumm Nov 25, 2022
33f1ea8
tweaks
dummdidumm Nov 28, 2022
286aef7
Merge branch 'main' into tutorial-routing
dummdidumm Nov 28, 2022
41c1355
remove log
dummdidumm Nov 28, 2022
b4ed498
disable csrf for tutorial
dummdidumm Nov 28, 2022
207422c
Merge branch 'main' into tutorial-routing
dummdidumm Dec 1, 2022
4e7dbf5
more content
dummdidumm Dec 1, 2022
237c9a0
fix interval
dummdidumm Dec 1, 2022
d2f129a
leverage adding files functionality for error tutorial
dummdidumm Dec 1, 2022
1ab3d65
simplify
dummdidumm Dec 1, 2022
ae0c8a5
tweak tutorials so people have to create the files
dummdidumm Dec 1, 2022
456c82c
Merge branch 'main' into tutorial-routing
dummdidumm Dec 1, 2022
81d3da8
Merge branch 'main' into tutorial-routing
dummdidumm Dec 1, 2022
a1dc977
Merge branch 'main' into tutorial-routing
dummdidumm Dec 1, 2022
6da6bae
tweaks
dummdidumm Dec 1, 2022
889bee1
tweaks and fixes
dummdidumm Dec 1, 2022
9d943c5
Update content/tutorial/02-sveltekit/03-mutating-data/04-progressive-…
dummdidumm Dec 2, 2022
1f371e9
switch example, because alert will prevent the ping events, resulting…
dummdidumm Dec 2, 2022
3b1226d
labels for inputs
dummdidumm Dec 2, 2022
b08e212
Merge branch 'tutorial-routing' of https://github.com/sveltejs/learn.…
dummdidumm Dec 2, 2022
2d97045
tweaks and fixes
dummdidumm Dec 2, 2022
fa74f29
Merge branch 'main' into tutorial-routing
dummdidumm Dec 2, 2022
0cf6e87
fix
dummdidumm Dec 2, 2022
3dfc716
Merge branch 'main' into tutorial-routing
dummdidumm Dec 2, 2022
c426ca0
Merge branch 'main' into tutorial-routing
dummdidumm Dec 2, 2022
e1c17dd
environment variables tutorial
dummdidumm Dec 2, 2022
e27059d
fixes
dummdidumm Dec 2, 2022
c7e9127
Merge branch 'main' into tutorial-routing
dummdidumm Dec 2, 2022
d1dfdf3
Update content/tutorial/02-sveltekit/01-concepts/03-server-and-client…
dummdidumm Dec 2, 2022
7f9bfd9
Update content/tutorial/02-sveltekit/01-concepts/03-server-and-client…
dummdidumm Dec 2, 2022
78e3c89
Update content/tutorial/02-sveltekit/01-concepts/04-server-side-rende…
dummdidumm Dec 2, 2022
996935c
Update content/tutorial/02-sveltekit/01-concepts/04-server-side-rende…
dummdidumm Dec 2, 2022
e77c99c
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm Dec 2, 2022
660894d
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm Dec 2, 2022
683c839
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm Dec 2, 2022
e4de675
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm Dec 2, 2022
bc5ab84
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm Dec 2, 2022
3cf476e
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm Dec 2, 2022
f0b18e9
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm Dec 2, 2022
ef50ed1
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/…
dummdidumm Dec 2, 2022
5811953
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/…
dummdidumm Dec 2, 2022
7ab6e68
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-b/src/…
dummdidumm Dec 2, 2022
e0f9930
Update content/tutorial/02-sveltekit/02-routing/03-loading-data/READM…
dummdidumm Dec 2, 2022
bb068a7
Update content/tutorial/02-sveltekit/02-routing/04-loading-layout-dat…
dummdidumm Dec 2, 2022
13911e6
Update content/tutorial/02-sveltekit/02-routing/07-params/README.md
dummdidumm Dec 2, 2022
c739801
Update content/tutorial/02-sveltekit/03-mutating-data/02-form-validat…
dummdidumm Dec 2, 2022
eb981ab
lowercase
dummdidumm Dec 2, 2022
48bb9d5
Update content/tutorial/02-sveltekit/06-rendering-options/02-ssr/READ…
dummdidumm Dec 2, 2022
bbf7594
Update content/tutorial/02-sveltekit/06-rendering-options/03-csr/READ…
dummdidumm Dec 2, 2022
1091694
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/01-…
dummdidumm Dec 2, 2022
d8db15e
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/02-…
dummdidumm Dec 2, 2022
f144d35
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/03-…
dummdidumm Dec 2, 2022
6a7cfaa
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/04-…
dummdidumm Dec 2, 2022
a31b462
move explanatory content from concepts into render options chapter
dummdidumm Dec 2, 2022
a8d2d26
remove backticks
dummdidumm Dec 2, 2022
79949cf
Update content/tutorial/02-sveltekit/02-routing/05-loading-server-dat…
dummdidumm Dec 2, 2022
c73b093
Update content/tutorial/02-sveltekit/02-routing/03-loading-data/READM…
Rich-Harris Dec 5, 2022
b59b121
Merge branch 'main' into tutorial-routing
dummdidumm Dec 5, 2022
ca32193
Loading (#115)
Rich-Harris Dec 6, 2022
d05450a
merge
Rich-Harris Dec 6, 2022
076d586
fix casing
Rich-Harris Dec 6, 2022
5dbbc8e
note configurability
Rich-Harris Dec 6, 2022
eebfe16
remove mention of vite
Rich-Harris Dec 6, 2022
ba61b27
fix solve button styles
Rich-Harris Dec 6, 2022
ba972cc
only add data-file attribute to files
Rich-Harris Dec 6, 2022
a0729d5
separate routing and loading into separate sections, etc
Rich-Harris Dec 6, 2022
21b03fe
remove $types, for now
Rich-Harris Dec 6, 2022
a3f99bf
move stuff
Rich-Harris Dec 6, 2022
26a5c21
use a todo list example - better showcases PE, avoids complexity arou…
Rich-Harris Dec 6, 2022
e6bb885
swap exercise order
Rich-Harris Dec 6, 2022
f91d3e8
flesh out form examples
Rich-Harris Dec 6, 2022
24d4178
move invalidation to advanced section
Rich-Harris Dec 6, 2022
e3875f3
tweak page options docs
Rich-Harris Dec 7, 2022
e202d3e
move
Rich-Harris Dec 7, 2022
409ced6
flesh out example to show use of page store etc
Rich-Harris Dec 7, 2022
999ace6
small tweaks
dummdidumm Dec 7, 2022
a896d57
better redirects tutorial
dummdidumm Dec 8, 2022
5f3a251
move $page.X usage into second chapter
dummdidumm Dec 8, 2022
c616097
await parent
dummdidumm Dec 8, 2022
2fd3a2b
tweaks
dummdidumm Dec 9, 2022
b05a44f
better private env tutorials
dummdidumm Dec 9, 2022
4910962
get rid of VITE_ prefix
dummdidumm Dec 9, 2022
01863fd
Merge branch 'main' into tutorial-routing
dummdidumm Dec 9, 2022
b27ba39
fix gitignore
dummdidumm Dec 9, 2022
2d387f4
Merge branch 'main' into tutorial-routing
dummdidumm Dec 9, 2022
3ca854a
remove logging
Rich-Harris Dec 9, 2022
f8bce43
fix
Rich-Harris Dec 9, 2022
4c7b409
fixes
Rich-Harris Dec 9, 2022
b4c1c11
invalid -> fail
Rich-Harris Dec 9, 2022
a51653e
robustify
Rich-Harris Dec 9, 2022
74a1a98
rename section to the less-ambiguous "exercise" for my own sanity
Rich-Harris Dec 9, 2022
4005ac1
allow an exercise to inherit files from previous exercises to reduce …
Rich-Harris Dec 9, 2022
0186cfc
remove some now-unnecessary files
Rich-Harris Dec 9, 2022
13a40c9
delete file
Rich-Harris Dec 9, 2022
473df77
hide error customisation exercise for now - not ready to talk about h…
Rich-Harris Dec 9, 2022
10188bd
update errors and redirects section
Rich-Harris Dec 10, 2022
b9c1273
more detail
Rich-Harris Dec 10, 2022
7c8507e
tweaks
Rich-Harris Dec 10, 2022
2145bb6
add inspector
Rich-Harris Dec 10, 2022
97acd03
formatting
Rich-Harris Dec 10, 2022
af3e09c
make "next" links more sensible
Rich-Harris Dec 10, 2022
67d5db2
fix chaining logic
Rich-Harris Dec 10, 2022
09ad179
update optional params example
Rich-Harris Dec 10, 2022
1b9a969
rest params example
Rich-Harris Dec 10, 2022
d427fd2
different param matcher example
Rich-Harris Dec 11, 2022
ae2075a
tidy up
Rich-Harris Dec 11, 2022
8357819
move some content around
Rich-Harris Dec 11, 2022
bc5f182
add placeholder chapters on hooks and stores
Rich-Harris Dec 11, 2022
97c9d23
env var stuff
Rich-Harris Dec 11, 2022
9daf010
oops, didnt mean to add these
Rich-Harris Dec 11, 2022
38d4867
tweak
Rich-Harris Dec 11, 2022
f8d27f0
style tweaks
Rich-Harris Dec 11, 2022
e717404
move filetree components to separate folder to ease future refactoring
Rich-Harris Dec 11, 2022
fc55ecf
tweaks
Rich-Harris Dec 11, 2022
4e55485
remove warning spiel
Rich-Harris Dec 11, 2022
47b0158
update deps
Rich-Harris Dec 11, 2022
ac9f872
use a regex
Rich-Harris Dec 11, 2022
63f59ff
Revert "use a regex"
Rich-Harris Dec 11, 2022
e90c46f
try this instead
Rich-Harris Dec 11, 2022
58fca02
use +layout.server.js instead of tutorial.json
Rich-Harris Dec 11, 2022
9880bb6
make typescript a bit less complainy
Rich-Harris Dec 11, 2022
0fb7626
show current exercise in menu
Rich-Harris Dec 11, 2022
e8a24c6
refactor filetree stuff a bit
Rich-Harris Dec 11, 2022
cf743b9
remove unused import
Rich-Harris Dec 11, 2022
25d7cfc
simplify file creation, always select newly created file
Rich-Harris Dec 11, 2022
19dc8aa
move icons
Rich-Harris Dec 11, 2022
cf8bcab
oops
Rich-Harris Dec 11, 2022
197e2d4
derive depth at point of use
Rich-Harris Dec 11, 2022
7ece7f1
simplify renaming
Rich-Harris Dec 11, 2022
0a4f260
simplify
Rich-Harris Dec 11, 2022
ce08bf6
refactor filetree
Rich-Harris Dec 11, 2022
39fee9b
remove unused import
Rich-Harris Dec 11, 2022
19b89de
make editing constraints visible - is more accessible etc
Rich-Harris Dec 11, 2022
046a08e
use a store for filesystem state
Rich-Harris Dec 11, 2022
66a2431
make constraints more granular
Rich-Harris Dec 11, 2022
09bab9c
DRY things out a bit
Rich-Harris Dec 11, 2022
93435ac
more DRYing out
Rich-Harris Dec 12, 2022
f3450df
insertion order
Rich-Harris Dec 12, 2022
1290aea
various fixes
Rich-Harris Dec 12, 2022
59d1db5
a11y fix
Rich-Harris Dec 12, 2022
29badc5
fix
Rich-Harris Dec 12, 2022
f60a799
context menu styling
Rich-Harris Dec 12, 2022
95bd7cf
fixes
Rich-Harris Dec 12, 2022
34593d6
DRY out actions
Rich-Harris Dec 12, 2022
91c51f7
fix
Rich-Harris Dec 12, 2022
fda2434
fix
Rich-Harris Dec 12, 2022
81d3ac9
fix
Rich-Harris Dec 12, 2022
5e9e8fc
DRY out some more
Rich-Harris Dec 12, 2022
7d4c5ec
refactor
Rich-Harris Dec 12, 2022
85d9ef7
split up env var exercises
Rich-Harris Dec 12, 2022
697a587
fix
Rich-Harris Dec 12, 2022
8b56439
put <img> inside <button>
Rich-Harris Dec 12, 2022
f1a3c2f
remove some unnecessary files
Rich-Harris Dec 12, 2022
3bad83e
add route group example that includes a load function
Rich-Harris Dec 12, 2022
b5de778
shuffle things around a bit
Rich-Harris Dec 12, 2022
e9a030a
make breakout example more visual
Rich-Harris Dec 12, 2022
84cbbaa
oops
Rich-Harris Dec 12, 2022
a5d1378
bump kit
dummdidumm Dec 12, 2022
c1d320f
add way to relax file tree constraints
dummdidumm Dec 12, 2022
cf8f6df
fix complete/reset state toggle
dummdidumm Dec 12, 2022
98682d2
pretty sure we actually want to check in the lockfile
Rich-Harris Dec 12, 2022
0b010bc
tweak
Rich-Harris Dec 12, 2022
a15e316
remove TODO
Rich-Harris Dec 12, 2022
7a71008
tweak example
Rich-Harris Dec 12, 2022
e4597e9
demonstrate returning non-serializable data from load function
Rich-Harris Dec 12, 2022
75ebf85
rename exercise to invalidation
Rich-Harris Dec 12, 2022
f30c456
show a case where one load is invalidated and one isnt
Rich-Harris Dec 12, 2022
c6b9013
follow previous exercise, use generic data:blah key
Rich-Harris Dec 12, 2022
677157c
follow previous exercise
Rich-Harris Dec 12, 2022
3bc561c
add coming soon messages to get us off the hook for now
Rich-Harris Dec 12, 2022
10a6c68
remove old TODO
Rich-Harris Dec 12, 2022
75d14b4
work around hotlinking limitation
Rich-Harris Dec 12, 2022
c5b5478
remove __hidden mechanism, its way too confusing. comment out this ex…
Rich-Harris Dec 12, 2022
8b6d72b
add some default styles etc
Rich-Harris Dec 12, 2022
dda8414
more tidying up
Rich-Harris Dec 12, 2022
6063623
explain CSRF stuff
Rich-Harris Dec 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions content/tutorial/02-sveltekit/02-routing/01-pages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ title: Pages

SvelteKit uses filesystem-based routing, which means that the _routes_ of your app — in other words, what the app should do when a user navigates to a particular URL — are defined by the directories in your codebase.

The routes are located within `src/routes`. Every directory within which contains a `+page.svelte` file creates a route in your app.

In this app we have two routes — `src/routes/+page.svelte`, which maps to `/`, and `src/routes/about/+page.svelte`, which maps to `/about`. Clicking the `about` link will take you from the home page to the about page.

> Unlike traditional multi-page apps, navigating to `/about` updates the contents of the current page, like a single-page app. This gives us the best of both worlds — fast server-rendered startup, then instant navigation.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

32 changes: 32 additions & 0 deletions content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Layouts
---

Sections of your app often share a common UI. To not repeat that code, we can use layouts.

In this app we have two routes — `src/routes/+page.svelte`, and `src/routes/about/+page.svelte`. Both contain the same header which enables us to navigate around.

We can deduplicate this code by moving the common UI into a `+layout.svelte` file. All pages in the same folder or below this file will share that UI. The layout itself needs a `<slot />` at minimum to define where the content is projected.

Let's move the duplicate content from `src/routes/+page.svelte` and `src/routes/about/+page.svelte` into `src/routes/+layout.svelte`:

```svelte
+++<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
+++
<slot />
+++
<style>
nav {
display: flex;
justify-content: space-between;
padding: 1em;
background-color: #eee;
}
</style>
+++
```

> Layouts can also be nested
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<h1>Home</h1>

<p>This is the home page.</p>

<style>
nav {
display: flex;
justify-content: space-between;
padding: 1em;
background-color: #eee;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<h1>About</h1>

<p>This is the about page.</p>

<style>
nav {
display: flex;
justify-content: space-between;
padding: 1em;
background-color: #eee;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<slot />

<style>
nav {
display: flex;
justify-content: space-between;
padding: 1em;
background-color: #eee;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Home</h1>

<p>This is the home page.</p>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<h1>About</h1>

<p>This is the about page.</p>
<p>Go to the home page</p>
36 changes: 36 additions & 0 deletions content/tutorial/02-sveltekit/02-routing/03-loading-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: Loading Data
---

So far, we have seen how to create pages and layouts with static UI data. Most of your apps need to load some kind of data however. You could do this with a regular `fetch` inside `onMount`. That doesn't work with server-side-rendering or prerendering though, and you'd have half-finished pages loading data only after they are rendered.

SvelteKit provides a better way to do this through `load` functions.

This `load` function can appear in several places. Let's start with the `load` function inside `+page.js`.

This `load` function runs on the server when the user visits your page initially, and on the client for all further navigations - it's isomorphic. We call it a "shared `load` function". To harmonize the differences between client and server, use the `fetch` function passed to `load` to load data. The return type of the `load` function needs to be an object at the top level, below that everything is allowed. If we return a promise, it's awaited. The result is passed to the `data` prop inside `+page.svelte`, so we can access it through `export let data`.

Let's move the call inside `src/routes/+page.svelte` into `+page.js`:

```svelte
<script>
--- import { onMount } from 'svelte';

let greeting;
onMount(async () => {
greeting = await fetch('/api').then((r) =>
r.text()
);
});---
+++ export let data;+++
</script>

<p>{+++data.+++greeting}</p>
```

```js
+++export async function load({ fetch }) {
const greeting = await fetch('/api').then((r) => r.text());
return { greeting };
}+++
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { onMount } from 'svelte';

let greeting;
onMount(async () => {
greeting = await fetch('/api').then((r) =>
r.text()
);
});
</script>

<p>{greeting}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// We'll get to +server.js in a later chapter

export function GET() {
return new Response('Hello from your API');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function load({ fetch }) {
const greeting = await fetch('/api').then((r) => r.text());
return { greeting };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.greeting}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Loading Layout Data
---

Layouts can provide common UI across pages. They can also provide common data across pages.

Providing this data is similar to the `load` function in `+page.js`, only that we place it inside `+layout.js` - notice the symmetry?

Like in `+page.js`, this `load` function runs on the server when the user visits your page initially, and on the client for all further navigations - it's isomorphic. We call it a "shared `load` function". To harmonize the differences between client and server, use the `fetch` function passed to `load` to load data. The return type of the `load` function needs to be an object at the top level, below that everything is allowed. If we return a promise, it's awaited. The result is passed to the `data` prop inside `+layout.svelte`, so we can access it through `export let data`.

Let's make the same call inside `src/routes/+layout.js` as we are already doing in `src/routes/+page.js`, switch the search param from `page` to `layout`:

```svelte
+++<script>
export let data;
</script>
+++
<h1>---TODO---+++{data.greeting}+++</h1>
```

```js
+++export async function load({ fetch }) {
const greeting = await fetch('/api?layout').then((r) => r.text());
return { greeting };
}+++
```

> The data for `+page.js` and `+layout.js` will be loaded in parallel, so your UI renders faster
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>TODO</h1>

<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function load({ fetch }) {
const greeting = await fetch('/api?page').then((r) => r.text());
return { greeting };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.greeting}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
About page
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// We'll get to +server.js in a later chapter

export function GET({ url }) {
return new Response('Hello ' + url.search);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function load({ fetch }) {
const greeting = await fetch('/api?layout').then((r) => r.text());
return { greeting };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
export let data;
</script>

<h1>{data.greeting}</h1>

<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Loading Data on the Server
---

We have seen how to load data for pages and layouts through `load` functions in `+page.js` and `+layout.js`. These functions run on both the server and the client. This has some advantages but also some drawbacks, for example you can't directly query your database, you have to use `fetch`.

Luckily, SvelteKit provides a way to run a `load` _only_ on the server so you can access all your server-related data directly, by placing it inside `+page.server.js`. We call it a "server-only `load` function". The return type of the `load` function needs to be an object at the top level, and it needs to be serializable with `devalue`, which means JSON-objects and some JavaScript objects like `Date`s are allowed, but not custom classes for example. If we return a promise, it's awaited. The result is passed to the `data` prop inside `+page.svelte`, so we can access it through `export let data`.

Let's make a call to our fake database inside `src/routes/+page.server.js` and show the result in `src/routes/+page.svelte`:

```svelte
+++<script>
export let data
</script>
+++
<p>---TODO---+++{data.greeting}+++</p>
```

```js
import { db } from './fake-db.js';

+++export async function load() {
const greeting = await db.getGreeting();
return { greeting };
}+++
```

> Noticed how we can place `fake-db.js` right next to our page and layout files without risking to accidentally creating another route? This is one of several advantages of our "`+X` creates a route-related file"-convention
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { db } from './fake-db';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>TODO</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const db = {
getGreeting: () => Promise.resolve('Hello from your fake data base')
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { db } from './fake-db';

export async function load() {
const greeting = await db.getGreeting();
return { greeting };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.greeting}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const db = {
getGreeting: () => Promise.resolve('Hello from your fake data base')
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Loading Layout Data on the Server
---

Layouts can provide common UI across pages. They can also provide common data across pages. Like pages, they can do so from a shared `load` function or a server-only `load` function.

If you have worked through the previous tutorial chapters in order, you may notice a pattern by now:

- Load data for a page inside `+page.js` or `+page.server.js`
- Load data for a layout inside `+layout.js` or `+layout.server.js`

In this chapter, we look at the last of those four possibilities by placing the `load` function inside `+layout.server.js`. We call it a "server-only `load` function". The return type of the `load` function needs to be an object at the top level, and it needs to be serializable with `devalue`, which means JSON-objects and some JavaScript objects like `Date`s are allowed, but not custom classes for example. If we return a promise, it's awaited. The result is passed to the `data` prop inside `+layout.svelte`, so we can access it through `export let data`.

Let's make a call to our fake database inside `src/routes/+layout.server.js` and show the result in `src/routes/+layout.svelte`:

```svelte
+++<script>
export let data
</script>
+++
<p>---TODO---+++{data.greeting}+++</p>
```

```js
import { db } from './fake-db.js';

+++export async function load() {
const greeting = await db.getGreeting('layout');
return { greeting };
}+++
```

> The data for `+page.server.js` and `+layout.server.js` will be loaded in parallel, so your UI renders faster
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { db } from './fake-db';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>TODO</h1>

<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { db } from './fake-db';

export async function load() {
const greeting = await db.getGreeting('page');
return { greeting };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.greeting}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
About page
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const db = {
getGreeting: (toGreet) => Promise.resolve('Hello ' + toGreet)
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { db } from './fake-db';

export async function load() {
const greeting = await db.getGreeting('layout');
return { greeting };
}
Loading