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
| Feature | Svelte | React | Vue 3 | Angular |
|---|---|---|---|---|
| Bundle Size | ~3 KB | ~44 KB | ~34 KB | ~167 KB |
| Runtime | None (compiled) | Virtual DOM | Virtual DOM | Zone.js + RxJS |
| Reactivity | Compile-time | Hooks | Proxy-based | RxJS Observables |
| Boilerplate | Minimal | Moderate | Moderate | High |
| Learning Curve | Gentle | Moderate | Gentle | Steep |
| TypeScript | Excellent | Excellent | Excellent | Built-in |
| Animations | Built-in | Third-party | Third-party | Built-in |
| SEO/SSR | SvelteKit | Next.js | Nuxt | Universal |
| Ecosystem | Growing | Largest | Large | Large |
| Best For | Fast, small apps | Large ecosystems | Progressive apps | Enterprise |
Svelte + Mizu vs Standalone Svelte
| Aspect | Svelte + Mizu | Standalone Svelte (SPA) |
|---|---|---|
| Backend | Go (embedded FS) | Separate API server |
| Deployment | Single binary | Frontend + backend separate |
| Development | Mizu proxy + Vite HMR | Vite dev server only |
| API Calls | Same origin (/api/*) | CORS required |
| Build | make build (Go + Svelte) | npm run build only |
| Production | ./bin/server | nginx/CDN + API server |
| Type Safety | Go backend + TS frontend | TS frontend only |
Quick Start
Create a new Svelte project with the CLI:http://localhost:3000 to see your app!
Architecture
Development Mode
Production Mode
Project Structure
Backend Setup
Standard Mizu setup with auto-detection://go:embed all:../../distembeds the compiled Svelte app into the Go binaryfrontend.ModeAutoswitches between dev (proxy) and production (embedded FS)DevServerproxies 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
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 usingexport let.
Events
UsecreateEventDispatcher 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
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 thesvelte package.
- Component created
onMount()- After initial renderbeforeUpdate()- Before each updateafterUpdate()- After each updateonDestroy()- 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:tailwind.config.js:
src/styles/index.css:
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
Troubleshooting
HMR Not Working
Problem: Changes don’t hot-reload. Solution:- Check Vite dev server is running on port 5173
- Verify
vite.config.tsHMR 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
- 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