SvelteKit
Last reviewed: 2026-10-27 · Applies to: SvelteKit 3.1 (notes for 2.x where they differ)
When to read this
When @sveltejs/kit is a dependency. SvelteKit has no built-in server data cache: caching is HTTP headers (setHeaders), prerendering, adapter features (Vercel ISR) and your own server-side cache.
How to detect
@sveltejs/kit in package.json. Major ≥ 3: config lives in the sveltekit({...}) Vite plugin in vite.config.js (svelte.config.js is no longer supported). Major 2: svelte.config.js. Adapters: @sveltejs/adapter-vercel, -netlify, -cloudflare, -node.
Default recipe
// src/routes/products/[id]/+page.server.js — shared page cached at the edge
export async function load({ params, setHeaders }) {
const product = await getProduct(params.id) // your own cache-aside helper in $lib/server
setHeaders({ 'cache-control': 'public, max-age=0, s-maxage=300, stale-while-revalidate=3600' })
return { product }
}// src/routes/blog/[slug]/+page.server.js — Vercel ISR (adapter-vercel)
export const config = { isr: { expiration: 600, bypassToken: process.env.ISR_BYPASS_TOKEN } }export const prerender = true // or 'auto': prerender, but keep it in the SSR manifest for the long tailFor personalized pages set private, no-cache (or nothing shared at all) and keep s-maxage off.
Rules and gotchas
- Setting the same header twice, even from different
loadfunctions (layout and page), is an error. Setcache-controlin one place, usually the leaf page. setHeaderscan't setset-cookie(usecookies), doesn't work inside streamed promises, and is a no-op in the browser.fetchinloadinherits the user's cookies/auth. Responses fetched during SSR are inlined into the HTML and reused at hydration (headers aren't serialized unless listed infilterSerializedResponseHeaders). Don't put personalized SSR fetches on pages cached withs-maxage.- Vercel ISR:
expirationis required; use ISR only on routes where every visitor sees the same content; ISR on aprerender = trueroute does nothing;allowQuerylists query params that create separate entries. - Server data caching is yours to build: an LRU/Redis cache-aside helper in
$lib/server(references/layers/in-process.md,references/layers/distributed-cache.md). - Client reruns:
loadreruns when the params/url/parent it used change. Usedepends('app:key')+invalidate('app:key');refreshAll()reruns everything (SvelteKit 3;invalidateAllis deprecated). Serverloadnever automatically depends on a fetched URL.
Adapters
- adapter-node (3.x): serves static assets with content-hash
ETags and noLast-Modified; GET/HEAD only. Put a CDN in front with explicit headers. - adapter-cloudflare (3.x):
cachesis a global;env/waitUntilfromcloudflare:workers; wrangler ≥ 4.67. UsesetHeaders+ the Cache API or Workers Caching (references/platforms/cloudflare.md). - adapter-netlify uses the Netlify Frameworks API; Netlify ISR-equivalents for SvelteKit 3 weren't documented in this skill's research. Use
Netlify-CDN-Cache-Controlheaders. - adapter-vercel 3.x drops the
edgeruntime.
Remote functions (experimental)
experimental.remoteFunctions + compilerOptions.experimental.async. query args form a cache key (object keys sorted); request-scoped dedupe on the server; identical calls share one instance on the client while in use. Refresh with getPosts().refresh(), set with getPost(id).set(v) from form/command handlers, requested(getPosts, n) for client-requested refreshes, query.batch for n+1. Don't cache live query responses in a service worker. Flag as experimental in any plan.
Version notes
- 3.0: config moved to
vite.config.js;$lib→#lib;$app/environment→$app/env;$service-workerand$app/storesremoved (use$app/manifestfor offline lists);version.pollIntervaldefault one hour;output.linkHeaderPreloaddefaults to<link>elements; Node 22.17, Vite 8.
invalidateAll()(deprecated in 3; userefreshAll()).kit.prerender/kit.versioninsvelte.config.js(move intovite.config.jsin 3).$service-workermodule.
Verify
curl -s -o /dev/null -D - https://example.com/products/1 | grep -iE 'cache-control|age|x-vercel-cache|cf-cache-status'
curl -s -o /dev/null -D - -H 'Cookie: session=x' https://example.com/account | grep -i cache-control # private/no-store