Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 35 ยท SvelteKit

Rendering Modes and Deploying SvelteKit

Choose between SSR, prerendering, static sites and SPA mode in SvelteKit, set ssr, csr and prerender page options, handle env vars and pick the right adapter.

A finished SvelteKit app can be delivered in several ways. A server can render each page on demand, a build step can render pages once into plain files, or the browser can render everything. You choose per project, even per page, using page options. Then an adapter packages the build for your host.

SvelteKit and its build run only on a developer machine or server, so everything here is read-only. The stand-in <TryIt> blocks model the decisions in plain JavaScript (which pages can be prerendered, which adapter fits which host).

Server-side rendering (the default)

By default SvelteKit renders a page on the server for the first request, sends complete HTML, then hydrates it in the browser: Svelte attaches event listeners to the existing DOM so the page becomes interactive. Later navigations are client-side (only data is fetched, not full HTML).

Benefits: fast first paint, good SEO, works before JavaScript loads. The cost is that you need a server (or serverless functions).

flowchart LR
  A["Browser requests /blog/hello"] --> B["Server runs load + renders HTML"]
  B --> C["Browser shows HTML immediately"]
  C --> D["JS loads and hydrates"]
  D --> E["Next clicks: client-side navigation"]

SSR then hydration.

Page options

Export constants from +page.js, +page.server.js, +layout.js or +layout.server.js. A layout's options apply to everything below it, and a page can override them.

JavaScript
// +layout.js
export const prerender = true;   // render at build time
export const ssr = true;         // render on the server (default)
export const csr = true;         // ship JS and hydrate (default)
export const trailingSlash = 'never';
Option Meaning
prerender = true Generate the HTML once at build time. 'auto' prerenders but still allows server rendering for pages not found at build time.
ssr = false Do not render on the server: send an empty shell; the browser renders (SPA behavior for that page).
csr = false Ship no JavaScript for the page. It becomes pure HTML (forms still work).
trailingSlash 'never' (default), 'always' or 'ignore'.

Combine them freely: a marketing page prerendered, a dashboard with ssr = false (it needs window and is behind login anyway), a long article with csr = false for zero JS.

Which pages can be prerendered?

A page can be prerendered if every user would see the same HTML: no cookies, no user-specific data, no form actions. SvelteKit crawls links from your entry pages; for dynamic routes it must discover every slug, either through links or by listing them:

JavaScript
// src/routes/blog/[slug]/+page.server.js
export const prerender = true;

export function entries() {
  return [{ slug: 'hello' }, { slug: 'second-post' }];
}

Here is a checker that applies those rules:

JS Can this route be prerendered?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Rendering strategies compared

Strategy Options Needs a server? Good for
SSR + hydration defaults yes apps with per-user or fresh data
Static site (SSG) prerender = true everywhere + adapter-static no, any file host blogs, docs, marketing
SPA ssr = false + adapter-static with a fallback page no apps behind a login
Hybrid per-route options yes most real projects
Zero-JS page csr = false depends articles, legal pages

Environment variables

SvelteKit separates what is safe for the browser from what is not:

Module Contents Allowed in
$env/static/private build-time secrets (DATABASE_URL) server code only
$env/dynamic/private run-time secrets server code only
$env/static/public names starting with PUBLIC_ anywhere
$env/dynamic/public run-time PUBLIC_ values anywhere
JavaScript
import { DATABASE_URL } from '$env/static/private';
import { PUBLIC_SITE_NAME } from '$env/static/public';

If client code imports a private module, the build fails, so you cannot leak a secret by accident. Files under $lib/server/ get the same protection.

Adapters

Your build output differs by host, so SvelteKit uses an adapter set in svelte.config.js:

JavaScript
import adapter from '@sveltejs/adapter-node';

export default {
  kit: { adapter: adapter() },
};
Adapter Target
adapter-auto Detects Vercel, Netlify, Cloudflare etc. at deploy time (the default for new projects)
adapter-node A standalone Node server (node build), for VPS or Docker
adapter-static Prerendered files for any static host (GitHub Pages, S3, CDN)
adapter-vercel, adapter-netlify, adapter-cloudflare Serverless/edge platforms, with platform-specific options

For production on your own VPS: npm run build, copy build/, package.json and install production dependencies, then run node build behind a reverse proxy. With adapter-node set ORIGIN (for the CSRF check) and PORT environment variables.

JS Choosing an adapter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Before you ship

  • Run npm run build then npm run preview to test the production output locally.
  • Set the <title> and meta description per page with <svelte:head>.
  • Add a sitemap.xml and robots.txt (a prerendered +server.js route works well).
  • Confirm secrets never appear in .svelte files; only PUBLIC_ values may.
  • Add the security headers in handle and test the app with JavaScript disabled for key flows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does hydration mean in SvelteKit?

    The server sends complete HTML; the client then runs the components against that DOM to make it interactive.
  2. 2. Which option generates a page's HTML at build time?

    prerender = true renders the page once during the build. ssr and csr control server rendering and client JavaScript.
  3. 3. Which env module is safe to import in browser code?

    Only variables prefixed with PUBLIC_ are exposed to the client. Importing a private module from client code fails the build.
  4. 4. Which adapter produces a standalone Node server?

    adapter-node outputs a server you start with node build. adapter-static outputs only files.

Summary

  • SvelteKit renders on the server by default, then hydrates; navigation afterwards happens client-side.
  • Page options prerender, ssr, csr and trailingSlash can be set per layout or page.
  • Prerender pages that are identical for everyone; use entries() for dynamic routes.
  • Private env modules are server-only; only PUBLIC_ values reach the browser.
  • Pick an adapter for your host: adapter-static, adapter-node or a platform adapter.

Next: Testing Svelte Components