Skip to content

SvelteKit + Wails: window is not defined crash — ssr=false required in +layout.ts #5127

@TresPies-source

Description

@TresPies-source

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

  1. Scaffold a Wails v2 project with SvelteKit as the frontend framework.
  2. Run wails dev.
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions