SvelteKit

On this page 8

SvelteKit is the official application framework for Svelte, a compiler-based UI library, and it supplies everything a component library alone does not: file-based routing, server-side rendering, data loading, form handling and deployment adapters. In the field of web applications, it occupies the same role that Next.js plays for React and Nuxt plays for Vue.js, turning reusable components into a complete, deployable product.

The framework reached version 1.0 in December 2022 and version 2.0 in December 2023, and it builds on Vite for development and bundling. Its defining idea is that one codebase can render pages in three places: on the server, in the browser or at build time, with the choice made per route rather than for the whole site.

How SvelteKit structures an application

SvelteKit organizes an application through filesystem routing: every directory inside src/routes becomes a URL path, and specially named files inside it define what that path does. A directory such as src/routes/blog/[slug] creates a dynamic route whose bracketed segment becomes a parameter available to code.

The plus-sign prefix marks route files that the framework treats as entry points, while all other files remain ordinary modules. This convention separates code that may run in the browser from code that must stay on the server, which is the most important boundary in the framework.

The table below lists the route files that appear in most projects and where each one executes.

FilePurposeRuns on
+page.sveltePage component rendered for the routeServer and browser
+page.jsUniversal load function for page dataServer and browser
+page.server.jsServer load function and form actionsServer only
+layout.svelteShared wrapper for nested routesServer and browser
+server.jsAPI endpoint answering HTTP methodsServer only
+error.svelteError page for the route subtreeServer and browser
Core route files in SvelteKit

Beyond routes, the hooks.server.js file exposes a handle function that intercepts every request. Teams use it for authentication, session lookup and response headers, because it runs before any load function and can modify the response afterward.

Rendering modes and page options

By default, SvelteKit performs server-side rendering on the first request and then hydrates the page so that later navigation happens through client-side routing. This hybrid model gives fast first paint and indexable HTML while keeping the responsiveness of a single-page application.

Developers change this behavior with page options exported from a page or layout file. Because options cascade down the route tree, one layout can turn a whole section into a statically generated area while another section stays fully dynamic.

OptionEffect when setTypical use
prerender = trueHTML is generated once at build timeDocumentation, marketing pages
ssr = falseServer sends an empty shell, browser rendersDashboards that rely on browser-only APIs
csr = falseNo JavaScript is shipped to the pagePurely static content pages
trailingSlashControls whether URLs end with a slashMatching legacy URL structures
Page options that control rendering

Combining these options lets one project behave as an MPA, an SPA or an SSG in different places. Setting both ssr and csr to false is invalid for interactive content, so the framework expects at least one rendering path to remain enabled.

Data loading, form actions and progressive enhancement

Load functions are the standard way to fetch data for a page. A load function returns an object that the page receives as its data property, and SvelteKit runs parallel loads for nested layouts to avoid request waterfalls where possible.

A universal load in +page.js runs on the server during the first render and in the browser on later navigation, so it must not touch secrets. A server load in +page.server.js always runs on the server and can safely use database connections and private environment variables imported from $env/static/private.

Form actions handle POST requests from ordinary HTML forms. Because a native form element works without JavaScript, the page remains usable when scripts fail; adding the use:enhance directive upgrades the same form to submit without a full page reload. This approach follows the principle of progressive enhancement and reduces the amount of custom client code.

SvelteKit belongs to the family of meta-frameworks that wrap a component library with routing and rendering. The differences lie mainly in the underlying UI model and in how much runtime code reaches the browser.

The following comparison summarizes the main architectural contrasts with neighboring frameworks.

FrameworkComponent layerReactivity modelNotable trait
SvelteKitSvelteCompiled, no virtual DOMSmall client bundles
Next.jsReactVirtual DOM with hooksReact Server Components
NuxtVue.jsProxy-based reactivityAuto-imported composables
RemixReactVirtual DOM with hooksWeb-standard forms and loaders
AstroMultiple librariesIslands architectureZero JavaScript by default
SvelteKit and comparable meta-frameworks

Because Svelte compiles components into direct DOM updates, SvelteKit applications often ship less framework runtime than comparable React projects. The trade-off is a smaller ecosystem of third-party components and fewer experienced developers on the hiring market.

Deployment through adapters

SvelteKit separates the build output from the hosting platform through adapters, small packages that convert the generic server build into the format a target environment expects. The default adapter-auto detects common platforms during the build.

Choosing an adapter decides whether the application runs as a long-lived Node.js process, as serverless functions, at the network edge or as plain static files.

AdapterOutputFits
adapter-nodeStandalone Node.js serverContainers and virtual machines
adapter-staticStatic HTML and assetsFully prerendered sites, CDN hosting
adapter-vercelServerless and edge functionsVercel deployments
adapter-netlifyNetlify functionsNetlify deployments
adapter-cloudflareWorkers-compatible bundleCloudflare edge network
Common adapters and their output

Limitations and common mistakes

Most problems with SvelteKit come from misplacing code across the server-browser boundary or from assuming that a single rendering mode applies everywhere.

  • Importing secrets into universal code: the framework blocks private environment modules in browser bundles, but logic copied into +page.js can still leak data through returned objects.
  • Using browser globals during SSR: accessing window or localStorage at module level fails on the server; such code belongs in onMount or behind the browser flag.
  • Prerendering dynamic pages: a prerendered route freezes its data at build time, so frequently changing content shows stale information until the next build.
  • Ignoring migration effort: the move to Svelte 5 introduced runes such as $state and $derived, and older component patterns need gradual updating.

A plain-language summary

Svelte is a tool for building the individual pieces of a website, such as buttons, menus and forms. SvelteKit is the toolkit that assembles those pieces into a complete website, deciding which page appears at which address, where the data comes from and how the finished product is put online.

A useful comparison is a kit house. Svelte provides well-made walls and windows, while SvelteKit provides the floor plan, the plumbing and the delivery truck. The result is a fast site that works before scripts load, and it can be placed on many kinds of hosting.

An example from practice

A small regional newspaper rebuilds its website with SvelteKit. Article pages use a server load function that reads stories from the content database, and the editors configure the archive section with prerender = true, so thousands of older articles become static files served directly from a content delivery network.

The comment form on each article uses a form action with use:enhance, which means readers on slow mobile connections can still post comments even if JavaScript has not finished loading. The team deploys with adapter-node inside a container on its existing infrastructure.

After launch, the newsroom observes that article pages are noticeably lighter than in the previous React-based site, and the archive no longer places load on the database. The editors gain speed and lower hosting costs without giving up dynamic features on the current news pages.