I've been heads-down in a SvelteKit 5 codebase for a while — full-feature e-commerce with MongoDB, S3, Firebase, Airwallex payments, Shopify, the works. Not a toy app. This is the post I wish someone had handed me on day one.
It's opinionated on purpose. Generic "SvelteKit is fast" takes don't help when you're three hours into debugging why localhost:8081 resolves to the wrong container.
The short version
- SvelteKit's mental model is small, but the boundaries matter. Server vs client, static vs dynamic env, route file vs page component. Get those wrong and you'll fight the framework.
- Performance is real, but not free. You still have to make decisions about hydration, adapter, and what you do at request time vs build time.
- Svelte 5 runes change muscle memory. If you're coming from Svelte 4,
$:and$app/storesaren't where the action is anymore.
Do: pick your adapter on purpose
We ship with both @sveltejs/adapter-node and @sveltejs/adapter-cloudflare installed, and a build-time ADAPTER_TARGET env var switches between them. That sounds clever, but the real lesson is:
adapter-nodeis the boring, predictable choice. SSR runs in a Node process you control. Sentry, sessions, long-lived clients (MongoDB pool, etc.) all behave normally.adapter-cloudflaregets you Workers' edge cold-start story, but you're now in a runtime where Node APIs don't exist by default and any library that touchesfs,Buffer, or sockets is a landmine.
Don't assume you can swap adapters at the end. Choose early, and choose based on what you actually need at the edge vs. in a region.
Do: respect $env/static/private vs $env/dynamic/private
This one bit me hard. In our codebase, basically every API_URL reference imports from $env/static/private. That's a Vite-time replacement — the value is baked into the build artifact.
That works great until you containerize. Inside a Docker network, http://localhost:8081 resolves to the container itself, not the API container. So a build with API_URL=http://localhost:8081 will silently call into the wrong thing in production.
Two takeaways:
- For values that change per environment but not per request,
$env/static/privateis fine — just remember it's baked at build time. Bake the right value. - For values that need to be readable at runtime (feature flags read from the process env, things you set in docker-compose), use
$env/dynamic/privateand guard with an explicitif (!env.API_URL) throw new Error(...).
If you ever find yourself wondering "why is my prod build calling localhost", this is almost always why.
Do: keep analytics and side effects in route files, not page components
Easy mistake: you have a page component like MenLandingPage.svelte and you want to fire a Meta Pixel ViewContent when users land on it. The instinct is to drop onMount inside the component.
Don't. Put it in the route file (+page.svelte), not the page component library.
Why: $page (or in Svelte 5, page from $app/state) is conceptually a route-level concern. The page component is presentational and reusable. We had a setup where HomepageV2Page was reused by both / (when a feature flag was on) and /homepage-v2. If analytics had been inside the component, the same "view" would fire from two different routes with no way to tell them apart.
Rule of thumb: route files own context (params, URL, analytics, redirects). Page components own UI.
Do: let SvelteKit's $types typing do the work — consistently
SvelteKit auto-generates $types.d.ts per route, but only when that route has a +page.server.ts or +page.ts with a load function. If you grep your codebase and find that 25 of your routes import PageServerData from ./$types and 3 don't, you have a smell:
- Either those 3 routes are intentionally client-only (no server load), in which case fine — but say so.
- Or they used to have a server load and someone removed it without cleaning up.
- Or someone added a route by hand and never typed the
dataprop.
The fix isn't a megalith src/lib/types.ts. The fix is: if a route has a +page.server.ts, the matching +page.svelte should import PageServerData from ./$types. That's it. SvelteKit already generated the type. Use it.
Don't create a shared types file that re-declares what SvelteKit already generates. You'll drift, and the auto-generated version always wins.
Do: run svelte-kit sync before type-checking in CI
Our check script is literally svelte-kit sync && svelte-check --tsconfig ./tsconfig.json. The sync step is what materializes .svelte-kit/types/ so svelte-check doesn't choke on missing route types.
If your CI is flaky with "cannot find module './$types'" errors, this is almost always the cause. The dev server runs sync automatically; CI doesn't, unless you tell it to.
Don't: skip CSRF and CSP just because dev works
In svelte.config.js we have:
csrf: {
checkOrigin: true,
trustedOrigins: env.VITE_APP_ENV === 'production' ? [] : ['http://localhost:5173'],
}
Empty trusted origins in production = strict. Good. The temptation is to add a few "trusted" external origins to make some integration work. Don't. Once that list grows, CSRF protection becomes decorative.
CSP is the other half. We deliberately do not define CSP in svelte.config.js. Why? Our policy hits 50+ allowed domains (analytics, payment providers, CDNs, fonts, image hosts). Baking that into the build slows every cold start. Instead, CSP headers are set in hooks.server.ts per request.
Trade-off: per-request CPU cost, but builds stay fast and we can change CSP without redeploying. Worth it.
Don't: rely on SvelteKit's $app/stores in new code
If you're on Svelte 5 (and you should be by now), $app/state is the new home for route state. The classic import { page } from '$app/stores' still works, but it's the bridge, not the destination.
In Svelte 5:
<script>
import { page } from '$app/state';
// page.params, page.url, page.data — no $ prefix needed
</script>
Mixing the two in the same codebase is technically fine and practically painful. Pick one (the new one) and migrate over time.
Don't: pre-compress static assets
Default precompress: false is correct for almost everyone. Your reverse proxy (nginx, Cloudflare, Fly's edge) compresses better than the build does and respects Accept-Encoding more intelligently. Build-time compression duplicates the work and bloats the deploy artifact.
The exception is fully static, Cloudflare-Worker-only deploys where you have no proxy. Even then, measure first.
Performance: what it actually feels like
Honest version, after months of real use:
What works as advertised
- Page-to-page navigation is fast. SPA-style client routing with server-rendered initial HTML is the right shape. First paint feels static-site-fast; subsequent navigations feel instant.
loadfunctions are predictable. Serverloadruns on the server, clientloadruns on the client, they compose, and thedataprop is well-typed (if you use$types). Compared to fighting Next.js's data-fetching mental model, this feels engineered, not bolted-on.- The bundle is genuinely small. Svelte's compile-away-the-runtime story is real. You feel it on TTI for the first paint and on mobile.
What you'll still have to engineer
- Server
loadis on the critical path. A slow API call in+page.server.tsblocks SSR. There's no React-Suspense-style streaming-by-default in stable SvelteKit. If your homepage's serverloadcalls four APIs serially, your TTFB is the sum of all four. Parallelize withPromise.all, or push non-critical data to the client. - Hydration cost still exists. "No virtual DOM" doesn't mean "no hydration." If your route mounts a tree of 200 reactive components, that still has a cost.
- Image performance is your job. SvelteKit doesn't ship an
<Image>component like Next.js. You're expected to bring your own (or use@sveltejs/enhanced-img). On an e-commerce site, this is the single biggest perf lever — don't ignore it.
Lighthouse vs. reality
Lighthouse will tell you a SvelteKit app is fast. It usually is. But Lighthouse won't catch:
- A
+page.server.tsthat calls a downstream API which itself has p95 latency of 800ms. - A bundle that's small but loads a 400KB analytics script in a
<script>tag inapp.html. - A CSP misconfiguration that causes the browser to retry-block-retry a critical resource.
Real user monitoring (RUM) plus Sentry's performance tracing tells you the truth. Lighthouse is a smoke test.
The five-line summary
If I had to compress this into rules:
- Server vs client is a hard boundary. Don't import
server-onlymodules from client components. Don't read$env/static/privateoutside server files. SvelteKit will yell — listen. - Route files own context. Page components own UI.
- Use the types SvelteKit generates. Don't re-invent them.
- Pick your adapter on purpose, early.
- Performance is good by default and great with effort. The defaults won't make a slow API fast.
When I'd reach for SvelteKit again
Greenfield product, small-to-mid team, content + interactivity (e-commerce, marketing sites with auth, dashboards): yes, eagerly. The DX is the best I've used and Svelte 5 runes are a meaningful improvement on Svelte 4.
I'd think twice if my team is large and already deep in React, or if I need a niche library that only exists in the React ecosystem. SvelteKit's ecosystem is good — it's not React-sized.
But for the projects where it fits, it fits well. Less code. Faster pages. Fewer footguns than I was expecting going in — and the ones that exist are documented above.
