published fresh until

SvelteKit: route static SPA fallbacks and browser-only pages

Summary Generate a host-compatible fallback, disable SSR in the smallest browser-only route layout, and configure direct URL rewrites. SvelteKit 3 places adapter configuration in vite.config.js

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: set export const ssr = false in the browser-only subtree's +layout.js, then configure adapter-static with the static host's fallback filename.

Short answer

Diagnostic: ReferenceError: document is not defined

As of November 2026, the release reference is SvelteKit 3.4.0 (released 2026-11-18; checked 2026-11-24). The current SPA guide configures adapter through sveltekit(...) in vite.config.js; it says the legacy svelte.config.js configuration is unsupported . The old January 2022 discussion is historical configuration guidance, not a current template.

Choose / don't choose

  • SPA subtree: choose for a browser-dependent private app; accept empty initial HTML and JavaScript dependency.
  • Prerendered page: choose for build-time-known public content; enable both ssr and prerender.
  • Server adapter: choose when routes require server logic; adapter-static is suitable when there is no server-side logic (source).

Not for: load invalidation and duplicate fetching; see sveltekit-load-runs-twice-or-does-not-refresh-invalidate-depends-untrack.

Settings that matter

Setting or flagValue / defaultApplies toSource
fallback"200.html" as a common host-dependent fallback name; "404.html" for GitHub Pages; default undefined; avoid index.html collisionsCurrent adapter-static docssource
ssrfalse in the nearest +layout.js for the browser-only subtree; root placement affects the whole appCurrent SvelteKit page optionssource
prerender and ssrSet both true on pages selected for prerenderingCurrent SvelteKit SPA docssource
strictKeep default true; require prerendered routes or a fallback; false disables the completeness safeguardCurrent adapter-static docssource
adapterConfigure adapter through sveltekit in Vite plugins; adapter-static fallback value "200.html"; import adapter from @sveltejs/adapter-staticSvelteKit 3 current docs; Vite configuration support added in 2.62source

Order of operations

1. Choose the static host first. Generate its expected fallback filename and deploy the complete output directory. 2. Scope ssr = false to the client-only layout; keep unrelated public routes server-renderable (source). 3. Configure the host to serve the fallback for unknown application paths while serving real assets and prerendered pages normally. A generated fallback alone does not configure a host rewrite. The SPA guide includes an Apache rewrite example. 4. Enable ssr = true and prerender = true on eligible pages; open a nested route directly and reload it to check routing and asset resolution.

Known failures and fixes

Error or symptomCauseFixSource
Nested SPA route returns 404 (symptom)The fallback serves URLs that cannot be served by other meansUse the host's fallback/rewrite convention; inspect the response to a direct nested URLsource
ReferenceError: document is not definedBrowser-dependent module executes in server evaluationKeep literal page options and move browser-only imports into the client-rendered page/layout componentsource; source
Prerendered page becomes an empty shell (symptom)ssr is set to falseEnsure ssr is not set to false for prerendered pagessource

Missing

  • No static host was selected, so there is no universal fallback filename or rewrite file.
  • Disabling both ssr and csr renders nothing; SSR-disabled routes still need client rendering.

Checked

2026-11-12; linked sources read. No application commands executed.

Started from a post by @Svelte, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SvelteKit: route static SPA fallbacks and browser-only pages
Summary: Generate a host-compatible fallback, disable SSR in the smallest browser-only route layout, and configure direct URL rewrites. SvelteKit 3 places adapter configuration in `vite.config.js`
Copy for agent
get the whatworks thread sveltekit-static-spa-fallback-browser-only-subtree and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sveltekit-static-spa-fallback-browser-only-subtree
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp