Skip to main content
Svelte is a radical new approach to building user interfaces. Instead of using a runtime library, Svelte is a compiler that converts your components into efficient JavaScript at build time.

Why Svelte?

Svelte takes a fundamentally different approach from React and Vue. Instead of shipping a framework to the browser, Svelte compiles your components into highly optimized vanilla JavaScript at build time. This results in incredibly small bundle sizes and blazing-fast performance. Key strengths:
  • No virtual DOM: Direct DOM manipulation, faster updates
  • Truly reactive: Assignments trigger updates automatically
  • Compile-time framework: Most work done at build time, not runtime
  • Minimal boilerplate: Less code to write than React/Vue
  • Built-in animations: Transitions and animations included
  • Smallest bundles: Often 70-90% smaller than React equivalents
  • Great DX: Intuitive syntax, excellent error messages

Svelte vs Other Frameworks

Svelte + Mizu vs Standalone Svelte

Quick Start

Create a new Svelte project with the CLI:
Visit http://localhost:3000 to see your app!

Architecture

Development Mode

Production Mode

Project Structure

Backend Setup

Standard Mizu setup with auto-detection:
How it works:
  • //go:embed all:../../dist embeds the compiled Svelte app into the Go binary
  • frontend.ModeAuto switches between dev (proxy) and production (embedded FS)
  • DevServer proxies to Vite during development
  • Svelte compiles to vanilla JS, so bundle is tiny

API Routes Example

Frontend Setup

frontend/src/main.ts

frontend/src/App.svelte

frontend/src/components/Layout.svelte

frontend/src/pages/Home.svelte

Svelte Reactivity

Svelte’s reactivity is built into the language itself. Assignments are reactive.

Basic Reactivity

How it works: Svelte’s compiler analyzes your code and inserts update calls wherever variables are reassigned.

Reactive Declarations ($:)

Use $: to create reactive statements that automatically re-run when dependencies change.

Reactive Dependencies

Svelte automatically tracks dependencies:

Array and Object Reactivity

Important: Assignments trigger updates, but mutating methods (push, pop, etc.) don’t.

Component Communication

Props

Pass data from parent to child using export let.

Events

Use createEventDispatcher to communicate from child to parent.

Event Forwarding

Forward events from child components:

Context API

Share data between components without prop drilling.

Slots

Pass markup from parent to child.

Slot Props

Pass data from child to parent through slots:

Svelte Stores

Stores provide a way to share state across components.

Writable Stores

Readable Stores

For read-only values (e.g., time, mouse position):

Derived Stores

Create stores derived from other stores:

Custom Stores

Create stores with custom methods:

Store Contracts

Type-safe store subscription:

Routing

Svelte doesn’t include routing by default. Here are your options:

Option 1: Simple Routing (Manual)

We’ve seen this in the App.svelte example above.

Option 2: svelte-spa-router

Basic usage:

Dynamic Routes with Params

Programmatic Navigation

Route Guards

Option 3: SvelteKit

For full-featured routing with SSR, use SvelteKit:
  • File-based routing
  • Server-side rendering
  • Static site generation
  • API routes
  • Advanced features

Lifecycle Hooks

Svelte provides lifecycle functions from the svelte package.
Lifecycle order:
  1. Component created
  2. onMount() - After initial render
  3. beforeUpdate() - Before each update
  4. afterUpdate() - After each update
  5. onDestroy() - Before removal

Bindings

Svelte provides powerful two-way bindings.

Input Bindings

Element Bindings (bind:this)

Get a reference to a DOM element:

Component Bindings

Bind to component props:

Dimensions and Scroll

Logic Blocks

{#if} blocks

{#each} blocks

{#await} blocks

Handle promises declaratively:

{#key} blocks

Re-render when value changes:

Special Elements

<svelte:self>

Recursively render a component:

<svelte:component>

Dynamically render different components:

<svelte:window>

Bind to window events and properties:

<svelte:body>

Bind to document.body events:

<svelte:head>

Add elements to document.head:

Transitions and Animations

Svelte has built-in transitions and animations.

Built-in Transitions

Separate In/Out Transitions

Custom Transitions

Transition Events

Animations (flip)

Animate element positions in lists:

Motion (Tweened and Spring)

Animate values over time.

Tweened

Spring

Physics-based animation:

Actions

Reusable DOM manipulation functions.

Basic Action

Action with Parameters

Class and Style Directives

Class Directive

Style Directive

Vite Configuration

frontend/vite.config.ts

frontend/svelte.config.js

Performance Optimization

Immutable Data

Tell Svelte your data is immutable for performance:

Component Options

Styling

Component Styles (Scoped)

Styles are scoped by default:

Global Styles

Use :global() modifier:

Tailwind CSS

Install Tailwind:
Configure tailwind.config.js:
Add to src/styles/index.css:
Use in components:

Complete Real-World Example: Task Manager

Let’s build a complete task management app with Svelte stores and routing.

Backend (Go)

Frontend - Store

Frontend - Components

Building for Production

Produces a single binary with embedded frontend. Run in production:

Troubleshooting

HMR Not Working

Problem: Changes don’t hot-reload. Solution:
  • Check Vite dev server is running on port 5173
  • Verify vite.config.ts HMR config:

Reactivity Not Working

Problem: Component doesn’t update when data changes. Solution: Use assignments, not mutations:

Store Not Updating

Problem: Store value changes but component doesn’t update. Solution: Make sure you’re using the $ prefix:

TypeScript Errors with Stores

Problem: Type errors when using stores. Solution: Properly type your stores:

Component Props Not Updating

Problem: Props don’t reflect parent changes. Solution: Make sure you’re not reassigning props directly:

When to Choose Svelte

Choose Svelte if:
  • You want the smallest possible bundle size
  • You value minimal boilerplate and clean syntax
  • You need excellent performance out of the box
  • You’re building a new project (not integrating with existing React/Vue)
  • You want built-in transitions and animations
  • You prefer compile-time over runtime approaches
Consider alternatives if:
  • React: You need the largest ecosystem, mature libraries, or are integrating with existing React code
  • Vue: You want a larger community, more third-party components, or progressive adoption
  • SvelteKit: You need SSR, static generation, or file-based routing (Svelte’s official framework)
  • Angular: You need a full enterprise framework with everything included

Next Steps

SvelteKit

Full-featured Svelte framework with routing and SSR

Svelte Tutorial

Official interactive Svelte tutorial

Svelte Examples

Official Svelte code examples

Deployment

Build and deploy your app