From 33636200f3824f1a249b844e7d9c9d88a7f6c836 Mon Sep 17 00:00:00 2001 From: Benjiyaya Date: Wed, 16 Sep 2026 04:17:42 +0800 Subject: [PATCH] fix(i18n): stop SSR crashing on Node's non-Storage localStorage global MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- calliope-web/src/lib/i18n.svelte.ts | 16 +++---- calliope-web/src/lib/storage.ts | 45 +++++++++++++++++++ .../src/routes/canvas/[id]/+page.svelte | 35 ++++++--------- 3 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 calliope-web/src/lib/storage.ts diff --git a/calliope-web/src/lib/i18n.svelte.ts b/calliope-web/src/lib/i18n.svelte.ts index daad168f..cb62ce1f 100644 --- a/calliope-web/src/lib/i18n.svelte.ts +++ b/calliope-web/src/lib/i18n.svelte.ts @@ -6,6 +6,8 @@ import { ja } from './i18n/ja'; import { ko } from './i18n/ko'; import { zh } from './i18n/zh'; +import { storageGet, storageSet } from './storage'; + export type Language = 'en' | 'zh' | 'es' | 'fr' | 'de' | 'ja' | 'ko'; export type Dict = typeof en; @@ -19,10 +21,10 @@ function isLanguage(value: string | null): value is Language { } function initialLanguage(): Language { - if (typeof localStorage === 'undefined') return 'en'; - // Validate against the registered dictionaries — a stored value from a - // language this build no longer ships must not blank the UI. - const stored = localStorage.getItem(STORAGE_KEY); + // Validated against the registered dictionaries — a stored value naming a + // language this build no longer ships must not blank the UI. This runs on + // the server too, where storage is absent and we render the default. + const stored = storageGet(STORAGE_KEY); return isLanguage(stored) ? stored : 'en'; } @@ -30,11 +32,7 @@ export const language = $state<{ current: Language }>({ current: initialLanguage export function setLanguage(lang: Language) { language.current = lang; - try { - localStorage.setItem(STORAGE_KEY, lang); - } catch { - /* ponytail: storage unavailable (private mode) — UI language just won't persist */ - } + storageSet(STORAGE_KEY, lang); } /** Look up a UI string. Falls back to English, then to the key itself. */ diff --git a/calliope-web/src/lib/storage.ts b/calliope-web/src/lib/storage.ts new file mode 100644 index 00000000..280ac49d --- /dev/null +++ b/calliope-web/src/lib/storage.ts @@ -0,0 +1,45 @@ +/** + * 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. + } +} diff --git a/calliope-web/src/routes/canvas/[id]/+page.svelte b/calliope-web/src/routes/canvas/[id]/+page.svelte index 0b818d4a..c2793dfe 100644 --- a/calliope-web/src/routes/canvas/[id]/+page.svelte +++ b/calliope-web/src/routes/canvas/[id]/+page.svelte @@ -26,6 +26,7 @@ import ArtifactNodeComp from '$lib/canvas/ArtifactNode.svelte'; import Icon from '$lib/components/ui/Icon.svelte'; import { t } from '$lib/i18n.svelte'; + import { storageGet, storageSet } from '$lib/storage'; import { agentApi, assetUrl, @@ -297,10 +298,10 @@ } function restoreViewportFromStorage(): Viewport | null { - if (typeof localStorage === 'undefined') return null; + const raw = storageGet(viewportLocalStorageKey(canvasId)); + if (raw == null) return null; try { - const raw = localStorage.getItem(viewportLocalStorageKey(canvasId)); - return raw ? (JSON.parse(raw) as Viewport) : null; + return JSON.parse(raw) as Viewport; } catch { return null; } @@ -313,11 +314,7 @@ const json = JSON.stringify(v); // localStorage first (synchronous, survives navigation + reloads // even if the request never lands), backend per canvas second. - try { - localStorage.setItem(viewportLocalStorageKey(canvasId), json); - } catch { - // storage full/blocked — backend save still applies - } + storageSet(viewportLocalStorageKey(canvasId), json); void canvasApi .patchCanvas(canvasId, { viewport_json: json }) .catch(() => undefined); @@ -444,7 +441,7 @@ replaceUrlParam('session'); return; } - const stored = Number(localStorage.getItem(SESSION_KEY)); + const stored = Number(storageGet(SESSION_KEY)); if (Number.isFinite(stored) && sessions.some((s) => s.id === stored)) { const s = sessions.find((x) => x.id === stored)!; if ((s.project_id ?? null) === (canvas.project_id ?? null)) { @@ -873,20 +870,16 @@ } } - let railCollapsed = $state( - typeof localStorage !== 'undefined' && localStorage.getItem(RAIL_KEY) === '1', - ); - let chatCollapsed = $state( - typeof localStorage !== 'undefined' && localStorage.getItem(CHAT_KEY) === '1', - ); + let railCollapsed = $state(storageGet(RAIL_KEY) === '1'); + let chatCollapsed = $state(storageGet(CHAT_KEY) === '1'); function toggleRail() { railCollapsed = !railCollapsed; - localStorage.setItem(RAIL_KEY, railCollapsed ? '1' : '0'); + storageSet(RAIL_KEY, railCollapsed ? '1' : '0'); } function toggleChat() { chatCollapsed = !chatCollapsed; - localStorage.setItem(CHAT_KEY, chatCollapsed ? '1' : '0'); + storageSet(CHAT_KEY, chatCollapsed ? '1' : '0'); } // ---- draggable chat splitter --------------------------------------------- @@ -905,7 +898,7 @@ } $effect(() => { - const raw = Number(localStorage.getItem(CHAT_W_KEY)); + const raw = Number(storageGet(CHAT_W_KEY)); if (Number.isFinite(raw) && raw > 0) chatWidth = clampChatWidth(raw); // Keep the panel within the cap when the window shrinks. const onResize = () => (chatWidth = clampChatWidth(chatWidth)); @@ -928,7 +921,7 @@ dragging = false; document.body.style.userSelect = ''; document.body.style.cursor = ''; - localStorage.setItem(CHAT_W_KEY, String(Math.round(chatWidth))); + storageSet(CHAT_W_KEY, String(Math.round(chatWidth))); document.removeEventListener('mousemove', onMove); document.removeEventListener('mouseup', onUp); }; @@ -1103,11 +1096,11 @@ title={running if (e.key === 'ArrowLeft') { e.preventDefault(); chatWidth = clampChatWidth(chatWidth + 24); - localStorage.setItem(CHAT_W_KEY, String(Math.round(chatWidth))); + storageSet(CHAT_W_KEY, String(Math.round(chatWidth))); } else if (e.key === 'ArrowRight') { e.preventDefault(); chatWidth = clampChatWidth(chatWidth - 24); - localStorage.setItem(CHAT_W_KEY, String(Math.round(chatWidth))); + storageSet(CHAT_W_KEY, String(Math.round(chatWidth))); } }} >