SvelteKit 3: what's new

By Esteban Trillonew today8 min readWhy SvelteSvelte

The build and the startup check catch most of what falls out of an upgrade. svelte-check catches some of the rest, so hold both sieves before anything reaches users.
The build and the startup check catch most of what falls out of an upgrade. svelte-check catches some of the rest, so hold both sieves before anything reaches users.

Part 15 of a series on Svelte in 2026. Part 14 covered remote functions, which are still experimental. This part is about what SvelteKit 3 changed that isn’t.

I copied an old component into a SvelteKit 3 app, one that read $page.url.pathname from $app/stores. vite build passed without a warning. Then I opened the page and got a 500, with app_stores_removed in the server log.

That’s the exception, and it’s why I noticed the rule. Almost everything else I got wrong while trying SvelteKit 3 stopped the build or the dev server before a request could reach it: a leftover config file, a $lib import, a missing environment variable, a secret imported into a component. The release post lists the changes as config, #lib, env vars, service workers and errors.1 What they have in common is that SvelteKit 3 wants to fail early, and mostly it does.

I ran everything below on SvelteKit 3.1.0, Svelte 5.57.3, Vite 8.3.5 and svelte-check 4.7.7, with Playwright 1.64.1 in Chromium against vite build plus vite preview.

The config file is gone, loudly

SvelteKit 3 doesn’t read svelte.config.js. Everything that lived under kit goes straight into the sveltekit() plugin, next to compilerOptions:

vite.config.ts
import adapter from "@sveltejs/adapter-auto";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [sveltekit({ adapter: adapter() })],
});

The release candidate post gives the reason: the Vite plugin needs its config straight away, without an async step to go and find another file.2 An old svelte.config.js left next to it is neither merged nor ignored. The build stops with config_file_unsupported and a sentence telling you to move the options into the plugin. A silently ignored config file would be far worse.

#lib wants the extension, but only one tool notices

$lib was a SvelteKit alias. #lib is a Node subpath import, declared in your own package.json:

package.json
"imports": {
"#lib": "./src/lib/index.js",
"#lib/*": "./src/lib/*"
}

An old $lib/greet import failed the build with module_removed_lib, and the message offered an escape hatch: alias: { $lib: "src/lib" } in the plugin. It works, and the build prints config_option_deprecated_alias every time, which is the right amount of nagging.

The extension is the part I’d watch. The migration guide says to write #lib/greet.js because extensions are now required.3 I wrote #lib/greet anyway. vite build passed, vite dev rendered “hi ada”, and only svelte-check complained: “Cannot find module ‘#lib/greet’”. Vite resolves it and TypeScript doesn’t, so if your CI runs the build and skips the type check, you’ll ship imports your editor underlines in red. #lib/greet.js satisfied both, even though the file on disk is greet.ts.

Environment variables you declare up front

This is the best change in the release. In SvelteKit 2 you picked one of four $env modules, static or dynamic, public or private, and every value was a string you validated yourself, if you remembered to. In 3, you list your variables once in src/env.ts, each with a schema:

src/env.ts
import { defineEnvVars } from "@sveltejs/kit/env";
import * as v from "valibot";
export const variables = defineEnvVars({
API_URL: { schema: v.pipe(v.string(), v.url()) },
PORT: {
schema: (value) => {
if (value === undefined) return 3000;
const port = Number(value);
if (!Number.isInteger(port)) throw new Error("PORT must be an integer");
return port;
},
},
});

Server code imports them from $app/env/private; anything marked public: true comes from $app/env/public.4 The four $env modules still exist, deprecated, and go away in SvelteKit 4.

I built with no API_URL set, and the build refused: env_invalid, then API_URL - Invalid type: Expected string but received undefined. With API_URL=nope it was Invalid URL: Received "nope", and PORT=abc printed my own message, PORT must be an integer. Variables are read at runtime by default, so I built with API_URL=https://build.example and started vite preview with https://runtime.example and PORT=8080. The page showed the runtime URL, and typeof PORT was number, because the schema’s return value is what you import. Starting the preview server with API_URL unset failed before it bound a port, with the same env_invalid message.

That last one matters more than the build check. A missing secret used to be a 500 on the first request that touched it, maybe hours after a deploy. Now the process won’t start.

The private module is guarded, too. I imported API_URL from $app/env/private into a +page.svelte, and the build stopped with server_only_import: “Cannot import $app/env/private into code that runs in the browser, as this could leak sensitive information.” svelte-check reported 0 errors for the same file. The build is the guard here, not the types.

One detail from the docs’ static: true example, where a public flag decides whether a <DebugOverlay /> gets rendered and the docs say the component stays out of the bundle unless the flag is set.4 I built it both ways. With the flag off, the {#if} branch compiled to an empty function and the overlay component’s code was gone. Its markup string, <aside>OVERLAY_COMPONENT_MARKER</aside>, was still sitting in the page’s client chunk. So it’s dead code elimination for the code, not for the template. Don’t put anything in a “hidden” component’s markup that you wouldn’t put in the page.

src/env.ts declares STRIPE_KEY: { schema: v.string() }, private, read at runtime. You build in CI with STRIPE_KEY set, then deploy the output to a server where nobody has set it yet. What happens?

Any folder called server is server-only

In SvelteKit 2, only src/lib/server was off-limits to the browser. In 3, any directory named server is (except under src/routes and static), and so is any file with a server segment, like db.server.ts.3 I put a constant in src/features/server/secret.ts and imported it into a page component. The build stopped with the same server_only_import as the env var. If your project is organised by feature instead of by lib, that’s a guard you didn’t have before.

Every error goes through one hook

This is the section where SvelteKit 3 changed behaviour you might rely on. handleError now receives every error, including the ones you throw on purpose with error(...), and it tells you which kind it got.3 I logged each one from a hooks.server.ts:

src/hooks.server.ts
import type { HandleServerError } from "@sveltejs/kit/hooks";
export const handleError: HandleServerError = ({ kind, error }) => {
console.log(kind, error);
if (kind === "unknown") return { message: "Something broke on our side" };
};

error(404, "No such post", { code: "POST_MISSING" }) in a load arrived as app, with status: 404 already on the object, since App.Error always carries a status now. An unmatched URL arrived as framework. A page component that read user.name on an undefined user arrived as unknown, and the response was my +error.svelte with a 500 and my message.

Two details are worth knowing. The stack trace in that production build pointed at src/routes/boom/+page.svelte:5:17, the line I wrote, because SvelteKit 3 applies sourcemaps to server stack traces. And the render error was caught at all: +error.svelte now handles errors thrown while rendering as well as while loading.2 I checked the client side with a navigation from / to the broken page. The error page appeared in place, and a value I’d put on window before clicking was still there, so the app hadn’t reloaded or died.

It showed “Internal Error”, though, not my message. The server hook doesn’t run for an error thrown in the browser. I needed the same three lines in hooks.client.ts before the client said “Something broke on our side” too.

You add a handleError to hooks.server.ts that turns unknown errors into a friendly message. A component throws while rendering after a client-side navigation. What does the error page say?

The small changes that will still bite

$app/stores is the one I opened with. The module still exists so old imports resolve, and every export just throws app_stores_removed when used. svelte-check caught it (“Cannot find module ‘$app/stores’”), the build didn’t. The same goes for $app/environment, which is now $app/env. Run the type check.

page.url is read-only. page.url.searchParams.set("q", "1") is now a type error, “Property ‘set’ does not exist on type ‘ReadonlyURLSearchParams’”, so copy it into a new URL(page.url) first.

goto("https://example.com") rejects. It throws navigation_external_url and the page stays put; for an external URL you set window.location.href yourself.

And clicking a link to the page you’re already on now re-runs its loads. In 2 it did nothing. A server load counter went from 1 to 2 on that click, with one GET /self/__data.json request.

The rest of the migration guide is long: pushState becoming goto(..., { shallow: true }), invalidateAll becoming refreshAll, resolve() replacing base, cookies defaulting to path: "/", Node 22.17 as the minimum, and adapter changes like Cloudflare’s env moving to cloudflare:workers.3 Part 16 runs sv migrate over a real app to see how much of it the tool does for you.

Next.js does env vars with a prefix

React has no opinion here, so the comparison is Next.js. Its docs keep anything without NEXT_PUBLIC_ on the server, and anything with it gets inlined into the bundle at build time, where it’s “frozen with the value evaluated at build time”.5 Server values are read from process.env at runtime, as plain strings. There’s no schema, so a missing key is undefined until something reads it. I didn’t build the Next.js side; that’s from the docs.

The prefix is a fine rule, and SvelteKit’s version asks for more typing in src/env.ts. In exchange you get a number when you asked for a number, a process that won’t start without its secrets, and a build that refuses to ship a private import to the browser.

What I’d take from this

Run svelte-check in CI next to vite build. The build caught the config file, $lib, the env schema and both server-only imports. The type check caught the extensionless #lib import, $app/stores and the read-only URL, all of which the build waved through.

Put a schema on every env var, even a lazy v.string(). That alone turns “500 on the first checkout” into “won’t start”. And if you write a server handleError, write the client one in the same commit.

The changes in SvelteKit 3 are mostly renames and moves, and I don’t think any of them is a reason on its own to pick Svelte. Taken together they’re a framework that tells you about a broken deploy before your users do, which is most of what I want from one.

Next: migrating with sv migrate, and what it leaves on its TODO list.

  • Part 16 — Migrating to SvelteKit 3 with sv migrate: Running sv migrate sveltekit-3 over a real SvelteKit 2 app: what it rewrote, what its MIGRATION_TASKS.md listed, and the two broken changes nothing flagged

Footnotes

  1. Svelte blog, SvelteKit 3 is here. ↩

  2. Svelte blog, The SvelteKit 3 release candidate, “Configuration”, “Error handling”. ↩ ↩2

  3. SvelteKit docs, Migrating to SvelteKit 3. ↩ ↩2 ↩3 ↩4

  4. SvelteKit docs, Environment variables. ↩ ↩2

  5. Next.js docs, Environment variables, “Bundling Environment Variables for the Browser”. ↩

Read next

  1. Migrating to SvelteKit 3 with sv migrateDec 1, 2026 Why Svelte · 7 min
  2. Remote functions in SvelteKitDec 1, 2026 Why Svelte · 8 min
  3. Form actions and progressive enhancementNov 30, 2026 Why Svelte · 9 min
  4. SvelteKit fundamentals: routes and layoutsNov 30, 2026 Why Svelte · 8 min
  5. Scoped styles: CSS that stays putNov 30, 2026 Why Svelte · 9 min
Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.