Keep older saved games working when a game's save format changes. Versioned saves with a single version number inside the data, a chain of small migrations (v1 → v2 → v3) that never change after release, filling new fields from defaults, validation, backups of the original save, handling corrupt saves and saves from newer versions without destroying them, testing every old version, and telling ...
Download skill (.zip) Files on GitHubGame save migration
Players’ progress is sacred. Any change to the saved data’s shape needs a migration, not a reset.
Drop-in (tested)
import { createSaveSystem } from "./core/migrate.js";
const saves = createSaveSystem({
key: "star-hopper-save",
version: 3, // bump ONLY when the save format changes
defaults: () => ({ best: 0, unlocked: [1], settings: { music: 0.5, sfx: 0.8 }, coins: 0 }),
migrations: {
1: (s) => ({ ...s, unlocked: Array.from({ length: s.levelsBeaten + 1 }, (_, i) => i + 1) }), // v1 → v2
2: (s) => { const { volume, ...rest } = s; return { ...rest, settings: { music: volume ?? 0.5, sfx: volume ?? 0.8 } }; }, // v2 → v3
},
validate: (s) => Number.isFinite(s.best) && Array.isArray(s.unlocked),
});
const data = saves.load(); // saves.lastLoad.status: new | ok | migrated | corrupt | invalid | future
saves.save(data);
Behavior (all tested): saves from before versioning count as v1; migrations run in order; missing fields are filled from
defaults (nested too); the original is backed up (<key>.backup-v1) before the migrated save is written; corrupt or invalid
data falls back to defaults with a backup; a save from a newer version is never overwritten.
Rules
- Version inside the data (
{ v: 3, data: {…} }), one key. Not a new key per version. - Never edit a released migration. Add a new one. Old players may jump from v1 straight to v5.
- Migrations are small and pure: take the old object, return the new one. No storage access inside.
- Renames: copy the old field to the new name, then delete the old one in the same migration.
- Removals: keep data you might need again (inventory from a removed mode) under an
archivedfield. - Bump the save version separately from the game version (game-version-management).
Test every old version
Keep a real save from each released version in tests/saves/v1.json, v2.json… and test in node:
for (const v of [1, 2]) {
const raw = JSON.parse(readFileSync(`tests/saves/v${v}.json`, "utf8"));
const r = saves.migrate(raw);
ok(`v${v} migrates`, r.status === "migrated" && r.data.best === raw.best); // progress kept
}
Then run game-save-testing’s save-test.py --old-save tests/saves/v1.json in the browser.
When a reset is unavoidable
Tell players plainly on first load (“This update resets level progress. Your coins were kept.”), keep the backup key, and mention it under “Breaking” in the changelog.