-
Notifications
You must be signed in to change notification settings - Fork 240
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
tutorial #105
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 d4080dd
more chapters
dummdidumm 85a3643
slots
dummdidumm 4581ff3
chapter skeletons
dummdidumm f392697
mutating data section
dummdidumm 99e07e1
Apply suggestions from code review
dummdidumm b3112ad
errors and redirects chapter
dummdidumm 4f7b0df
Merge branch 'tutorial-routing' of https://github.com/sveltejs/learn.…
dummdidumm c46b15b
invalidation chapter
dummdidumm 5401f33
render options section
dummdidumm db092ba
add missing meta.json files
dummdidumm ddde6b4
advanced routing wip
dummdidumm b498e13
fix link
dummdidumm 16178e4
Merge branch 'main' into tutorial-routing
dummdidumm bfaad11
Merge branch 'main' into tutorial-routing
dummdidumm 0593cd9
matching, groups
dummdidumm 441c00d
Merge branch 'main' into tutorial-routing
dummdidumm fc83dfd
use new capabilities to adjust tutorials
dummdidumm d785916
remove duplicate layout from page
dummdidumm 33abc42
more sections
dummdidumm 33f1ea8
tweaks
dummdidumm 286aef7
Merge branch 'main' into tutorial-routing
dummdidumm 41c1355
remove log
dummdidumm b4ed498
disable csrf for tutorial
dummdidumm 207422c
Merge branch 'main' into tutorial-routing
dummdidumm 4e7dbf5
more content
dummdidumm 237c9a0
fix interval
dummdidumm d2f129a
leverage adding files functionality for error tutorial
dummdidumm 1ab3d65
simplify
dummdidumm ae0c8a5
tweak tutorials so people have to create the files
dummdidumm 456c82c
Merge branch 'main' into tutorial-routing
dummdidumm 81d3da8
Merge branch 'main' into tutorial-routing
dummdidumm a1dc977
Merge branch 'main' into tutorial-routing
dummdidumm 6da6bae
tweaks
dummdidumm 889bee1
tweaks and fixes
dummdidumm 9d943c5
Update content/tutorial/02-sveltekit/03-mutating-data/04-progressive-…
dummdidumm 1f371e9
switch example, because alert will prevent the ping events, resulting…
dummdidumm 3b1226d
labels for inputs
dummdidumm b08e212
Merge branch 'tutorial-routing' of https://github.com/sveltejs/learn.…
dummdidumm 2d97045
tweaks and fixes
dummdidumm fa74f29
Merge branch 'main' into tutorial-routing
dummdidumm 0cf6e87
fix
dummdidumm 3dfc716
Merge branch 'main' into tutorial-routing
dummdidumm c426ca0
Merge branch 'main' into tutorial-routing
dummdidumm e1c17dd
environment variables tutorial
dummdidumm e27059d
fixes
dummdidumm c7e9127
Merge branch 'main' into tutorial-routing
dummdidumm d1dfdf3
Update content/tutorial/02-sveltekit/01-concepts/03-server-and-client…
dummdidumm 7f9bfd9
Update content/tutorial/02-sveltekit/01-concepts/03-server-and-client…
dummdidumm 78e3c89
Update content/tutorial/02-sveltekit/01-concepts/04-server-side-rende…
dummdidumm 996935c
Update content/tutorial/02-sveltekit/01-concepts/04-server-side-rende…
dummdidumm e77c99c
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm 660894d
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm 683c839
Update content/tutorial/02-sveltekit/01-concepts/05-prerendering/READ…
dummdidumm e4de675
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm bc5ab84
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm 3cf476e
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm f0b18e9
Update content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
dummdidumm ef50ed1
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/…
dummdidumm 5811953
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/…
dummdidumm 7ab6e68
Update content/tutorial/02-sveltekit/02-routing/02-layouts/app-b/src/…
dummdidumm e0f9930
Update content/tutorial/02-sveltekit/02-routing/03-loading-data/READM…
dummdidumm bb068a7
Update content/tutorial/02-sveltekit/02-routing/04-loading-layout-dat…
dummdidumm 13911e6
Update content/tutorial/02-sveltekit/02-routing/07-params/README.md
dummdidumm c739801
Update content/tutorial/02-sveltekit/03-mutating-data/02-form-validat…
dummdidumm eb981ab
lowercase
dummdidumm 48bb9d5
Update content/tutorial/02-sveltekit/06-rendering-options/02-ssr/READ…
dummdidumm bbf7594
Update content/tutorial/02-sveltekit/06-rendering-options/03-csr/READ…
dummdidumm 1091694
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/01-…
dummdidumm d8db15e
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/02-…
dummdidumm f144d35
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/03-…
dummdidumm 6a7cfaa
Update content/tutorial/04-advanced-sveltekit/01-advanced-routing/04-…
dummdidumm a31b462
move explanatory content from concepts into render options chapter
dummdidumm a8d2d26
remove backticks
dummdidumm 79949cf
Update content/tutorial/02-sveltekit/02-routing/05-loading-server-dat…
dummdidumm c73b093
Update content/tutorial/02-sveltekit/02-routing/03-loading-data/READM…
Rich-Harris b59b121
Merge branch 'main' into tutorial-routing
dummdidumm ca32193
Loading (#115)
Rich-Harris d05450a
merge
Rich-Harris 076d586
fix casing
Rich-Harris 5dbbc8e
note configurability
Rich-Harris eebfe16
remove mention of vite
Rich-Harris ba61b27
fix solve button styles
Rich-Harris ba972cc
only add data-file attribute to files
Rich-Harris a0729d5
separate routing and loading into separate sections, etc
Rich-Harris 21b03fe
remove $types, for now
Rich-Harris a3f99bf
move stuff
Rich-Harris 26a5c21
use a todo list example - better showcases PE, avoids complexity arou…
Rich-Harris e6bb885
swap exercise order
Rich-Harris f91d3e8
flesh out form examples
Rich-Harris 24d4178
move invalidation to advanced section
Rich-Harris e3875f3
tweak page options docs
Rich-Harris e202d3e
move
Rich-Harris 409ced6
flesh out example to show use of page store etc
Rich-Harris 999ace6
small tweaks
dummdidumm a896d57
better redirects tutorial
dummdidumm 5f3a251
move $page.X usage into second chapter
dummdidumm c616097
await parent
dummdidumm 2fd3a2b
tweaks
dummdidumm b05a44f
better private env tutorials
dummdidumm 4910962
get rid of VITE_ prefix
dummdidumm 01863fd
Merge branch 'main' into tutorial-routing
dummdidumm b27ba39
fix gitignore
dummdidumm 2d387f4
Merge branch 'main' into tutorial-routing
dummdidumm 3ca854a
remove logging
Rich-Harris f8bce43
fix
Rich-Harris 4c7b409
fixes
Rich-Harris b4c1c11
invalid -> fail
Rich-Harris a51653e
robustify
Rich-Harris 74a1a98
rename section to the less-ambiguous "exercise" for my own sanity
Rich-Harris 4005ac1
allow an exercise to inherit files from previous exercises to reduce …
Rich-Harris 0186cfc
remove some now-unnecessary files
Rich-Harris 13a40c9
delete file
Rich-Harris 473df77
hide error customisation exercise for now - not ready to talk about h…
Rich-Harris 10188bd
update errors and redirects section
Rich-Harris b9c1273
more detail
Rich-Harris 7c8507e
tweaks
Rich-Harris 2145bb6
add inspector
Rich-Harris 97acd03
formatting
Rich-Harris af3e09c
make "next" links more sensible
Rich-Harris 67d5db2
fix chaining logic
Rich-Harris 09ad179
update optional params example
Rich-Harris 1b9a969
rest params example
Rich-Harris d427fd2
different param matcher example
Rich-Harris ae2075a
tidy up
Rich-Harris 8357819
move some content around
Rich-Harris bc5f182
add placeholder chapters on hooks and stores
Rich-Harris 97c9d23
env var stuff
Rich-Harris 9daf010
oops, didnt mean to add these
Rich-Harris 38d4867
tweak
Rich-Harris f8d27f0
style tweaks
Rich-Harris e717404
move filetree components to separate folder to ease future refactoring
Rich-Harris fc55ecf
tweaks
Rich-Harris 4e55485
remove warning spiel
Rich-Harris 47b0158
update deps
Rich-Harris ac9f872
use a regex
Rich-Harris 63f59ff
Revert "use a regex"
Rich-Harris e90c46f
try this instead
Rich-Harris 58fca02
use +layout.server.js instead of tutorial.json
Rich-Harris 9880bb6
make typescript a bit less complainy
Rich-Harris 0fb7626
show current exercise in menu
Rich-Harris e8a24c6
refactor filetree stuff a bit
Rich-Harris cf743b9
remove unused import
Rich-Harris 25d7cfc
simplify file creation, always select newly created file
Rich-Harris 19dc8aa
move icons
Rich-Harris cf8bcab
oops
Rich-Harris 197e2d4
derive depth at point of use
Rich-Harris 7ece7f1
simplify renaming
Rich-Harris 0a4f260
simplify
Rich-Harris ce08bf6
refactor filetree
Rich-Harris 39fee9b
remove unused import
Rich-Harris 19b89de
make editing constraints visible - is more accessible etc
Rich-Harris 046a08e
use a store for filesystem state
Rich-Harris 66a2431
make constraints more granular
Rich-Harris 09bab9c
DRY things out a bit
Rich-Harris 93435ac
more DRYing out
Rich-Harris f3450df
insertion order
Rich-Harris 1290aea
various fixes
Rich-Harris 59d1db5
a11y fix
Rich-Harris 29badc5
fix
Rich-Harris f60a799
context menu styling
Rich-Harris 95bd7cf
fixes
Rich-Harris 34593d6
DRY out actions
Rich-Harris 91c51f7
fix
Rich-Harris fda2434
fix
Rich-Harris 81d3ac9
fix
Rich-Harris 5e9e8fc
DRY out some more
Rich-Harris 7d4c5ec
refactor
Rich-Harris 85d9ef7
split up env var exercises
Rich-Harris 697a587
fix
Rich-Harris 8b56439
put <img> inside <button>
Rich-Harris f1a3c2f
remove some unnecessary files
Rich-Harris 3bad83e
add route group example that includes a load function
Rich-Harris b5de778
shuffle things around a bit
Rich-Harris e9a030a
make breakout example more visual
Rich-Harris 84cbbaa
oops
Rich-Harris a5d1378
bump kit
dummdidumm c1d320f
add way to relax file tree constraints
dummdidumm cf8f6df
fix complete/reset state toggle
dummdidumm 98682d2
pretty sure we actually want to check in the lockfile
Rich-Harris 0b010bc
tweak
Rich-Harris a15e316
remove TODO
Rich-Harris 7a71008
tweak example
Rich-Harris e4597e9
demonstrate returning non-serializable data from load function
Rich-Harris 75ebf85
rename exercise to invalidation
Rich-Harris f30c456
show a case where one load is invalidated and one isnt
Rich-Harris c6b9013
follow previous exercise, use generic data:blah key
Rich-Harris 677157c
follow previous exercise
Rich-Harris 3bc561c
add coming soon messages to get us off the hook for now
Rich-Harris 10a6c68
remove old TODO
Rich-Harris 75d14b4
work around hotlinking limitation
Rich-Harris c5b5478
remove __hidden mechanism, its way too confusing. comment out this ex…
Rich-Harris 8b6d72b
add some default styles etc
Rich-Harris dda8414
more tidying up
Rich-Harris 6063623
explain CSRF stuff
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
content/tutorial/02-sveltekit/02-routing/02-endpoints/README.md
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
content/tutorial/02-sveltekit/02-routing/02-endpoints/app-a/src/routes/+page.svelte
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
content/tutorial/02-sveltekit/02-routing/02-endpoints/app-b/src/routes/about/+page.svelte
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
content/tutorial/02-sveltekit/02-routing/02-layouts/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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`: | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```svelte | ||
+++<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
</nav> | ||
+++ | ||
<slot /> | ||
+++ | ||
<style> | ||
nav { | ||
display: flex; | ||
justify-content: space-between; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padding: 1em; | ||
background-color: #eee; | ||
} | ||
</style> | ||
+++ | ||
``` | ||
|
||
> Layouts can also be nested | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
1 change: 1 addition & 0 deletions
1
content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<slot /> |
17 changes: 17 additions & 0 deletions
17
content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padding: 1em; | ||
background-color: #eee; | ||
} | ||
</style> |
17 changes: 17 additions & 0 deletions
17
content/tutorial/02-sveltekit/02-routing/02-layouts/app-a/src/routes/about/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padding: 1em; | ||
background-color: #eee; | ||
} | ||
</style> |
15 changes: 15 additions & 0 deletions
15
content/tutorial/02-sveltekit/02-routing/02-layouts/app-b/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padding: 1em; | ||
background-color: #eee; | ||
} | ||
</style> |
3 changes: 3 additions & 0 deletions
3
content/tutorial/02-sveltekit/02-routing/02-layouts/app-b/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>Home</h1> | ||
|
||
<p>This is the home page.</p> |
1 change: 0 additions & 1 deletion
1
...oints/app-a/src/routes/about/+page.svelte → ...youts/app-b/src/routes/about/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
content/tutorial/02-sveltekit/02-routing/03-loading-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Loading Data | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
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. | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
SvelteKit provides a better way to do this through `load` functions. | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This `load` function can appear in several places. Let's start with the `load` function inside `+page.js`. | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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`. | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 }; | ||
}+++ | ||
``` |
Empty file.
12 changes: 12 additions & 0 deletions
12
content/tutorial/02-sveltekit/02-routing/03-loading-data/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
content/tutorial/02-sveltekit/02-routing/03-loading-data/app-a/src/routes/api/+server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
tanhauhau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
4 changes: 4 additions & 0 deletions
4
content/tutorial/02-sveltekit/02-routing/03-loading-data/app-b/src/routes/+page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
5 changes: 5 additions & 0 deletions
5
content/tutorial/02-sveltekit/02-routing/03-loading-data/app-b/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
export let data; | ||
</script> | ||
|
||
<p>{data.greeting}</p> |
28 changes: 28 additions & 0 deletions
28
content/tutorial/02-sveltekit/02-routing/04-loading-layout-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: Loading Layout Data | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
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`. | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
Empty file.
8 changes: 8 additions & 0 deletions
8
...t/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-a/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
4 changes: 4 additions & 0 deletions
4
content/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-a/src/routes/+page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
5 changes: 5 additions & 0 deletions
5
...ent/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
export let data; | ||
</script> | ||
|
||
<p>{data.greeting}</p> |
1 change: 1 addition & 0 deletions
1
...torial/02-sveltekit/02-routing/04-loading-layout-data/app-a/src/routes/about/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
About page |
5 changes: 5 additions & 0 deletions
5
...t/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-a/src/routes/api/+server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
4 changes: 4 additions & 0 deletions
4
content/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-b/src/routes/+layout.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
12 changes: 12 additions & 0 deletions
12
...t/tutorial/02-sveltekit/02-routing/04-loading-layout-data/app-b/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
28 changes: 28 additions & 0 deletions
28
content/tutorial/02-sveltekit/02-routing/05-loading-server-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: Loading Data on the Server | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
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`. | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
1 change: 1 addition & 0 deletions
1
.../tutorial/02-sveltekit/02-routing/05-loading-server-data/app-a/src/routes/+page.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { db } from './fake-db'; |
1 change: 1 addition & 0 deletions
1
...ent/tutorial/02-sveltekit/02-routing/05-loading-server-data/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>TODO</p> |
3 changes: 3 additions & 0 deletions
3
content/tutorial/02-sveltekit/02-routing/05-loading-server-data/app-a/src/routes/fake-db.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
6 changes: 6 additions & 0 deletions
6
.../tutorial/02-sveltekit/02-routing/05-loading-server-data/app-b/src/routes/+page.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
5 changes: 5 additions & 0 deletions
5
...ent/tutorial/02-sveltekit/02-routing/05-loading-server-data/app-b/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
export let data; | ||
</script> | ||
|
||
<p>{data.greeting}</p> |
3 changes: 3 additions & 0 deletions
3
content/tutorial/02-sveltekit/02-routing/05-loading-server-data/app-b/src/routes/fake-db.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
33 changes: 33 additions & 0 deletions
33
content/tutorial/02-sveltekit/02-routing/06-loading-layout-server-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: Loading Layout Data on the Server | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
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 |
1 change: 1 addition & 0 deletions
1
.../02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/+layout.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { db } from './fake-db'; |
8 changes: 8 additions & 0 deletions
8
...ial/02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
6 changes: 6 additions & 0 deletions
6
...al/02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/+page.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
5 changes: 5 additions & 0 deletions
5
...orial/02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
export let data; | ||
</script> | ||
|
||
<p>{data.greeting}</p> |
1 change: 1 addition & 0 deletions
1
...02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/about/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
About page |
3 changes: 3 additions & 0 deletions
3
...utorial/02-sveltekit/02-routing/06-loading-layout-server-data/app-a/src/routes/fake-db.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const db = { | ||
getGreeting: (toGreet) => Promise.resolve('Hello ' + toGreet) | ||
}; |
6 changes: 6 additions & 0 deletions
6
.../02-sveltekit/02-routing/06-loading-layout-server-data/app-b/src/routes/+layout.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.