Summary
When using SvelteKit (adapter-static) inside a Wails v2 app, the SvelteKit dev server runs an SSR pass during vite dev even for SPA configurations. This pass crashes with ReferenceError: window is not defined because Wails runtime bindings (EventsOn, window.go.*) access window during module evaluation.
Steps to reproduce
- Scaffold a Wails v2 project with SvelteKit as the frontend framework.
- Run
wails dev.
- Observe crash:
ReferenceError: window is not defined in terminal.
Root cause
adapter-static with fallback: 'index.html' is an SPA configuration, but SvelteKit still performs an SSR pass during vite dev to generate the initial HTML shell. Any code that accesses window at module evaluation time crashes the Node.js SSR pass.
Fix — two layers
Layer 1 (required): Create src/routes/+layout.ts with:
```typescript
export const ssr = false;
export const prerender = false;
```
Layer 2 (defensive): Wrap any Wails listener registration in a guard:
```typescript
if (typeof window !== 'undefined') {
EventsOn('event:name', handler);
}
```
Also: Remove <link rel="stylesheet" href="%sveltekit.assets%/app.css" /> from app.html if present — SvelteKit does not serve src/app.css as a static file. Import global CSS in +layout.svelte instead: import '../app.css'.
Suggested documentation addition
The Wails SvelteKit integration guide (or a cookbook entry) should include this fix prominently. It is not obvious that adapter-static still runs SSR during development.
Environment
- Wails v2 (tested v2.9+)
- SvelteKit with adapter-static
- macOS
Summary
When using SvelteKit (adapter-static) inside a Wails v2 app, the SvelteKit dev server runs an SSR pass during
vite deveven for SPA configurations. This pass crashes withReferenceError: window is not definedbecause Wails runtime bindings (EventsOn,window.go.*) accesswindowduring module evaluation.Steps to reproduce
wails dev.ReferenceError: window is not definedin terminal.Root cause
adapter-staticwithfallback: 'index.html'is an SPA configuration, but SvelteKit still performs an SSR pass duringvite devto generate the initial HTML shell. Any code that accesseswindowat module evaluation time crashes the Node.js SSR pass.Fix — two layers
Layer 1 (required): Create
src/routes/+layout.tswith:```typescript
export const ssr = false;
export const prerender = false;
```
Layer 2 (defensive): Wrap any Wails listener registration in a guard:
```typescript
if (typeof window !== 'undefined') {
EventsOn('event:name', handler);
}
```
Also: Remove
<link rel="stylesheet" href="%sveltekit.assets%/app.css" />fromapp.htmlif present — SvelteKit does not servesrc/app.cssas a static file. Import global CSS in+layout.svelteinstead:import '../app.css'.Suggested documentation addition
The Wails SvelteKit integration guide (or a cookbook entry) should include this fix prominently. It is not obvious that
adapter-staticstill runs SSR during development.Environment