SvelteKit 13 🧡 Rendering Modes (SSR, SSG & SPA) — Configuring ssr, csr, prerender, trailingSlash, and hybrid per-route rendering
SvelteKit provides a highly flexible rendering architecture that allows you to control how each page is delivered to the user. By default, SvelteKit uses Server-Side Rendering (SSR) to generate the initial HTML on the server, followed by Client-Side Hydration (CSR) to make the page interactive. However, this is not a “one size fits all” configuration. Depending on your needs—whether it’s high-performance static delivery, purely dynamic client apps, or a mix of both—SvelteKit allows you to toggle these behaviors at the page, layout, or global level.
Understanding rendering modes is critical for performance tuning and infrastructure planning. You can choose to prerender pages at build time to save server costs, disable SSR for pages that rely heavily on browser-only APIs, or create “hybrid” applications where some routes are static and others are dynamically rendered on demand.
This chapter covers the configuration options that define your application’s delivery model. We will explore how to set these policies via the +page.ts and +layout.ts load files and how to manage the nuances of trailing slashes and routing behavior in a static vs. dynamic environment.
Key point: SvelteKit’s rendering modes are controlled by exported booleans (ssr, csr, prerender) in your route files; these controls allow you to shift the burden of page generation between build-time (SSG), server-runtime (SSR), and client-runtime (SPA), enabling a tailored performance strategy for every route.
Why rendering modes exist
The SEO vs. Speed trade-off. SSR provides excellent SEO and fast initial content paint but increases server load. SPA mode (CSR only) is fast to navigate but relies on the client to fetch all data, which can hurt indexing. Modes allow you to pick the right tool for the job.
The infrastructure cost problem. Prerendering (SSG) allows your entire site to be hosted on a global CDN, completely eliminating server costs for those pages. Rendering modes provide the switch to move routes to cheaper infrastructure.
The browser API compatibility issue. Some code paths or libraries rely on window or document objects that simply don’t exist on the server. Disabling SSR for these specific routes prevents server-side crashes during the build or request process.
The data freshness requirement. Some pages need to reflect real-time data (e.g., a stock ticker) and cannot be cached. Others are essentially static content (e.g., an “About Us” page). Hybrid rendering allows you to treat these differently within the same project.
The consistency challenge. Handling of URL slashes and route resolution can differ between web servers (Nginx vs. Vercel vs. static files). Configuration options like trailingSlash ensure your routing behaves identically regardless of the hosting provider.
a. SSR, CSR, and Prerendering
These three variables are the fundamental levers for rendering. They are exported from +page.js or +layout.js (or their .server equivalents).
// +page.ts
export const ssr = true; // Render HTML on the server
export const csr = true; // Hydrate with JS on the client
export const prerender = true; // Generate HTML at build time
ssr = false: The server sends an empty shell, and the client fetches the data and renders the HTML. This is the “SPA” mode.csr = false: The server renders the HTML, but no Svelte JavaScript is sent to the client. The page is static and non-interactive.prerender = true: The route is generated once at build time. This is “SSG.” If a route is dynamic (e.g.,[id]), you must export aentriesfunction to tell SvelteKit which IDs to build.
b. Trailing Slashes
The trailingSlash option determines how URLs are resolved. This is vital for static hosting providers, which often map example.com/about to a file and example.com/about/ to a folder.
// svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
trailingSlash: 'always' // or 'never', 'ignore'
}
};
never: URLs have no trailing slash (the default).always: URLs always have a trailing slash (e.g.,/about/).ignore: SvelteKit does not force a slash style, which can lead to duplicate indexing issues if your server doesn’t handle redirects.
c. Hybrid per-route rendering
The power of SvelteKit lies in “Hybrid Rendering.” You might have a marketing landing page that is fully prerendered for performance, an authenticated dashboard that is SSR-only to ensure security, and a complex editor that is SPA-only (SSR disabled) because it requires heavy browser integration.
// src/routes/dashboard/+page.ts
export const ssr = true;
export const csr = true; // Dashboard needs JS
// src/routes/about/+page.ts
export const prerender = true; // Static marketing
You can define these settings at the +layout.ts level to apply a rendering policy to an entire section of your site, then override it in specific +page.ts files if necessary.
Complete Example Session
// ============================================
// PART 1: GLOBAL CONFIG
// ============================================
// svelte.config.js
export default { kit: { trailingSlash: 'never' } };
// ============================================
// PART 2: STATIC LANDING PAGE
// ============================================
// src/routes/about/+page.ts
export const prerender = true;
// ============================================
// PART 3: DYNAMIC APP (SPA)
// ============================================
// src/routes/app/+page.ts
export const ssr = false; // No server rendering
export const csr = true; // Client takes over
// ============================================
// PART 4: SECURE SSR ROUTE
// ============================================
// src/routes/admin/+page.ts
export const ssr = true; // Securely rendered HTML
export const csr = true; // Interactive dashboard
This sequence creates a balanced architecture: marketing pages load fast via CDN, the app shell loads in the browser, and admin pages are protected by server-side logic.
Quick Reference
Rendering Levers
| Variable | true | false |
|---|---|---|
ssr | Render on server | Render in browser |
csr | Hydrate components | Static HTML only |
prerender | Build-time HTML | Request-time HTML |
Trailing Slash Styles
| Setting | URL Style |
|---|---|
always | /path/ |
never | /path |
ignore | Depends on host |
Best Practices
✅ Do This:
// Prerender static pages
export const prerender = true; // ✅
// SPA for browser-only tools
export const ssr = false; // ✅
// Always define trailingSlash for SEO
trailingSlash: 'never' // ✅
❌ Don’t Do This:
// Prerender dynamic routes without 'entries'
export const prerender = true; // Error if [id] is missing // ❌
// Disable CSR if the page has interactivity
export const csr = false; // Buttons will be dead! // ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Hydration Mismatch | SSR/CSR mismatch | Ensure data consistency |
| Dead UI | csr = false | Set csr = true |
| Missing Routes | Dynamic prerendering | Export entries function |
| SEO Issues | trailingSlash: 'ignore' | Use always or never |
Real-World Examples
1. Marketing Page
export const prerender = true;
2. Fully Client-Side App
export const ssr = false;
3. Secure Admin Panel
export const ssr = true;
4. Static Blog Post
export const prerender = true;
5. Complex Dashboard
export const csr = true;
6. Disabling JS (No-JS fallback)
export const csr = false;
7. Build-time Dynamic IDs
export const entries = () => [{ id: '1' }, { id: '2' }];
8. Layout Override
// +layout.ts
export const ssr = true;
9. Trailing Slash Enforcement
trailingSlash: 'always'
10. API Route Policy
// API routes always run on the server
Visual
The Rendering Spectrum
┌──────────────────────────────────────────────┐
│ PRERENDER (SSG) ──► Server ──► Client │
│ SSR (Dynamic) ──► Server ──► Client │
│ SPA (CSR) ──► Browser ──► Browser │
└──────────────────────────────────────────────┘
Hybrid Policy Architecture
┌──────────────────────────────────────────────┐
│ [Layout: SSR=True] │
│ ├── Page A (About) ──► Prerender: True │
│ └── Page B (App) ──► SSR: False │
└──────────────────────────────────────────────┘
Trailing Slash Logic
┌──────────────────────────────────────────────┐
│ /about/ ──► always ──► Keep trailing slash │
│ /about/ ──► never ──► Strip trailing slash │
└──────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
ssr | Server-rendered HTML |
csr | Client-side hydration |
prerender | Static build output |
| Trailing Slash | URL consistency |
| Hybrid | Per-route configuration |
Key takeaways:
- Use SSG for speed. Prerender everything you can; it’s the cheapest, fastest way to deliver content.
- SSR for dynamics. Keep SSR enabled for pages that require SEO or data-driven content that changes per-request.
- SPA for browser apps. If your app is a tool (like a photo editor) that doesn’t need indexing, set
ssr = falseto simplify your server logic. - Trailing slash consistency. Pick
alwaysorneverand stick to it; avoidingignoreprevents URL fragmentation. - Mix and match. SvelteKit is uniquely powerful because it lets you configure each route to match the specific performance and security profile it requires.
Remember: Rendering modes are about managing trade-offs. By defaulting to the right configuration for each specific path—prerendering marketing pages, using SSR for dynamic content, and SPA for heavy dashboards—you build applications that feel instant to the user while remaining technically optimized for the server.
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!