| | |

Svelte 72 ๐Ÿงก Server-Side Rendering (SSR) Configuration and Pre-rendering Strategies

โฑ๏ธ Estimated read: 7 min | |

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

ExportValueMeaning
ssrtrueRender on server
ssrfalseRender on client (SPA)
prerendertrueGenerate HTML at build time
prerenderfalseGenerate 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

PitfallWhy It HappensFix
Hydration errorServer/Client DOM mismatchAvoid window in components
Build failureDynamic route pre-renderingUse prerender = false
SEO lossSSR set to falseSet ssr = true for public pages
Stale pre-renderBuild didn’t runRe-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

ItemValue
Config Location+page.js or +layout.js
Server Side Renderssr
Build Time Renderprerender
Global StrategyRoot layout
Override StrategyPage-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 = false creates 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.

Was this helpful?


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!