Page Content Here
This represents the <slot>
where page content renders.
SvelteKit uses a file-based routing system where files in the src/routes
directory automatically become routes in your application.
src/routes/ ├── +page.svelte # Home page (/) ├── about/ │ └── +page.svelte # About page (/about) ├── blog/ │ ├── +page.svelte # Blog index (/blog) │ └── [slug]/ # Dynamic route │ └── +page.svelte # Individual blog post (/blog/:slug)
Each +page.svelte
file represents a page component that will be rendered for that route.
SvelteKit supports dynamic route parameters using square brackets in the folder name.
/users/[id]
matches /users/123
, /users/456
, etc./products/[category]/[id]
matches /products/books/svelte-book
/[...catchAll]
matches any route and captures the rest of the pathAccess route parameters in the +page.svelte
component via the $page.params
store:
<script> import { page } from '$app/stores'; </script> <h1>User ID: {$page.params.id}</h1>
Control how pages are rendered using additional files:
SvelteKit provides a powerful data loading system through load
functions.
// +page.ts export async function load({ params, fetch }) { const response = await fetch(`/api/posts/${params.slug}`); const post = await response.json(); return { post }; }
// +page.server.ts export async function load({ params, locals }) { // Access server-only resources like databases const post = await db.getPost(params.slug); // Access server-only data like session info const user = locals.user; return { post, user }; }
In your +page.svelte
component, you can access this data as props:
<script> export let data; // Contains the data returned by load functions </script> <h1>{data.post.title}</h1> <p>Written by {data.user.name}</p>
Layouts in SvelteKit allow you to share UI elements across multiple pages.
src/routes/ ├── +layout.svelte # Root layout (applies to all routes) ├── +page.svelte # Home page ├── dashboard/ │ ├── +layout.svelte # Dashboard layout (nested) │ ├── +page.svelte # Dashboard index │ └── settings/ │ └── +page.svelte # Settings page (gets both layouts)
<script> import Header from '$lib/Header.svelte'; import Footer from '$lib/Footer.svelte'; // You can access layout data export let data; </script> <Header user={data.user} /> <main> <slot></slot> <!-- Page content goes here --> </main> <Footer />
This represents the <slot>
where page content renders.
SvelteKit provides several rendering options to optimize for performance and SEO.
// SSR (default) export const ssr = true; // CSR (disable SSR) export const ssr = false; // SSG (prerender at build time) export const prerender = true; // For dynamic routes that should be prerendered export const entries = () => [ { slug: 'hello-world' }, { slug: 'getting-started' } ];