Save Manager
Saves game data in the browser, with save slots, version upgrades for old saves, and a safe fallback when storage is blocked or a save is damaged.
Files: save-manager.js (the component, no dependencies) and save-manager.test.mjs (7 tests).
Copy save-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createSave } from "./save-manager.js";
const save = createSave({
key: "my-game", version: 2,
defaults: { level: 1, coins: 0, unlocked: [] },
migrations: { 2: (d) => ({ ...d, unlocked: d.unlocked ?? [] }) }, // turns a version 1 save into version 2
});
const data = save.load(); // defaults if there's no save
data.coins += 10; save.save(data);
API
createSave({ key, version, defaults, migrations, storage }) → savesave.load(slot = "main")the saved data, upgraded to the current version, or a copy of defaultssave.save(data, slot)returns true if stored, false if storage failed (data stays in memory for this visit)save.has(slot) / save.clear(slot) / save.slots()slots() lists { slot, savedAt, version }save.export(slot) → stringa code the player can copy; save.import(code, slot) → true/falsesave.persistentfalse when the browser blocks storage (private mode, some embeds)save.lastErrorwhy the last load or save failed, or null
Damaged saves are kept under “
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes, no engine code involved. Works inside itch.io and Bash.tv iframes; falls back to memory if storage is blocked. |
Tests
Node tests: defaults, round trip, slots, migrations in order, newer-version saves, damaged saves kept, blocked storage, export and import.
Run them with node --test toolkit/components/save-manager/ (Node 20 or newer, nothing to install).