Bash.tv → GitHub → itch.io

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

Damaged saves are kept under “::broken" so nothing is lost.

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).