feat: release 1.5.3 - fix the SSR localStorage crash on Node 22+
Patch the app-breaking regression 1.5.2 shipped: every server render threw `localStorage.getItem is not a function`, so the UI only appeared behind a Vite error overlay. Node 22+ exposes a `localStorage` global that is not a Storage when no valid `--localstorage-file` is set, which defeats the `typeof localStorage` guard the language layer used. The code fix landed in 3363620; this commit cuts it as a release and adds `npm test`, which fails if the unsafe idiom reappears. Docs: docs/changelogs/2026-09-16-ssr-storage-fix.md
This commit is contained in:
@@ -4,4 +4,4 @@
|
||||
# reads this; pyproject.toml is kept in sync for the build (hatchling dynamic
|
||||
# version reads this file). PR #47: hard-coded literals drifted twice
|
||||
# (1.3.2 shipped reporting 1.2.1; 1.4.1's /api/health still said 1.4.0).
|
||||
__version__ = "1.5.2"
|
||||
__version__ = "1.5.3"
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "calliope-web",
|
||||
"version": "1.5.2",
|
||||
"version": "1.5.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "calliope-web",
|
||||
"version": "1.5.2",
|
||||
"version": "1.5.3",
|
||||
"dependencies": {
|
||||
"@tanstack/svelte-query": "^5.66.0",
|
||||
"@types/three": "0.185.0",
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "calliope-web",
|
||||
"version": "1.5.2",
|
||||
"version": "1.5.3",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||
"test": "node scripts/check-storage-ssr.mjs",
|
||||
"lint": "prettier --check . && eslint .",
|
||||
"format": "prettier --write ."
|
||||
},
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Regression guard for the "localStorage.getItem is not a function" SSR crash.
|
||||
*
|
||||
* Node 22+ exposes a global `localStorage`, and when it is enabled without a
|
||||
* usable `--localstorage-file` the binding is a plain object with no Storage
|
||||
* methods. `typeof localStorage === 'undefined'` therefore evaluates false, the
|
||||
* next call throws, and every server render dies. It only surfaces at module or
|
||||
* component scope, because that is what SvelteKit also evaluates on the server.
|
||||
*
|
||||
* Two layers of protection:
|
||||
* 1. behavioural — the helper must degrade to null under every storage shape
|
||||
* the server can present, and still round-trip against a real Storage.
|
||||
* 2. static — the unsafe `typeof localStorage` idiom is banned outside the
|
||||
* helper, so the crash cannot be reintroduced by copying the old pattern.
|
||||
*
|
||||
* Run with `npm test` (or `node scripts/check-storage-ssr.mjs`).
|
||||
*/
|
||||
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
||||
import { dirname, join, relative } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||
const ROOT = join(HERE, '..');
|
||||
const SRC = join(ROOT, 'src');
|
||||
const HELPER = join(SRC, 'lib', 'storage.ts');
|
||||
|
||||
let failures = 0;
|
||||
|
||||
function fail(message) {
|
||||
failures += 1;
|
||||
console.error(`FAIL ${message}`);
|
||||
}
|
||||
|
||||
function ok(message) {
|
||||
console.log(`ok ${message}`);
|
||||
}
|
||||
|
||||
/** Replace the global under test. Node's own binding may be getter-only. */
|
||||
function setGlobalStorage(value) {
|
||||
Object.defineProperty(globalThis, 'localStorage', {
|
||||
value,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
|
||||
/** A Storage that behaves like a browser's. */
|
||||
function fakeStorage(initial = {}) {
|
||||
const map = new Map(Object.entries(initial));
|
||||
return {
|
||||
getItem: (k) => (map.has(k) ? map.get(k) : null),
|
||||
setItem: (k, v) => void map.set(k, String(v)),
|
||||
removeItem: (k) => void map.delete(k),
|
||||
clear: () => map.clear(),
|
||||
key: (i) => [...map.keys()][i] ?? null,
|
||||
get length() {
|
||||
return map.size;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const { storageGet, storageSet, storageRemove } = await import(pathToFileURL(HELPER).href);
|
||||
|
||||
// ---- 1. behavioural ----------------------------------------------------
|
||||
|
||||
// The exact shape Node presents on the server: an object with no Storage methods.
|
||||
// This is the configuration that produced the original crash.
|
||||
setGlobalStorage({});
|
||||
try {
|
||||
if (storageGet('calliope-lang') !== null) fail('method-less localStorage: expected null');
|
||||
else if (storageSet('calliope-lang', 'ja') !== undefined) fail('method-less localStorage: setItem should no-op');
|
||||
else {
|
||||
storageRemove('calliope-lang');
|
||||
ok('method-less localStorage (Node SSR shape) degrades to null without throwing');
|
||||
}
|
||||
} catch (err) {
|
||||
fail(`method-less localStorage threw: ${err.message}`);
|
||||
}
|
||||
|
||||
// Absent entirely (the common server case).
|
||||
setGlobalStorage(undefined);
|
||||
try {
|
||||
if (storageGet('k') !== null) fail('undefined localStorage: expected null');
|
||||
else ok('absent localStorage degrades to null');
|
||||
} catch (err) {
|
||||
fail(`undefined localStorage threw: ${err.message}`);
|
||||
}
|
||||
|
||||
// Access itself can throw (sandboxed frame / blocked third-party storage).
|
||||
setGlobalStorage(
|
||||
new Proxy(
|
||||
{},
|
||||
{
|
||||
get() {
|
||||
throw new Error('SecurityError');
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
try {
|
||||
if (storageGet('k') !== null) fail('throwing localStorage: expected null');
|
||||
else ok('throwing localStorage access degrades to null');
|
||||
} catch (err) {
|
||||
fail(`throwing localStorage access threw: ${err.message}`);
|
||||
}
|
||||
|
||||
// Quota / private-mode errors on write must not escape.
|
||||
setGlobalStorage({
|
||||
getItem: () => null,
|
||||
setItem: () => {
|
||||
throw new Error('QuotaExceededError');
|
||||
},
|
||||
removeItem: () => {},
|
||||
});
|
||||
try {
|
||||
storageSet('k', 'v');
|
||||
ok('setItem failure is swallowed');
|
||||
} catch (err) {
|
||||
fail(`setItem failure escaped: ${err.message}`);
|
||||
}
|
||||
|
||||
// A real browser Storage must still work — guards against a helper that just
|
||||
// returns null unconditionally to satisfy the checks above.
|
||||
setGlobalStorage(fakeStorage());
|
||||
try {
|
||||
storageSet('calliope-lang', 'ko');
|
||||
const read = storageGet('calliope-lang');
|
||||
if (read !== 'ko') fail(`working storage round-trip: expected "ko", got ${JSON.stringify(read)}`);
|
||||
else {
|
||||
storageRemove('calliope-lang');
|
||||
if (storageGet('calliope-lang') !== null) fail('working storage remove: expected null');
|
||||
else ok('working storage round-trips and removes');
|
||||
}
|
||||
} catch (err) {
|
||||
fail(`working storage threw: ${err.message}`);
|
||||
}
|
||||
|
||||
// ---- 2. static ---------------------------------------------------------
|
||||
|
||||
const UNSAFE = /typeof\s+localStorage/;
|
||||
const EXTENSIONS = ['.ts', '.js', '.svelte'];
|
||||
|
||||
function walk(dir) {
|
||||
const found = [];
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (entry.name === 'node_modules') continue;
|
||||
found.push(...walk(full));
|
||||
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext))) {
|
||||
found.push(full);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function exists(p) {
|
||||
try {
|
||||
return statSync(p).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const offenders = [];
|
||||
for (const file of walk(SRC)) {
|
||||
if (file === HELPER) continue; // the one place allowed to probe the global
|
||||
const lines = readFileSync(file, 'utf8').split('\n');
|
||||
lines.forEach((line, i) => {
|
||||
if (UNSAFE.test(line)) offenders.push(`${relative(ROOT, file)}:${i + 1}: ${line.trim()}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (offenders.length) {
|
||||
fail(
|
||||
`unsafe \`typeof localStorage\` guard outside lib/storage.ts — use storageGet/storageSet instead:\n ${offenders.join('\n ')}`,
|
||||
);
|
||||
} else {
|
||||
ok('no unsafe `typeof localStorage` guards in src/');
|
||||
}
|
||||
|
||||
if (!exists(HELPER)) fail('src/lib/storage.ts is missing');
|
||||
|
||||
// ---- result ------------------------------------------------------------
|
||||
|
||||
if (failures) {
|
||||
console.error(`\n${failures} storage/SSR check(s) failed.`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('\nstorage SSR checks passed.');
|
||||
@@ -0,0 +1,46 @@
|
||||
# Calliope 1.5.3 — fix the "localStorage.getItem is not a function" crash on Node 22+
|
||||
|
||||
1.5.2 could fail to render at all. The page came up behind a Vite error overlay
|
||||
reading `TypeError: localStorage.getItem is not a function`, with the stack
|
||||
pointing at `initialLanguage()` in `lib/i18n.svelte.ts`.
|
||||
|
||||
A patch rather than a feature: the language layer was correct, but its storage
|
||||
read was not safe to run on the server.
|
||||
|
||||
## What changed
|
||||
|
||||
- **The guard was wrong, not just incomplete.** The code sampled the global's
|
||||
existence:
|
||||
|
||||
```ts
|
||||
if (typeof localStorage === 'undefined') return 'en';
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
```
|
||||
|
||||
That is safe in a browser, but Node 22+ ships a web-storage global, and when
|
||||
it is enabled without a usable `--localstorage-file` the binding is a plain
|
||||
object with **no Storage methods**. The `typeof` check therefore passes and
|
||||
the next line throws. Presence is not the same as usability.
|
||||
- **New `lib/storage.ts`** probes for a callable method instead of trusting the
|
||||
binding to exist, and falls back to `null` for every shape the server can
|
||||
present — absent, method-less, and throwing-on-access (sandboxed frames,
|
||||
blocked storage). Write failures (private mode, quota) are swallowed.
|
||||
- **Only module- and component-scope reads were affected.** SvelteKit evaluates
|
||||
those on the server too, which is why `i18n.svelte.ts` broke the whole app.
|
||||
The same guard inside `onMount` / `$effect` never runs on the server and was
|
||||
harmless — worth knowing, since the idiom appears throughout the codebase.
|
||||
- **A second latent crash fixed.** The canvas page's `railCollapsed` /
|
||||
`chatCollapsed` `$state` initializers had the identical flaw and would have
|
||||
failed as soon as the first one was fixed.
|
||||
- **Regression guard** — `npm test` runs `scripts/check-storage-ssr.mjs`, which
|
||||
exercises the helper against all four storage shapes and fails the build if
|
||||
the unsafe `typeof localStorage` idiom reappears anywhere in `src/`.
|
||||
|
||||
Verified by reverting to the previous version: `/build-scene` returned
|
||||
`HTTP 500 Internal Error` before the fix and `HTTP 200` with full server-rendered
|
||||
output after it. `npm run check` and `npm run build` both pass.
|
||||
|
||||
## Upgrade
|
||||
|
||||
No migration and no new dependencies. `git pull` and reload. If you were seeing
|
||||
the error overlay, it will be gone — no need to change your Node version.
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
Dated notes for GitHub releases. Newest first.
|
||||
|
||||
- [2026-09-16 SSR storage fix](./2026-09-16-ssr-storage-fix.md) — fix the `localStorage.getItem is not a function` crash on Node 22+; new `npm test` guard (1.5.3)
|
||||
- [2026-09-16 multilingual UI](./2026-09-16-multilingual-ui.md) — seven-language UI (en/zh/es/fr/de/ja/ko); header language switcher; correct backend version; local start scripts (1.5.2)
|
||||
- [2026-09-13 clip shot brief](./2026-09-13-clip-shot-brief.md) — per-clip brief in the Video composer; two-column Video stage (1.5.1)
|
||||
- [2026-09-10 Build Scene + scene → clips](./2026-09-10-build-scene-clips.md) — 3D blockout composer; vision/doc input; 1 scene : N shot clips; faithful scripts (1.5.0)
|
||||
|
||||
Reference in New Issue
Block a user