Why SvelteKit?
SvelteKit is Svelte’s answer to Next.js (React) and Nuxt (Vue). It provides a batteries-included framework with file-based routing, layouts, data loading, and more—all built on top of Svelte’s compiler magic. Key benefits:- File-based routing: Routes are automatically created from your file structure
- Nested layouts: Share UI and logic across routes
- Code splitting: Automatic code splitting per route
- Data loading: Load data before rendering pages
- TypeScript-first: Excellent TypeScript support out of the box
- Build optimizations: Smart bundling and preloading
- Developer experience: Hot Module Replacement, error overlays, and more
SvelteKit vs Other Meta-Frameworks
| Feature | SvelteKit | Next.js | Nuxt | Remix |
|---|---|---|---|---|
| Framework | Svelte | React | Vue | React |
| Routing | File-based | File-based | File-based | File-based |
| SSR | Yes | Yes | Yes | Yes |
| SSG | Yes | Yes | Yes | Limited |
| Bundle Size | ~30 KB | ~85 KB | ~50 KB | ~80 KB |
| Learning Curve | Gentle | Moderate | Gentle | Moderate |
| Data Loading | load() | getServerSideProps | useAsyncData | loader() |
| Forms | Actions | API routes | API routes | Actions |
| TypeScript | Excellent | Excellent | Excellent | Excellent |
| Image Optimization | Manual | Built-in | Built-in | Manual |
SvelteKit with Mizu vs Standalone SvelteKit
| Aspect | SvelteKit + Mizu | Standalone SvelteKit |
|---|---|---|
| Backend | Go (Mizu) | Node.js (adapter) |
| API Routes | Go handlers | SvelteKit endpoints |
| Database | Go libs (pgx, sqlc) | Node libs (Prisma, Drizzle) |
| Deployment | Single binary | Adapter-specific |
| Rendering | Static (SPA mode) | SSR + SSG |
| Build Output | build/ → embedded | Depends on adapter |
| Best For | Go backends | Full Node.js stack |
Quick Start
Create a new SvelteKit project:http://localhost:3000 to see your app!
Architecture
Development Mode
Production Mode
Why SvelteKit with Mizu?
SvelteKit can run as a full-stack framework with its own server, but with Mizu you use it as a static site generator: SvelteKit provides:- File-based routing (
src/routes/+page.svelte) - Layouts and nested routes
- Data loading (
+page.ts) - Static site generation
- Code splitting per route
- Built-in transitions
- Go-based API backend
- Database access with Go libraries
- Type-safe business logic
- Authentication and authorization
- Easy deployment (single binary)
- Performance (Go runtime)
Project Structure
SvelteKit Configuration
frontend/svelte.config.js
adapter-static: Generates static HTML/CSS/JSpages/assets: Output directory (buildinstead ofdist)fallback: 'index.html': SPA mode for client-side routingalias: Custom path aliases (in addition to default$lib)
frontend/vite.config.ts
Backend Configuration
app/server/app.go
build/ by default, not dist/.
API Routes Example
File-Based Routing
SvelteKit uses file-based routing where the structure of yoursrc/routes directory defines your app’s routes.
Basic Routes
Dynamic Routes
Use[param] for dynamic segments:
Optional Parameters
Use[[param]] for optional parameters:
Rest Parameters
Use[...rest] to match multiple segments:
Route Groups
Group routes without affecting the URL with(group):
Layouts
Layouts wrap pages and can be nested. They persist across route changes.Root Layout
Nested Layouts
Layout Data
Load data in layouts:Resetting Layouts
Break out of parent layouts with[email protected]:
+layout@[id].svelte:
Data Loading
SvelteKit’sload functions fetch data before rendering pages.
Basic Load Function
Load Function with Params
Parallel Loading
Load multiple resources in parallel:Dependent Loads
When data depends on other data:Streaming with Promises
Return promises to stream data:Navigation
SvelteKit provides programmatic navigation and lifecycle hooks.Using goto
Navigation Lifecycle
Prefetching
SvelteKit can prefetch data before navigation:Disabling Client-Side Routing
For external links or special cases:Page Options
Configure page behavior with+page.ts:
prerender: Generate static HTML at build timessr: Server-side rendering (not applicable with static adapter)csr: Client-side renderingtrailingSlash: URL trailing slash handling
SvelteKit Stores
SvelteKit provides built-in stores for navigation state.$app/stores
Forms and Actions
In static mode, SvelteKit form actions don’t work. Use client-side forms instead.Client-Side Form Handling
Error Handling
Handle errors with+error.svelte pages.
Error Page
Throwing Errors in Load Functions
Environment Variables
Access environment variables safely.Public Variables
PUBLIC_* variables are embedded in client-side code.
Private Variables (Build-time only)
Complete Real-World Example: Blog
Let’s build a complete blog with routing, layouts, and data loading.Backend (Go)
Frontend Structure
Root Layout
Blog List Page
Blog Post Page
Development Workflow
Start Development
Building for Production
- Builds SvelteKit (
npm run build→ createsbuild/) - Builds Go binary with embedded
build/
Troubleshooting
Build Directory Not Found
Problem: Go can’t find thebuild/ directory.
Solution: Make sure you’ve run npm run build in the frontend/ directory first:
HMR Not Working
Problem: Changes don’t hot-reload. Solution: Verifyvite.config.ts:
404 on Page Refresh
Problem: Refreshing on/blog gives 404 in production.
Solution: Ensure fallback: 'index.html' is set in svelte.config.js:
Data Not Loading
Problem:load functions don’t fetch data.
Solution: Make sure you’re using fetch from the load context:
Route Params Not Working
Problem:$page.params is empty.
Solution: Make sure you’re accessing params in a component under the dynamic route:
Limitations with Static Adapter
When using the static adapter with Mizu: Works:- Client-side routing ✅
- Data loading from APIs ✅
- Forms (client-side) ✅
- Stores and state ✅
- Layouts and nested routes ✅
- Dynamic routes with params ✅
- Prefetching ✅
- Server-side rendering (SSR) ❌
- SvelteKit API routes (
+server.ts) ❌ - Server-only load functions (
+page.server.ts) ❌ - Form actions (
+page.server.ts) ❌ - Hooks (
hooks.server.ts) ❌
- Use Mizu API routes instead of SvelteKit endpoints
- Use
+page.ts(client) instead of+page.server.ts(server) - Handle forms client-side with
fetch()
When to Choose SvelteKit
Choose SvelteKit + Mizu if:- You want file-based routing with Svelte
- You like nested layouts
- You want automatic code splitting per route
- You’re comfortable with Go for your backend
- You prefer static site generation
- You prefer manual routing (simpler)
- You don’t need nested layouts
- Smaller learning curve is important
- You want minimal framework overhead
- You need SSR (server-side rendering)
- You want Node.js for your backend
- You need SvelteKit API routes and actions
- You want to use
+page.server.tsfiles