-
- SvelteKit
- Server-side and client-side rendering
- Comes with a routing system
- Provides a backend API
- Code Splitting
- built on top of Vite
- development server with Hot Module Replacement (live updates)
- Course files https://github.com/iamshaunjp/sveltekit-tutorial
- Sveltekit docs https://kit.svelte.dev/docs/introduction
- SvelteKit
-
2 - Setting up a SvelteKit App
npm init svelte@next myApp
-
- filesystem-based router
src/routes
creates rootsrc/routes/about
creates/about
- https://kit.svelte.dev/docs/routing
- filesystem-based router
-
$lib
=src/lib/
- I had to edit
.svelte-kit/tsconfig.json
{ "compilerOptions": { "paths": { "$lib": [ "../src/lib" ], "$lib/*": [ "../src/lib/*" ] },
- see also: sveltejs/language-tools#1459
/src/lib/component.svelte
works fine, no need for+page.svelte
naming
- I had to edit
-
- use
+layout.svelte
for layouts - components are rendered in
<slot> </slot>
- use
-
https://kit.svelte.dev/docs/advanced-routing#advanced-layouts-group
+layout.svelte
files inherit from layout files up in the file tree- use (group) for layout structure
- (App), (Guides) doesn't show in the url
routes/+layout.svelte
contains<slot> </slot>
(guides)+layout@.svelte
refers to the root layout and thus overrides(App)/+layout.svelte
├── routes ├── (App) │ ├── about │ │ └── +page.svelte │ ├── (guides) │ │ ├── guides │ │ │ └── +page.svelte │ │ └── +layout@.svelte ┄┐ │ ├── +layout.svelte ┊ │ └── +page.svelte ┊ └── +layout.svelte <┄┄┄┄┄┄┄┄┄┘
-
- https://kit.svelte.dev/docs/load
A
+page.svelte
file can have a sibling+page.js
that exports aload
function, the return value of which is available to the page via thedata
prop:
├── guides │ ├── +page.js │ └── +page.svelte └── +layout@.svelte
- https://kit.svelte.dev/docs/load
-
9 - Client-side vs Server-side Code
- on initial request code is run on the server, and some DOM objects aren't available.
-
id
get's it's own folder├── guides │ ├── [id] │ │ ├── +page.js │ │ └── +page.svelte │ ├── +page.js │ └── +page.svelte └── +layout.svelte
-
- file
+page.js
import { error } from "@sveltejs/kit"; throw error(420, "Could not fetch the guides"); // import { redirect } from "@sveltejs/kit"; throw redirect(302, "/guides");
- file
-
- Prefetching data is enabled by default, no code to add.
- Test: Firefox ->
CTRL+SHIFT+I
-> Networking -> hover over link
-
- < https://kit.svelte.dev/docs/errors >
- add
+error.svelte
-
- https://vercel.com/login
- use github to register
-
- https://learn.svelte.dev/tutorial/get-handlers
- add
+server.json
export function GET()