Svelte logoSvelte INTERMEDIATE

SvelteKit

Full-stack framework for building web applications with Svelte

10 min read
sveltekitsveltessrssgroutingapifullstackvite

Setup & Project Structure

Getting started with SvelteKit

Installation & Setup

Creating a new SvelteKit project

bash
🚀 Built on Vite for lightning-fast HMR
📦 TypeScript support out of the box
🔧 Multiple adapter options for deployment
⚡ File-based routing with special naming conventions

Project Structure

SvelteKit file and folder organization

javascript
📁 File-based routing in src/routes
🔗 $lib alias for easy imports
🎯 Special +page/+layout naming convention
⚙️ Separate client and server hooks

Routing

File-based routing and navigation

Basic Routing

File-based routing patterns

javascript
📁 File-based routing with folders and +page.svelte
🔗 Automatic code splitting per route
🎯 Dynamic routes with [param] syntax
⚡ Built-in prefetching with data attributes

Layouts

Nested layouts and layout groups

javascript
🎨 Nested layouts inherit from parent layouts
📦 Layout groups with (folder) for organization
🔄 Layout resets with @ syntax
⚡ Layouts can have their own load functions

Data Loading

Loading data in pages and layouts

Universal Load Functions

Load functions that run on both client and server

javascript
🔄 Runs on both server and client
🎯 Access to special SvelteKit fetch
📦 Can set response headers and dependencies
⚡ Automatic type safety with TypeScript

Server Load Functions

Server-only load functions with direct database access

javascript
🔒 Runs only on server with access to secrets
🗄️ Direct database access without API calls
🍪 Full access to cookies and headers
⚡ Can stream promises for faster initial render

Form Actions

Progressive form handling and mutations

Basic Actions

Server-side form handling

javascript
📝 Progressive enhancement - works without JavaScript
🔒 Server-side validation and processing
🍪 Direct cookie access for authentication
⚡ use:enhance for optimistic UI updates

Enhanced Forms

Client-side progressive enhancement

javascript
⚡ use:enhance for progressive enhancement
🎯 Optimistic UI updates without waiting
📝 Access to form data before submission
🔄 Custom result handling and validation

API Routes

Creating API endpoints

Server Endpoints

Creating RESTful API endpoints

javascript
🔗 RESTful endpoints with +server.js files
🔒 Access to cookies and authentication
📊 Full HTTP method support
⚡ Automatic error handling and status codes

Streaming & SSE

Server-sent events and streaming responses

javascript
📡 Server-sent events for real-time updates
🌊 Streaming responses for large data
📦 NDJSON for streaming JSON data
⚡ Efficient memory usage for large files

Hooks

Server and client hooks for request handling

Server Hooks

Server-side request/response handling

javascript
🔒 Authentication and authorization
🛡️ Security headers and CORS
📊 Request logging and monitoring
⚡ Transform responses before sending

Client Hooks

Client-side navigation and error handling

javascript
❌ Client-side error handling and reporting
📊 Performance monitoring
💾 Service worker for offline support
🔄 Handle navigation events

Authentication

Implementing authentication patterns

Session-based Auth

Cookie-based authentication

javascript
🍪 Secure HTTP-only cookies for sessions
🔒 Server-side session validation
⚡ Automatic session extension
🛡️ Protected routes in hooks

OAuth Integration

Third-party authentication providers

javascript
🔑 OAuth with GitHub, Google, etc.
✉️ Magic link authentication
🛡️ CSRF protection with state parameter
🔄 Account linking for existing users

Deployment

Building and deploying SvelteKit apps

Adapters

Deployment adapters for different platforms

javascript
🚀 Multiple adapter options for different platforms
📦 Static site generation with adapter-static
☁️ Serverless deployment with Vercel/Netlify
🐳 Docker support with adapter-node

Performance Optimization

Optimizing SvelteKit apps for production

javascript
⚡ Prerendering for static pages
📦 Code splitting with dynamic imports
🖼️ Image lazy loading and optimization
📊 Core Web Vitals monitoring

More Svelte Cheat Sheets