★ SvelteKit ★
Getting Started with SvelteKit
A quick guide to building full-stack apps with SvelteKit and TypeScript. Learn about routing, server-side rendering, and API routes.
Introduction
SvelteKit is a full-stack framework built on top of Svelte that makes it easy to build fast, modern web applications. It handles routing, server-side rendering, API routes, and much more out of the box.
Setting Up a Project
To create a new SvelteKit project, run:
npx sv create my-app
cd my-app
bun install
bun dev
File-Based Routing
SvelteKit uses a file-based routing system. Every +page.svelte file inside src/routes becomes a page:
src/routes/
+page.svelte → /
about/
+page.svelte → /about
blog/
+page.svelte → /blog
[slug]/
+page.svelte → /blog/:slug
Server-Side Rendering
SvelteKit renders pages on the server by default. You can load data server-side using +page.server.ts:
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const post = await fetchPost(params.slug);
return { post };
};
API Routes
Create API endpoints using +server.ts files:
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ url }) => {
const data = await fetchSomeData();
return json(data);
};
Conclusion
SvelteKit is one of the most enjoyable full-stack frameworks available today. Its tight integration with Svelte's reactivity model, combined with powerful server-side features, makes it an excellent choice for building modern web applications.