-
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
tutorial #105
Changes from all commits
c1db80f
d4080dd
85a3643
4581ff3
f392697
99e07e1
b3112ad
4f7b0df
c46b15b
5401f33
db092ba
ddde6b4
b498e13
16178e4
bfaad11
0593cd9
441c00d
fc83dfd
d785916
33abc42
33f1ea8
286aef7
41c1355
b4ed498
207422c
4e7dbf5
237c9a0
d2f129a
1ab3d65
ae0c8a5
456c82c
81d3da8
a1dc977
6da6bae
889bee1
9d943c5
1f371e9
3b1226d
b08e212
2d97045
fa74f29
0cf6e87
3dfc716
c426ca0
e1c17dd
e27059d
c7e9127
d1dfdf3
7f9bfd9
78e3c89
996935c
e77c99c
660894d
683c839
e4de675
bc5ab84
3cf476e
f0b18e9
ef50ed1
5811953
7ab6e68
e0f9930
bb068a7
13911e6
c739801
eb981ab
48bb9d5
bbf7594
1091694
d8db15e
f144d35
6a7cfaa
a31b462
a8d2d26
79949cf
c73b093
b59b121
ca32193
d05450a
076d586
5dbbc8e
eebfe16
ba61b27
ba972cc
a0729d5
21b03fe
a3f99bf
26a5c21
e6bb885
f91d3e8
24d4178
e3875f3
e202d3e
409ced6
999ace6
a896d57
5f3a251
c616097
2fd3a2b
b05a44f
4910962
01863fd
b27ba39
2d387f4
3ca854a
f8bce43
4c7b409
b4c1c11
a51653e
74a1a98
4005ac1
0186cfc
13a40c9
473df77
10188bd
b9c1273
7c8507e
2145bb6
97acd03
af3e09c
67d5db2
09ad179
1b9a969
d427fd2
ae2075a
8357819
bc5f182
97c9d23
9daf010
38d4867
f8d27f0
e717404
fc55ecf
4e55485
47b0158
ac9f872
63f59ff
e90c46f
58fca02
9880bb6
0fb7626
e8a24c6
cf743b9
25d7cfc
19dc8aa
cf8bcab
197e2d4
7ece7f1
0a4f260
ce08bf6
39fee9b
19b89de
046a08e
66a2431
09bab9c
93435ac
f3450df
1290aea
59d1db5
29badc5
f60a799
95bd7cf
34593d6
91c51f7
fda2434
81d3ac9
5e9e8fc
7d4c5ec
85d9ef7
697a587
8b56439
f1a3c2f
3bad83e
b5de778
e9a030a
84cbbaa
a5d1378
c1d320f
cf8f6df
98682d2
0b010bc
a15e316
7a71008
e4597e9
75ebf85
f30c456
c6b9013
677157c
3bc561c
10a6c68
75d14b4
c5b5478
8b6d72b
dda8414
6063623
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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.
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. |
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> |
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: | ||
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. |
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 | ||
})) | ||
}; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.