Skip to content

Commit 1ae0172

Browse files
committed
FIX: Format document.
1 parent 00e70ab commit 1ae0172

File tree

1 file changed

+103
-47
lines changed

1 file changed

+103
-47
lines changed

examples/tutorials/sveltekit.md

Lines changed: 103 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Building a SvelteKit app with sv and Dino
22

3-
SvelteKit has been a stable popular vote since its launch and with Svelte version 5 releasing recently, as of time of writing, there isn't a better time to show off running it with Deno!
3+
SvelteKit has been a stable popular vote since its launch and with Svelte
4+
version 5 releasing recently, as of time of writing, there isn't a better time
5+
to show off running it with Deno!
46

5-
Through this tutorial we will walk through setting up a SvelteKit project, made easier with the sv cli release and look at loading patters.
7+
Through this tutorial we will walk through setting up a SvelteKit project, made
8+
easier with the sv cli release and look at loading patters.
69

7-
You can see the finished app at [GitHub](https://github.com/s-petey/deno-sveltekit)
10+
You can see the finished app at
11+
[GitHub](https://github.com/s-petey/deno-sveltekit)
812

913
## Getting started
1014

11-
We can scaffold an application easily with `npx sv create`. This is [SvelteKit's CLI](https://github.com/sveltejs/cli) which has a lot of utility.
15+
We can scaffold an application easily with `npx sv create`. This is
16+
[SvelteKit's CLI](https://github.com/sveltejs/cli) which has a lot of utility.
1217

1318
https://github.com/user-attachments/assets/8357d46b-b535-44e3-9191-1496e0632bd1
1419

@@ -28,40 +33,54 @@ If you have followed the video above great! If not, here are the selections:
2833
- Package manager
2934
- Deno
3035

31-
For the remainder of this, you should have `deno task dev` running in the background so you can see your changes and the application running locally.
36+
For the remainder of this, you should have `deno task dev` running in the
37+
background so you can see your changes and the application running locally.
3238

3339
### Walkthrough
3440

3541
There are a few different folders to be mindful of.
3642

37-
`src` this is the root of your application code and where most of your time and effort will go.
43+
`src` this is the root of your application code and where most of your time and
44+
effort will go.
3845

39-
`src/lib` this is a SvelteKit aliased directory for fast import and where many of your helpers or library code will live.
46+
`src/lib` this is a SvelteKit aliased directory for fast import and where many
47+
of your helpers or library code will live.
4048

41-
`src/routes` this is the rendered pages for your application with SvelteKit, there is folder routing.
49+
`src/routes` this is the rendered pages for your application with SvelteKit,
50+
there is folder routing.
4251

4352
#### Good info
4453

45-
There are a few conventions which we will use in our SvelteKit application. (Although there are more available, I am only covering the ones used).
54+
There are a few conventions which we will use in our SvelteKit application.
55+
(Although there are more available, I am only covering the ones used).
4656

47-
- Files or folders with `server` in the name are meant to **only** be run on the server and may cause errors if you try to run them in the client.
57+
- Files or folders with `server` in the name are meant to **only** be run on the
58+
server and may cause errors if you try to run them in the client.
4859
- Within `src/routes` files have a naming conventions:
4960
- `+page.svelte` -- this is the rendered file in the browser
50-
- `+page.server.ts` -- This file will run on the server and sends, and can receive, data directly and type safely to the `+page.svelte` it is directly next to.
51-
- `+layout.svelte` -- a file which defines a layout with an outlet to be added to any `+page.svelte` on the same directory or any child directories.
52-
- `+error.svelte` -- A custom error page you can add to make error pages nicer to come across.
53-
54-
Additional note later you will see we put the `dino.ts` file within a `lib/server` directory. This will mean as stated above that this file is meant to **only** be accessed by other server files.
61+
- `+page.server.ts` -- This file will run on the server and sends, and can
62+
receive, data directly and type safely to the `+page.svelte` it is directly
63+
next to.
64+
- `+layout.svelte` -- a file which defines a layout with an outlet to be added
65+
to any `+page.svelte` on the same directory or any child directories.
66+
- `+error.svelte` -- A custom error page you can add to make error pages nicer
67+
to come across.
68+
69+
Additional note later you will see we put the `dino.ts` file within a
70+
`lib/server` directory. This will mean as stated above that this file is meant
71+
to **only** be accessed by other server files.
5572

5673
### Setup our "database"
5774

58-
We will be using a ts file with a `Map` to access and find our items for simplicity. Create a file and folder:
75+
We will be using a ts file with a `Map` to access and find our items for
76+
simplicity. Create a file and folder:
5977

6078
```
6179
src/lib/server/dino.ts
6280
```
6381

64-
Within this file we will set up a type for our `Dino`s and store the array of data to be exported as a map.
82+
Within this file we will set up a type for our `Dino`s and store the array of
83+
data to be exported as a map.
6584

6685
```ts
6786
export type Dino = { name: string; description: string };
@@ -83,7 +102,8 @@ With this setup we have our "database"! Next to access it on a page.
83102

84103
### Loading data to be rendered
85104

86-
We now need to create a `+page.server.ts` file which will be at the root of our routes directory. There should already be a `+page.svlete` there.
105+
We now need to create a `+page.server.ts` file which will be at the root of our
106+
routes directory. There should already be a `+page.svlete` there.
87107

88108
```
89109
src/routes/+page.server.ts
@@ -100,7 +120,9 @@ export const load = async ({ url }) => {
100120
});
101121
```
102122

103-
All we are doing here is converting our map to an array so we can see them rendered on the `+page.svelte`. Within this page you can remove anything you'd like or just add the following.
123+
All we are doing here is converting our map to an array so we can see them
124+
rendered on the `+page.svelte`. Within this page you can remove anything you'd
125+
like or just add the following.
104126

105127
```svelte
106128
<script lang="ts">
@@ -115,44 +137,54 @@ All we are doing here is converting our map to an array so we can see them rende
115137
</section>
116138
```
117139

118-
Notice while you are working with `data` we have type safety to know that `data.dinos` exists and the types that are available!
140+
Notice while you are working with `data` we have type safety to know that
141+
`data.dinos` exists and the types that are available!
119142

120143
### Adding an individual Dino page
121144

122-
Now that we are rendering each dino and have links on each of them setup, we can add a route to handle rendering this data.
145+
Now that we are rendering each dino and have links on each of them setup, we can
146+
add a route to handle rendering this data.
123147

124148
```
125149
src/routes/[name]/+page.server.ts
126150
src/routes/[name]/+page.svelte
127151
```
128152

129-
There is something neat and unique about this route. I am sure you noticed the `[name]` inserted as a folder name. This allows us to have a named route parameter. We could have used anything as the `name`, however we want to be able to route to `localhost:5173/Ingenia` and see our dinosaur and since that is the name I've used the parameter `name`.
153+
There is something neat and unique about this route. I am sure you noticed the
154+
`[name]` inserted as a folder name. This allows us to have a named route
155+
parameter. We could have used anything as the `name`, however we want to be able
156+
to route to `localhost:5173/Ingenia` and see our dinosaur and since that is the
157+
name I've used the parameter `name`.
130158

131-
With that explained now we can access this without our server loader to get our dino and send it to the page!
159+
With that explained now we can access this without our server loader to get our
160+
dino and send it to the page!
132161

133162
```ts
134163
/// src/routes/[name]/+page.server.ts
135-
import { dinos } from '$lib/server/dino.js';
136-
import { error } from '@sveltejs/kit';
164+
import { dinos } from "$lib/server/dino.js";
165+
import { error } from "@sveltejs/kit";
137166

138167
export const load = async ({ params: { name } }) => {
139168
const dino = dinos.get(name.toLowerCase());
140169

141170
if (!dino) {
142-
throw error(404, { message: 'Dino not found' });
171+
throw error(404, { message: "Dino not found" });
143172
}
144173

145174
return { name: dino.name, description: dino.description };
146175
};
147176
```
148177

149-
Notice we are throwing an error here. We don't have an `+error.svelte` page set up yet, so any errors will currently bubble to the default SvelteKit error page. Lets add one at the root level to handle any errors.
178+
Notice we are throwing an error here. We don't have an `+error.svelte` page set
179+
up yet, so any errors will currently bubble to the default SvelteKit error page.
180+
Lets add one at the root level to handle any errors.
150181

151182
```
152183
src/routes/+error.svelte
153184
```
154185

155-
This is a very simple page, feel free to spruce up the styles here or add your own flair!
186+
This is a very simple page, feel free to spruce up the styles here or add your
187+
own flair!
156188

157189
```svelte
158190
<script lang="ts">
@@ -162,9 +194,12 @@ This is a very simple page, feel free to spruce up the styles here or add your o
162194
<h1>{page.status}: {page.error?.message}</h1>
163195
```
164196

165-
We simply want to show that something went wrong and with the `page` exposed by SvelteKit, we can show the status code thrown and if there was a message attached to the error.
197+
We simply want to show that something went wrong and with the `page` exposed by
198+
SvelteKit, we can show the status code thrown and if there was a message
199+
attached to the error.
166200

167-
Now with that pesky error distraction handled, pun intended, we can get back to showing our individual dinosaur!
201+
Now with that pesky error distraction handled, pun intended, we can get back to
202+
showing our individual dinosaur!
168203

169204
```svelte
170205
<script lang="ts">
@@ -177,13 +212,18 @@ Now with that pesky error distraction handled, pun intended, we can get back to
177212
<p>{data.description}</p>
178213
```
179214

180-
Starting to work on this page you can see we still get the type safety knowing a `name` and `description` will exist on our data and we can render it!
215+
Starting to work on this page you can see we still get the type safety knowing a
216+
`name` and `description` will exist on our data and we can render it!
181217

182-
However, there is another problem if you navigate to this page, either by clicking on one of the links on the main page or by manually adding the dinosaur name to the URL we have no way of getting back!
218+
However, there is another problem if you navigate to this page, either by
219+
clicking on one of the links on the main page or by manually adding the dinosaur
220+
name to the URL we have no way of getting back!
183221

184222
### Layouts
185223

186-
We want to have a standard layout so that our pages can share different information or links. This can be achieved through `+layout.svelte` pages. Lets go ahead and update the one up at the root of the `routes` directory.
224+
We want to have a standard layout so that our pages can share different
225+
information or links. This can be achieved through `+layout.svelte` pages. Lets
226+
go ahead and update the one up at the root of the `routes` directory.
187227

188228
Here are a few things we want to achieve:
189229

@@ -216,30 +256,37 @@ Here are a few things we want to achieve:
216256
</footer>
217257
```
218258

219-
As you see, we are seeing `{@render children()}` for the first time. This just works as an "outlet" if you are coming from the React world to render whatever other child page may need to be output.
259+
As you see, we are seeing `{@render children()}` for the first time. This just
260+
works as an "outlet" if you are coming from the React world to render whatever
261+
other child page may need to be output.
220262

221-
Going back to your application you can see our heading `h1` has a link to go back to the home page.
263+
Going back to your application you can see our heading `h1` has a link to go
264+
back to the home page.
222265

223266
### Advanced routing, search parameters, and styling
224267

225-
We don't want to render all of the dinos at a single time; as that is too much to scroll through. We want our users to be able to search and click through pages of dinosaurs, which will also showcase another awesome Svelte 5 feature, snippets!
268+
We don't want to render all of the dinos at a single time; as that is too much
269+
to scroll through. We want our users to be able to search and click through
270+
pages of dinosaurs, which will also showcase another awesome Svelte 5 feature,
271+
snippets!
226272

227273
Lets open our main page and its server page to make a few changes.
228274

229-
Previously we were returning an array version of our dinos, lets allow users to search them and add some pagination logic.
275+
Previously we were returning an array version of our dinos, lets allow users to
276+
search them and add some pagination logic.
230277

231278
```ts
232-
import { dinos } from '$lib/server/dino.js';
279+
import { dinos } from "$lib/server/dino.js";
233280

234281
export const load = async ({ url }) => {
235282
// We can access the search params by using the `url` provided
236283
// by SvelteKit
237284
const queryParams = url.searchParams;
238285

239286
// We will use `q` as our search string
240-
const q = queryParams.get('q');
287+
const q = queryParams.get("q");
241288
// We will use `page` to know which page we are on
242-
const pageParam = queryParams.get('page');
289+
const pageParam = queryParams.get("page");
243290
let page = 1;
244291
// We should verify there is a page param, if there is verify it is a number
245292
// otherwise use our default of 1
@@ -249,7 +296,7 @@ export const load = async ({ url }) => {
249296
page = parsedPage;
250297
}
251298
}
252-
const limitParam = queryParams.get('limit');
299+
const limitParam = queryParams.get("limit");
253300
let limit = 25;
254301
// We should verify there is a limit param, if there is verify it is a number
255302
// otherwise use our default of 1
@@ -275,7 +322,7 @@ export const load = async ({ url }) => {
275322
const offset = Math.abs((page - 1) * limit);
276323
const paginatedDinos = Array.from(filteredDinos).slice(
277324
offset,
278-
offset + limit
325+
offset + limit,
279326
);
280327
const totalDinos = filteredDinos.length;
281328
const totalPages = Math.ceil(totalDinos / limit);
@@ -293,7 +340,8 @@ export const load = async ({ url }) => {
293340
};
294341
```
295342

296-
Wuuf, that was a lot of work, and with it out of the way lets get some pagination and search inputs added to the UI!
343+
Wuuf, that was a lot of work, and with it out of the way lets get some
344+
pagination and search inputs added to the UI!
297345

298346
```svelte
299347
<script lang="ts">
@@ -402,13 +450,21 @@ Wuuf, that was a lot of work, and with it out of the way lets get some paginatio
402450
</style>
403451
```
404452

405-
Notice for the input we use `defaultValue={data.q ?? ''}` so that when it is rendered in the UI we don't get `undefined` or `null` showing.
453+
Notice for the input we use `defaultValue={data.q ?? ''}` so that when it is
454+
rendered in the UI we don't get `undefined` or `null` showing.
406455

407-
With snippets you can create re-useable parts of Svelte code for easier rendering. `{#snippet pageButton(...)}` allows us to define the section to be rendered. We can then use it and pass the required type safe parameters using `{@render pageButton(...)}`. You can see this for all of the pagination buttons.
456+
With snippets you can create re-useable parts of Svelte code for easier
457+
rendering. `{#snippet pageButton(...)}` allows us to define the section to be
458+
rendered. We can then use it and pass the required type safe parameters using
459+
`{@render pageButton(...)}`. You can see this for all of the pagination buttons.
408460

409-
Another neat Svelte trick is whenever `<style>` is defined on the page, it is scoped only to the file it is used in. So we are able to add these classes knowing that it will not affect any of our other files if they also used that styling.
461+
Another neat Svelte trick is whenever `<style>` is defined on the page, it is
462+
scoped only to the file it is used in. So we are able to add these classes
463+
knowing that it will not affect any of our other files if they also used that
464+
styling.
410465

411-
I have updated some of the styling to make it more pleasant to look at, but I know you have great taste and are free to make it look however you'd like!
466+
I have updated some of the styling to make it more pleasant to look at, but I
467+
know you have great taste and are free to make it look however you'd like!
412468

413469
# App Showcase
414470

0 commit comments

Comments
 (0)