Rendering Modes
Client-Side Rendering (CSR)
The browser downloads JavaScript, executes it, and renders the page:- Full interactivity immediately
- Simpler deployment (static hosting)
- No server required
- Slower initial load
- Poor SEO (content not in initial HTML)
- Blank page until JavaScript executes
Server-Side Rendering (SSR)
The server generates HTML and sends it to the browser:- Fast initial page load
- Better SEO (crawlers see content)
- Works without JavaScript
- No interactivity until JavaScript loads
- Requires server infrastructure
- More complex deployment
Hydration (SSR + CSR)
Combine SSR and CSR for the best of both worlds:1
Svelte components are always hydratable in Svelte 5. The
hydratable compiler option from Svelte 4 has been removed.Using the Render API
Basic Server Rendering
Therender function from svelte/server generates HTML:
Handling <svelte:head>
Content in <svelte:head> is returned separately:
App.svelte
Async Server Rendering
Svelte 5 supports asynchronous server rendering withawait expressions:
Hydration Process
How Hydration Works
During hydration, Svelte:- Walks the server-rendered DOM
- Attaches event listeners
- Initializes reactive state
- Makes the application interactive
Hydration Markers
Svelte 5 uses HTML comments as markers for efficient hydration:Hydration Mismatches
A hydration mismatch occurs when server and client HTML differ:hydration_mismatch warning because the time will be different when rendered on the server vs. when hydrating on the client.
Solutions:
- Use client-only rendering for dynamic values:
- Use
hydratablefor values that should be consistent:
Optimizing Data Fetching
The hydratable API
Avoid fetching data twice (server + client) with hydratable:
1
getUser(), serializes result into HTMLgetUser() callgetUser() normallySerialization
hydratable uses devalue which supports:
- Primitives (string, number, boolean, null, undefined)
- Objects and arrays
Date,Map,Set,RegExpBigInt,URL- Promises (Svelte-specific enhancement)
Content Security Policy (CSP)
hydratable injects a script tag. For CSP compliance, provide a nonce:
SSR Considerations
Browser-Only Code
Some code should only run in the browser:$effect callbacks never run on the server, so you don’t need if (browser) checks inside them.Lifecycle Hooks
OnlyonDestroy runs during SSR:
Invalid HTML Structure
Browsers “repair” invalid HTML, causing hydration mismatches:node_invalid_placement_ssr.
Using SvelteKit
For production applications, use SvelteKit which handles:- Automatic SSR/hydration setup
- Data loading with
loadfunctions - Routing and navigation
- Deployment adapters
- Streaming SSR
- Prerendering
+page.svelte
+page.server.js
Best Practices
1
hydratable for async data - Avoid duplicate fetchingwindow, document availability