You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
4
6
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.
6
9
7
-
You can see the finished app at [GitHub](https://github.com/s-petey/deno-sveltekit)
@@ -28,40 +33,54 @@ If you have followed the video above great! If not, here are the selections:
28
33
- Package manager
29
34
- Deno
30
35
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.
32
38
33
39
### Walkthrough
34
40
35
41
There are a few different folders to be mindful of.
36
42
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.
38
45
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.
40
48
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.
42
51
43
52
#### Good info
44
53
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).
46
56
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.
48
59
- Within `src/routes` files have a naming conventions:
49
60
-`+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.
55
72
56
73
### Setup our "database"
57
74
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:
59
77
60
78
```
61
79
src/lib/server/dino.ts
62
80
```
63
81
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
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.
104
126
105
127
```svelte
106
128
<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
115
137
</section>
116
138
```
117
139
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!
119
142
120
143
### Adding an individual Dino page
121
144
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.
123
147
124
148
```
125
149
src/routes/[name]/+page.server.ts
126
150
src/routes/[name]/+page.svelte
127
151
```
128
152
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`.
130
158
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
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.
150
181
151
182
```
152
183
src/routes/+error.svelte
153
184
```
154
185
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!
156
188
157
189
```svelte
158
190
<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
162
194
<h1>{page.status}: {page.error?.message}</h1>
163
195
```
164
196
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.
166
200
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!
168
203
169
204
```svelte
170
205
<script lang="ts">
@@ -177,13 +212,18 @@ Now with that pesky error distraction handled, pun intended, we can get back to
177
212
<p>{data.description}</p>
178
213
```
179
214
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!
181
217
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!
183
221
184
222
### Layouts
185
223
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.
187
227
188
228
Here are a few things we want to achieve:
189
229
@@ -216,30 +256,37 @@ Here are a few things we want to achieve:
216
256
</footer>
217
257
```
218
258
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.
220
262
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.
222
265
223
266
### Advanced routing, search parameters, and styling
224
267
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!
226
272
227
273
Lets open our main page and its server page to make a few changes.
228
274
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.
230
277
231
278
```ts
232
-
import { dinos } from'$lib/server/dino.js';
279
+
import { dinos } from"$lib/server/dino.js";
233
280
234
281
exportconst load =async ({ url }) => {
235
282
// We can access the search params by using the `url` provided
236
283
// by SvelteKit
237
284
const queryParams =url.searchParams;
238
285
239
286
// We will use `q` as our search string
240
-
const q =queryParams.get('q');
287
+
const q =queryParams.get("q");
241
288
// We will use `page` to know which page we are on
242
-
const pageParam =queryParams.get('page');
289
+
const pageParam =queryParams.get("page");
243
290
let page =1;
244
291
// We should verify there is a page param, if there is verify it is a number
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!
297
345
298
346
```svelte
299
347
<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
402
450
</style>
403
451
```
404
452
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.
406
455
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.
408
460
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.
410
465
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!
0 commit comments