SvelteKit 3.0 Production Architecture & Migration Guide — SveltyCMS
SveltyCMS is built on the latest SvelteKit 3 (@sveltejs/kit ^3.0.1) and Svelte 5 runes following official sveltekit.com best practices. Explore the production migration guide and architectural conventions.
On this page
SveltyCMS is built natively on SvelteKit 3 (@sveltejs/kit: ^3.0.1) and Svelte 5 (Runes).
Rather than relying on legacy frameworks or retrofitting older architectures, SveltyCMS is implemented from the ground up according to official best practices from sveltekit.com and svelte.dev.
This guide serves a dual purpose:
- Community Migration Guide: A real-world production reference for developers in the Svelte ecosystem upgrading their applications to SvelteKit 3.
- Plugin & Widget Developer Contract: An architectural guide for third-party developers building custom plugins, widgets, and themes for SveltyCMS.
Why SveltyCMS Embraces SvelteKit 3 & Official Best Practices
By adopting SvelteKit 3 and Svelte 5, SveltyCMS leverages the latest advancements in web architecture:
- Fine-Grained Reactivity (Runes): Moving beyond legacy store subscriptions, SveltyCMS uses
$state(),$derived(), and$props(). Fine-grained reactivity updates only the exact DOM nodes that change, completely bypassing virtual DOM diffing and ensuring the admin interface responds in sub-millisecond times. - Deterministic Hydration with
$props.id(): Hydration mismatches are eliminated across SSR and client DOM trees by generating deterministic IDs via$props.id()rather than runtime random values. - In-Process LocalCMS vs. HTTP Serialization: Server routes (
+page.server.tsand hooks) communicate with the CMS core through in-processLocalCMScalls (0–2ms), adhering to SvelteKit’s server function model rather than making unnecessary internal HTTP requests. - Defensive API & Redirect Security: All external redirects (such as OAuth identity flows) enforce explicit
{ allowExternal: true }contracts to protect against open-redirect vulnerabilities.
SvelteKit 3 Upgrade Checklist
For developers upgrading enterprise SvelteKit applications or plugins to SvelteKit 3:
- Update Dependencies:
bun add -D @sveltejs/kit@^3.0.1 @sveltejs/vite-plugin-svelte@latest - Synchronize SvelteKit Artifacts:
bun x svelte-kit sync - Audit Environment Variables: Replace legacy
process.env.ORIGINwithevent.url.originin request handlers, or configure explicit environment variables for background workers. - Audit External Redirects: Add
{ allowExternal: true }(or{ external: true }) to any OAuth or third-party redirect calls. - Update Error Handlers: Update
handleErrorinhooks.server.tsto return explicit{ message, code, status }objects. - Embrace Svelte 5 Runes: Use
$state(),$derived(), and$props.id()for deterministic DOM/ARIA IDs across SSR and hydration.
A) process.env.ORIGIN → event.url.origin / kit.paths.origin
Kit 3.0 removes process.env.ORIGIN. In SveltyCMS, origin resolution is handled via event.url.origin (where request event is available) or configured environment variable:
| File | Line | Change |
|---|---|---|
src/hooks/handle-system-state.ts |
90 | process.env.ORIGIN → event.url.origin |
src/plugins/sitemap/index.server.ts |
50 | process.env.ORIGIN → process.env.SVELTYCMS_ORIGIN (plugin context — no event available) |
B) External Redirects Require allowExternal: true
Kit 3.0 forbids redirects to external URLs by default. OAuth and IdP flows require { allowExternal: true }:
| File | Lines | Change |
|---|---|---|
src/routes/login/+page.server.ts |
490, 500 | redirect(303, authUrl) → redirect(303, authUrl, { allowExternal: true }) |
src/routes/login/oauth/+page.server.ts |
380, 398, 412, 612 | redirect(302, authUrl) → redirect(302, authUrl, { allowExternal: true }) |
C) handleError Returns Status Code
Kit 3.0 allows handleError in hooks.server.ts to influence the HTTP response status code by returning { message, code, status }:
| File | Line | Change |
|---|---|---|
src/hooks.server.ts |
607 | Add return { message, code, status }; after the logger.error() call |
D) fail() Status Code Propagation
Kit 3.0 propagates the status code from fail(status) to the HTTP response automatically. SveltyCMS leverages this across server actions:
src/routes/login/+page.server.ts—fail(401),fail(429)for auth/rate limitingsrc/routes/(app)/config/collectionbuilder/collectionbuilder.server.ts—fail(400),fail(404),fail(500)- All action handlers adhere to explicit error status codes
Verification & Parity
The SvelteKit 3 implementation is validated continuously through the CI and local test harness:
bun run check: Validates types against$app/typesand extended$app/tsconfig.bun run test:unit: Vitest suite runs across all 625 test files on SvelteKit 3.bun run test:security: Verifies OAuth external redirects, authentication hooks, and error status handling.
Production Reference Implementation for the Svelte Community
SveltyCMS serves as an open-source reference implementation for enterprise SvelteKit 3 and Svelte 5 application design. Developers migrating their own projects can inspect SveltyCMS’s codebase to see real-world implementations of:
- Type-safe Remote Functions: Clean
.server.tsaction handling without bulky form action boilerplate. - In-Process LocalCMS: Zero-tax in-memory data retrieval alongside resilient database adapters (SQLite, PostgreSQL, MariaDB, MongoDB).
- Enterprise Identity: Passkeys/WebAuthn, 2FA, and SCIM 2.0 user provisioning integrated with SvelteKit server hooks.
Frequently Asked Questions (FAQ)
How do I fix process.env.ORIGIN errors in SvelteKit 3?
In SvelteKit 3, process.env.ORIGIN is no longer automatically injected. For server actions, endpoint handlers, and hooks where the request event is present, read event.url.origin. In isolated background contexts (such as sitemap generators or cron tasks), supply a dedicated environment variable (e.g., process.env.SVELTYCMS_ORIGIN).
Why does redirect() throw an error when pointing to an external OAuth URL?
By default, SvelteKit 3 fail-closes against open-redirect vulnerabilities. When redirecting to an external origin (such as GitHub, Google, or enterprise Okta/Entra ID IdPs), pass { allowExternal: true } as the third parameter:
redirect(302, authUrl, { allowExternal: true });
How do Svelte 5 runes interact with SvelteKit 3 routing?
SvelteKit 3 fully embraces Svelte 5 runes. Page and layout data are accessed reactively via $props() rather than legacy export let data. Computed view states use $derived(), side effects use $effect(), and deterministic client/server HTML element IDs are generated with $props.id() rather than runtime random numbers.