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 all 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
.env
.env.*
!.env.example
!/content/tutorial/04-advanced-sveltekit/06-environment-variables/**/.env
.apps
.vercel
/content/tutorial/common/.svelte-kit
/content/tutorial/common/node_modules
/content/tutorial/common/package-lock.json
/content/tutorial/common/node_modules
8 changes: 7 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
{
"files": ["content/**/*.svelte"],
"options": {
"printWidth": 50,
"printWidth": 70,
"requirePragma": true
}
},
{
"files": ["content/**/*.js"],
"options": {
"printWidth": 70
}
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ The next steps depend on whether you want to run this locally in filesystem mode

## Creating new tutorials

Tutorials live inside `content`. Each tutorial consists of a `README.md`, which is the text to the left, and `app-a` and `app-b` folders, which represent the initial and solved state. Files that stay the same can be omitted from `app-b`. Files are marked as deleted in `app-b` if they start with `__delete`. Folders that are marked as deleted in `app-b` if they contain a file named `__delete`. Folders that contain a file named `__hidden` are not visible in the file tree.
Tutorials live inside `content`. Each tutorial consists of a `README.md`, which is the text to the left, and `app-a` and `app-b` folders, which represent the initial and solved state. Files that stay the same can be omitted from `app-b`. Files are marked as deleted in `app-b` if they start with `__delete`. Folders that are marked as deleted in `app-b` if they contain a file named `__delete`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
title: Welcome to Svelte
---

> WARNING: this content is a work in progress and may be out of date. If you are learning Svelte, please refer to the [tutorial on svelte.dev](https://svelte.dev/tutorial) for now. If you are learning SvelteKit, refer to the [SvelteKit docs](https://kit.svelte.dev/docs).

Welcome to the Svelte tutorial! This will teach you everything you need to know to easily build fast, small web applications.

You can also consult the [API docs](https://svelte.dev/docs) and the [examples](https://svelte.dev/examples), or — if you're impatient to start hacking on your machine locally — create a project with `npm init svelte`.
Expand All @@ -25,8 +23,8 @@ This tutorial is split into four main parts:
- [Welcome to Svelte](/tutorial/welcome-to-svelte) (you are here)
- [Introduction to SvelteKit](/tutorial/introducing-sveltekit)
- [Advanced Svelte](/tutorial/tweens)
- [Advanced SvelteKit](/tutorial/sandbox)
- [Advanced SvelteKit](/tutorial/optional-params)

Each section will present an exercise designed to illustrate a feature. Later sections build on the knowledge gained in earlier ones, so it's recommended that you go from start to finish. If necessary, you can navigate via the menu above.
Each section will present an exercise designed to illustrate a feature. Later exercises build on the knowledge gained in earlier ones, so it's recommended that you go from start to finish. If necessary, you can navigate via the menu above.

If you get stuck, you can click the `solve` button to the left of the editor. (It's disabled on sections like this one that don't include an exercise.) Try not to rely on it too much; you will learn faster by figuring out where to put each suggested code block and manually typing it in to the editor.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
title: What is SvelteKit?
---

> WARNING: this content is a work in progress and is not up to date with breaking changes in the latest version of SvelteKit. For the latest information, refer to the [SvelteKit docs](https://kit.svelte.dev/docs).

So far, we've been working on individual components, or groups of components, in isolation. But to build a complete app, you need more than just components.

That's where SvelteKit comes in. Whereas Svelte is a _component framework_, SvelteKit is an _app framework_ (or 'metaframework', depending on who you ask) that solves the tricky problems of building something production-ready:
Expand All @@ -20,6 +18,6 @@ That's where SvelteKit comes in. Whereas Svelte is a _component framework_, Svel
- Deploying to different hosting providers
- ...and so on

It also provides a world-class development experience powered by [Vite](https://vitejs.dev). SvelteKit apps are server-rendered by default (like traditional 'multi-page apps' or MPAs) for excellent first load performance and SEO characteristics, but can then transition to client-side navigation (like modern 'single-page apps' or SPAs) to avoid jankily reloading everything (including things like third-party analytics code) when the user navigates. They can run anywhere JavaScript runs, though — as we'll see — your users may not need to run any JavaScript at all.
SvelteKit apps are server-rendered by default (like traditional 'multi-page apps' or MPAs) for excellent first load performance and SEO characteristics, but can then transition to client-side navigation (like modern 'single-page apps' or SPAs) to avoid jankily reloading everything (including things like third-party analytics code) when the user navigates. They can run anywhere JavaScript runs, though — as we'll see — your users may not need to run any JavaScript at all.

If that sounds complicated, worry not: you've been using SvelteKit this whole time!

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ A SvelteKit app can be thought of as two distinct entities working in tandem —

'Client' refers to the JavaScript that loads in the browser.

TODO
SvelteKit makes the two communicate with each other seamlessly. On the initial page load, the server renders the HTML, meaning content is visible as quickly as possible. The client then takes over in a process called 'hydration', so that subsequent navigations happen without full page reloads. It will request additional code and data from the server as needed.

> You can [adjust this behavior](https://kit.svelte.dev/docs/page-options) as needed. SvelteKit is very versatile!

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 14 additions & 4 deletions content/tutorial/02-sveltekit/02-routing/01-pages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ 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.

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.
The routes are located within `src/routes`. Every directory within which contains a `+page.svelte` file creates a route in your app.

> 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.
In this app we currently have one route — `src/routes/+page.svelte`, which maps to `/`.

Let's add a link in `src/routes/about/+page.svelte` back to the homepage:
Let's add a second route, `src/routes/about/+page.svelte`, which maps to `/about`:

```svelte
/// file: src/routes/about/+page.svelte
<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>
```

We can now navigate between `/` and `/about`.

> Unlike traditional multi-page apps, navigating to `/about` and back 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. (This behaviour can be [configured](https://kit.svelte.dev/docs/page-options).)
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>
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>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 29 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,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.

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.
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 />
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Home</h1>
<p>This is the home page.</p>
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 content/tutorial/02-sveltekit/02-routing/03-params/README.md
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.
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 />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Home Page</p>
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Blog post</h1>
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:

```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.
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 />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Home Page</p>
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Blog post</h1>
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>"
}
];
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
}))
};
}
Loading