`localStorage.getItem is not a function` aborted every server render, so the whole UI loaded behind a Vite error overlay. Node 22+ exposes a global `localStorage`, and without a usable `--localstorage-file` it is a bare object with no Storage methods. The `typeof localStorage === 'undefined'` guard therefore passed and the next call threw. It only bit at module/component scope, because SvelteKit evaluates those on the server too — the same guard is harmless inside onMount and $effect. Probe for a callable method in a shared storage helper instead of trusting the binding to exist, and route the SSR-executed reads through it.
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
/**
|
|
* SSR-safe `localStorage` access.
|
|
*
|
|
* `typeof localStorage === 'undefined'` is NOT a sufficient guard on modern
|
|
* Node. Node 22+ ships a web-storage global, and when it is enabled without a
|
|
* usable `--localstorage-file` the binding exists but is not a `Storage`: the
|
|
* typeof check passes and the next call throws
|
|
* `localStorage.getItem is not a function`. Because SvelteKit evaluates module
|
|
* and component scope on the server, that turns a page load into a 500.
|
|
*
|
|
* Probe for a callable method instead of trusting the binding to exist.
|
|
*/
|
|
function webStorage(): Storage | null {
|
|
try {
|
|
if (typeof localStorage === 'undefined') return null;
|
|
return typeof localStorage.getItem === 'function' ? localStorage : null;
|
|
} catch {
|
|
// Reading the global itself can throw (sandboxed frame, blocked storage).
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function storageGet(key: string): string | null {
|
|
try {
|
|
return webStorage()?.getItem(key) ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function storageSet(key: string, value: string): void {
|
|
try {
|
|
webStorage()?.setItem(key, value);
|
|
} catch {
|
|
// Private mode / quota exceeded — persistence is best-effort.
|
|
}
|
|
}
|
|
|
|
export function storageRemove(key: string): void {
|
|
try {
|
|
webStorage()?.removeItem(key);
|
|
} catch {
|
|
// Best-effort.
|
|
}
|
|
}
|