Svelte 72 ๐งก Server-Side Rendering (SSR) Configuration and Pre-rendering Strategies
SvelteKit is a hybrid framework that enables a flexible rendering strategy for every page of your application. While Server-Side Rendering (SSR) is the default, providing fast First Contentful Paint (FCP) and excellent SEO, SvelteKit allows you to disable it to create a fully client-side rendered Single-Page Application (SPA). Additionally, SvelteKit supports pre-rendering, which calculates the HTML at build time, essentially turning dynamic routes into static files for maximum performance.
The power of SvelteKit lies in its ability to mix these strategies per-route. You can pre-render your marketing pages for high-speed edge distribution, use SSR for authenticated dashboard pages that need live data, and disable SSR for private, heavy dashboard modules where user-specific state makes caching difficult. This chapter explores how to configure these rendering behaviors via the +page.js and +layout.js configuration exports.
Key point: Rendering behavior is controlled via the ssr and prerender exports in +page.js or +layout.js; SSR ensures dynamic HTML on request, while pre-rendering produces static HTML at build time, allowing you to optimize each route for its specific data needs and latency requirements.
Why exists
The Time to First Byte (TTFB) optimization. Generating HTML on the server for every request takes time. Pre-rendering allows you to shift this work to build time, delivering files from a CDN near the user with zero server-side computation.
The SEO and social preview requirement. Search engines and social media scrapers perform best when they receive fully formed HTML. SSR ensures that regardless of your data, the crawler sees the final content immediately, bypassing the need for heavy client-side execution.
The client-side complexity trade-off. Sometimes, complex components have browser-specific dependencies that crash when run on the server (e.g., window or document). Disabling SSR is the standard escape hatch to allow these components to mount directly in the browser.
The resource efficiency problem. Rendering a complex page on the server requires CPU power for every single hit. By selectively pre-rendering or disabling SSR for non-critical pages, you can significantly reduce server load and infrastructure costs.
The hydration consistency challenge. SSR and client-side hydration must produce identical DOM structures. When they don’t, hydration mismatches occur. Selective SSR configuration allows developers to isolate problematic routes where the DOM might be too dynamic for reliable SSR.
a. SSR configuration (export const ssr)
The ssr export in your +page.js or +layout.js dictates whether SvelteKit renders the page on the server. If ssr = false, the server serves a blank shell and the page mounts on the client.
// +page.js
export const ssr = false; // Disable SSR for this route
Disabling SSR is useful for dashboards or internal tools where SEO is irrelevant and the application state is entirely user-specific. When ssr is false, the load function still runs on the client, but the initial HTML delivered to the browser will be a static “loading” shell, and the JavaScript will take over to render the component tree.
b. Pre-rendering (export const prerender)
The prerender export determines if the page should be generated at build time. When prerender = true, SvelteKit executes the load function during the build phase and writes a static .html file.
// +page.js
export const prerender = true; // Pre-render at build time
Pre-rendering is the ultimate optimization for static content. It essentially turns a dynamic SvelteKit page into a plain HTML file. prerender can also be set to 'auto', which is the default for most routes; it pre-renders if the page doesn’t depend on request-specific data (like cookies or event.url parameters).
c. The hybrid approach
SvelteKit allows you to define rendering behavior at the +layout.js level, which cascades down to all child routes. You can override this at any sub-page level, creating a “hybrid” application where parts are statically generated, parts are server-rendered, and parts are client-rendered.
// +layout.js (Apply to all child pages)
export const ssr = true;
export const prerender = false;
// +page.js (Override for a specific page)
export const ssr = false; // Only this specific page is client-side
This hierarchy gives you fine-grained control. You can set a global policy in your root layout and selectively optimize routes that need high performance (pre-rendering) or routes that are incompatible with server-side logic (disabling SSR).
Complete Example Session
// ============================================
// PART 1: GLOBAL LAYOUT DEFAULTS
// ============================================
// src/routes/+layout.js
export const ssr = true;
export const prerender = false;
// ============================================
// PART 2: STATIC MARKETING PAGE
// ============================================
// src/routes/about/+page.js
export const prerender = true;
// ============================================
// PART 3: CLIENT-ONLY DASHBOARD
// ============================================
// src/routes/dashboard/+page.js
export const ssr = false;
// ============================================
// PART 4: DYNAMIC SERVER-RENDERED PAGE
// ============================================
// src/routes/profile/[id]/+page.js
export const ssr = true;
export const prerender = false; // Must be false for dynamic routes
These configurations demonstrate the flexibility of the hybrid model, where the build process and server behavior are tuned to the needs of each route.
Quick Reference
Configuration Settings
| Export | Value | Meaning |
|---|---|---|
ssr | true | Render on server |
ssr | false | Render on client (SPA) |
prerender | true | Generate HTML at build time |
prerender | false | Generate HTML on server request |
prerender | 'auto' | Prerender if data allows |
Best Practices
โ Do This:
// Prerender static content
export const prerender = true; // โ
// Disable SSR for user-only dashboards
export const ssr = false; // โ
// Rely on 'auto' for routes without request data
export const prerender = 'auto'; // โ
โ Don’t Do This:
// Don't set prerender = true on dynamic routes
// (build will fail or be incomplete) // โ
// Don't disable SSR for public, SEO-heavy pages
export const ssr = false; // โ
// Don't mix incompatible render modes in layouts
// (can cause confusing behavior) // โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Hydration error | Server/Client DOM mismatch | Avoid window in components |
| Build failure | Dynamic route pre-rendering | Use prerender = false |
| SEO loss | SSR set to false | Set ssr = true for public pages |
| Stale pre-render | Build didn’t run | Re-trigger build deployment |
Real-World Examples
1. Static Landing Page
export const prerender = true;
2. User Settings (CSR)
export const ssr = false;
3. Dynamic Product Page (SSR)
export const ssr = true;
export const prerender = false;
4. Blog Post (Prerendered)
export const prerender = true;
5. Search Results (Dynamic SSR)
export const ssr = true;
6. API Proxy Route (No SSR)
export const ssr = false;
7. Global Layout Settings
export const ssr = true;
8. Data-dependent Prerender
export const prerender = 'auto';
9. Admin Internal Tools
export const ssr = false;
10. Help Center Articles
export const prerender = true;
Visual
SSR vs Prerender Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SSR: Client Request โโโบ Server Render โโโบ Send HTML โ
โ โ
โ PRERENDER: Build Time โโโบ Generate HTML โโโบ Store file โ
โ (Delivered via CDN instantly on request) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Hybrid Hierarchy
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ROOT LAYOUT (ssr: true) โ
โ โ โ
โ โโโบ ABOUT PAGE (prerender: true) โ
โ โ โ
โ โโโบ DASHBOARD (ssr: false, overrides layout) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Client-Side Rendering Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Browser Request โโโบ Empty HTML Shell โโโบ JS Mounts โ
โ โ
โ (No content until JS executes in the browser) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Config Location | +page.js or +layout.js |
| Server Side Render | ssr |
| Build Time Render | prerender |
| Global Strategy | Root layout |
| Override Strategy | Page-level export |
Key takeaways:
- SSR is for dynamic, SEO-sensitive content. It creates fresh HTML on every request but incurs a performance cost on the server.
- Pre-rendering is for static, high-speed delivery. By calculating HTML at build time, you achieve maximum performance and zero server load for those routes.
ssr = falsecreates a client-side SPA. Use this for routes that don’t need SEO or rely on heavy browser-only dependencies.- Configuration is hierarchical. Layout settings act as defaults, but individual pages can override these behaviors to suit specific data or performance requirements.
- Choose the right tool for the route. Use the hybrid approach to balance SEO, performance, and server costs across your entire application.
Remember: SvelteKit provides a robust toolbox to manage rendering, but the responsibility to choose the right strategy per-route rests with you. Prioritize pre-rendering for content that rarely changes, SSR for pages that require SEO or live data, and client-side rendering only when SEO is unnecessary and your logic depends heavily on the browser environment.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!