Skip to main content
SvelteKit is the official full-stack framework for Svelte, providing file-based routing, server-side rendering, and more. When using SvelteKit with Mizu, you’ll typically use SvelteKit’s static adapter to generate a static site that Mizu serves.

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

SvelteKit with Mizu vs Standalone SvelteKit

Quick Start

Create a new SvelteKit project:
Visit 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
Mizu provides:
  • 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

Key settings:
  • adapter-static: Generates static HTML/CSS/JS
  • pages/assets: Output directory (build instead of dist)
  • fallback: 'index.html': SPA mode for client-side routing
  • alias: Custom path aliases (in addition to default $lib)

frontend/vite.config.ts

Backend Configuration

app/server/app.go

Important: SvelteKit outputs to build/ by default, not dist/.

API Routes Example

File-Based Routing

SvelteKit uses file-based routing where the structure of your src/routes directory defines your app’s routes.

Basic Routes

Dynamic Routes

Use [param] for dynamic segments:
Access params in your page:

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 +layout@.svelte:
Or skip all layouts with +layout@[id].svelte:

Data Loading

SvelteKit’s load 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:
SvelteKit provides programmatic navigation and lifecycle hooks.

Using goto

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:
Options:
  • prerender: Generate static HTML at build time
  • ssr: Server-side rendering (not applicable with static adapter)
  • csr: Client-side rendering
  • trailingSlash: 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

Important: 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

Or use the Makefile:
The Makefile typically runs both servers concurrently.

Building for Production

This:
  1. Builds SvelteKit (npm run build → creates build/)
  2. Builds Go binary with embedded build/
Run:

Troubleshooting

Build Directory Not Found

Problem: Go can’t find the build/ 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: Verify vite.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 ✅
Doesn’t work:
  • 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) ❌
Workarounds:
  • 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
Choose vanilla Svelte + Mizu if:
  • You prefer manual routing (simpler)
  • You don’t need nested layouts
  • Smaller learning curve is important
  • You want minimal framework overhead
Choose full SvelteKit (without Mizu) if:
  • 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.ts files

Next Steps

Svelte Guide

Learn vanilla Svelte

SvelteKit Docs

Official SvelteKit documentation

Next.js

Similar approach with React

Deployment

Build and deploy your app