This repository was archived by the owner on Aug 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
tutorial #105
Merged
Merged
tutorial #105
Changes from all 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
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
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
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
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
1 change: 0 additions & 1 deletion
1
content/tutorial/02-sveltekit/01-concepts/02-project-structure/app-a/src/routes/+page.svelte
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
content/tutorial/02-sveltekit/01-concepts/03-server-and-client/app-a/src/routes/+page.svelte
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
content/tutorial/02-sveltekit/01-concepts/04-server-side-rendering/README.md
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
.../tutorial/02-sveltekit/01-concepts/04-server-side-rendering/app-a/src/routes/+page.svelte
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
content/tutorial/02-sveltekit/01-concepts/05-prerendering/README.md
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
content/tutorial/02-sveltekit/01-concepts/05-prerendering/app-a/src/routes/+page.svelte
This file was deleted.
Oops, something went wrong.
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
7 changes: 5 additions & 2 deletions
7
content/tutorial/02-sveltekit/02-routing/01-pages/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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
<h1>Home</h1> | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
</nav> | ||
|
||
<h1>Home</h1> | ||
<p>This is the home page.</p> | ||
<p>Go to the <a href="/about">about</a> page.</p> |
7 changes: 5 additions & 2 deletions
7
content/tutorial/02-sveltekit/02-routing/01-pages/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,7 @@ | ||
<h1>About</h1> | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
</nav> | ||
|
||
<h1>About</h1> | ||
<p>This is the about page.</p> | ||
<p>Go to the <a href="/">home</a> page</p> |
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-a/src/routes/about/+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.
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
--- | ||
title: Layouts | ||
--- | ||
|
||
Different routes of your app will often share common UI. Instead of repeating it in each `+page.svelte` component, we can use a `+layout.svelte` component that applies to all routes in the same directory. | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In this app we have two routes, `src/routes/+page.svelte` and `src/routes/about/+page.svelte`, that contain the same navigation UI. Let's create a new file, `src/routes/+layout.svelte`... | ||
|
||
```diff | ||
src/routes/ | ||
├ about/ | ||
│ └ +page.svelte | ||
+├ +layout.svelte | ||
└ +page.svelte | ||
``` | ||
|
||
...and move the duplicated content from the `+page.svelte` files into the new `+layout.svelte` file. The `<slot />` element is where the page content will be rendered: | ||
|
||
```svelte | ||
/// file: src/routes/about/+page.svelte | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
</nav> | ||
|
||
<slot /> | ||
``` | ||
|
||
A `+layout.svelte` file applies to every child route, including the sibling `+page.svelte` (if it exists). You can nest layouts to arbitrary depth. |
6 changes: 6 additions & 0 deletions
6
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,6 @@ | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
</nav> | ||
|
||
<slot /> |
2 changes: 2 additions & 0 deletions
2
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,2 @@ | ||
<h1>Home</h1> | ||
<p>This is the home page.</p> |
2 changes: 0 additions & 2 deletions
2
...pages/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,2 @@ | ||
<h1>About</h1> | ||
|
||
<p>This is the about page.</p> | ||
<p>Go to the home page</p> |
16 changes: 16 additions & 0 deletions
16
content/tutorial/02-sveltekit/02-routing/03-params/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,16 @@ | ||
--- | ||
title: Route parameters | ||
--- | ||
|
||
To create routes with dynamic parameters, use square brackets around a valid variable name. For example, a file like `src/routes/blog/[slug]/+page.svelte` will create a route that matches `/blog/one`, `/blog/two`, `/blog/three` and so on. | ||
|
||
Let's create that file: | ||
|
||
```svelte | ||
/// file: src/routes/blog/[slug]/+page.svelte | ||
<h1>Blog post</h1> | ||
``` | ||
|
||
We can now navigate from the `/blog` page to individual blog posts. In the next chapter, we'll see how to load their content. | ||
|
||
> Multiple route parameters can appear _within_ one URL segment, as long as they are separated by at least one static character: `foo/[bar]x[baz]` is a valid route where `[bar]` and `[bar]` are dynamic parameters. |
6 changes: 6 additions & 0 deletions
6
content/tutorial/02-sveltekit/02-routing/03-params/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,6 @@ | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/blog">Blog</a> | ||
</nav> | ||
|
||
<slot /> |
1 change: 1 addition & 0 deletions
1
content/tutorial/02-sveltekit/02-routing/03-params/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>Home Page</p> |
7 changes: 7 additions & 0 deletions
7
content/tutorial/02-sveltekit/02-routing/03-params/app-a/src/routes/blog/+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,7 @@ | ||
<h1>Blog</h1> | ||
|
||
<ul> | ||
<li><a href="/blog/one">one</a></li> | ||
<li><a href="/blog/two">two</a></li> | ||
<li><a href="/blog/three">three</a></li> | ||
</ul> |
1 change: 1 addition & 0 deletions
1
content/tutorial/02-sveltekit/02-routing/03-params/app-b/src/routes/blog/[slug]/+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 @@ | ||
<h1>Blog post</h1> |
94 changes: 94 additions & 0 deletions
94
content/tutorial/02-sveltekit/03-loading-data/01-page-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,94 @@ | ||
--- | ||
title: Page data | ||
--- | ||
|
||
At its core, SvelteKit's job boils down to three things: | ||
|
||
1. **Routing** — figure out which route matches an incoming request | ||
2. **Loading** — get the data needed by the route | ||
3. **Rendering** - generate some HTML (on the server) or update the DOM (in the browser) | ||
|
||
We've seen how routing and rendering work. Let's talk about the middle part — loading. | ||
|
||
Every page of your app can declare a `load` function in a `+page.server.js` file alongside the `+page.svelte` file. As the file name suggests, this module only ever runs on the server, including for client-side navigations. Let's add a `src/routes/blog/+page.server.js` file so that we can replace the hard-coded links in `src/routes/blog/+page.svelte` with actual blog post data: | ||
|
||
```js | ||
/// file: src/routes/blog/+page.server.js | ||
import { posts } from './data.js'; | ||
|
||
export function load() { | ||
return { | ||
summaries: posts.map((post) => ({ | ||
slug: post.slug, | ||
title: post.title | ||
})) | ||
}; | ||
} | ||
``` | ||
|
||
> For the sake of the tutorial, we're importing data from `src/routes/blog/data.js`. In a real app, you'd be more likely to load the data from a database or a CMS, but for now we'll do it like this. | ||
|
||
We can access this data in `src/routes/blog/+page.svelte` via the `data` prop: | ||
|
||
```svelte | ||
+++<script> | ||
export let data; | ||
</script>+++ | ||
|
||
<h1>Blog</h1> | ||
|
||
<ul> | ||
--- <li><a href="/blog/one">one</a></li> | ||
<li><a href="/blog/two">two</a></li> | ||
<li><a href="/blog/three">three</a></li>--- | ||
+++ {#each data.summaries as { slug, title }} | ||
<li><a href="/blog/{slug}">{title}</a></li> | ||
{/each}+++ | ||
</ul> | ||
``` | ||
|
||
Now, let's do the same for the post page: | ||
|
||
```js | ||
/// file: src/routes/blog/[slug]/+page.server.js | ||
import { posts } from '../data.js'; | ||
|
||
export function load({ params }) { | ||
const post = posts.find((post) => post.slug === params.slug); | ||
|
||
return { | ||
post | ||
}; | ||
} | ||
``` | ||
|
||
```svelte | ||
/// file: src/routes/blog/[slug]/+page.svelte | ||
+++<script> | ||
export let data; | ||
</script>+++ | ||
|
||
---<h1>Blog post</h1>--- | ||
+++<h1>{data.post.title}</h1> | ||
<div>{@html data.post.content}</div>+++ | ||
``` | ||
|
||
There's one last detail we need to take care of — the user might visit an invalid pathname like `/blog/nope`, in which case we'd like to respond with a 404 page: | ||
Rich-Harris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js | ||
/// file: src/routes/blog/[slug]/+page.server.js | ||
+++import { error } from '@sveltejs/kit';+++ | ||
import { posts } from '../data.js'; | ||
|
||
export function load({ params }) { | ||
const post = posts.find((post) => post.slug === params.slug); | ||
|
||
+++if (!post) throw error(404);+++ | ||
|
||
return { | ||
post | ||
}; | ||
} | ||
``` | ||
|
||
We'll learn more about error handling in later chapters. |
6 changes: 6 additions & 0 deletions
6
content/tutorial/02-sveltekit/03-loading-data/01-page-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,6 @@ | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/blog">Blog</a> | ||
</nav> | ||
|
||
<slot /> |
1 change: 1 addition & 0 deletions
1
content/tutorial/02-sveltekit/03-loading-data/01-page-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>Home Page</p> |
7 changes: 7 additions & 0 deletions
7
...ent/tutorial/02-sveltekit/03-loading-data/01-page-data/app-a/src/routes/blog/+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,7 @@ | ||
<h1>Blog</h1> | ||
|
||
<ul> | ||
<li><a href="/blog/one">one</a></li> | ||
<li><a href="/blog/two">two</a></li> | ||
<li><a href="/blog/three">three</a></li> | ||
</ul> |
1 change: 1 addition & 0 deletions
1
...orial/02-sveltekit/03-loading-data/01-page-data/app-a/src/routes/blog/[slug]/+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 @@ | ||
<h1>Blog post</h1> |
21 changes: 21 additions & 0 deletions
21
content/tutorial/02-sveltekit/03-loading-data/01-page-data/app-a/src/routes/blog/data.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,21 @@ | ||
export const posts = [ | ||
{ | ||
slug: 'welcome', | ||
title: 'Welcome to the Aperture Science computer-aided enrichment center', | ||
content: | ||
'<p>We hope your brief detention in the relaxation vault has been a pleasant one.</p><p>Your specimen has been processed and we are now ready to begin the test proper.</p>' | ||
}, | ||
|
||
{ | ||
slug: 'safety', | ||
title: 'Safety notice', | ||
content: | ||
'<p>While safety is one of many Enrichment Center Goals, the Aperture Science High Energy Pellet, seen to the left of the chamber, can and has caused permanent disabilities, such as vaporization. Please be careful.</p>' | ||
}, | ||
|
||
{ | ||
slug: 'cake', | ||
title: 'This was a triumph', | ||
content: "<p>I'm making a note here: HUGE SUCCESS.</p>" | ||
} | ||
]; |
10 changes: 10 additions & 0 deletions
10
.../tutorial/02-sveltekit/03-loading-data/01-page-data/app-b/src/routes/blog/+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,10 @@ | ||
import { posts } from './data.js'; | ||
|
||
export function load() { | ||
return { | ||
summaries: posts.map((post) => ({ | ||
slug: post.slug, | ||
title: post.title | ||
})) | ||
}; | ||
} |
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.