Skip to content
Back to skills

Content Backup

ASecurity

The admin content form keeps a backup of the unsaved changes in the browser. A crash, a closed tab or a failed save does not lose the work. When the user opens the item again, the form offers to restore the changes. The plan is `.claude/plan/autosave.md` of the workspace. This skill covers the browser backup and the recovery UI. It builds on the [admin view chrome](../admin-view-chrome/SKILL.md) ("Unsaved changes", "The form context") and the [content model](../content-model/SKILL.md).

  • 4 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added November 7, 2026
developmentgovuegitapibackendsecurity

Works with

  • cli
  • api

Security analysis

A100/100

Scanned November 7, 2026

npx -y skills add plutocms/pluto --skill content-backup --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Content Backup?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Content Backup
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/plutocms-content-backup/badge)](https://www.skillsdirectory.com/skills/plutocms-content-backup)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Content backup

The admin content form keeps a backup of the unsaved changes in the browser. A crash, a closed
tab or a failed save does not lose the work. When the user opens the item again, the form offers
to restore the changes. The plan is `.claude/plan/autosave.md` of the workspace.

This skill covers the browser backup and the recovery UI. It builds on the
[admin view chrome](../admin-view-chrome/SKILL.md) ("Unsaved changes", "The form context") and the
[content model](../content-model/SKILL.md).

## What it is, and what it is not

- A **backup**, not an autosave. The Save and Publish buttons stay the only writes of content. A
  backup never touches the backend: no commit, no revision, no deploy, no live text.
- The word in code and UI is "backup". The form already has "Save draft" and a "Draft" status, so
  "draft" would mix with them.
- The browser backup works the same on every backend. A server copy (below) needs the optional
  `PlutoContentAdapter.backups`: `@plutocms/supabase` will have it, the Git CMS does not.

## The record

One `localStorage` entry for each app base path, user, type and item:
`'pluto:backup:' + JSON.stringify([baseURL, userId, type, id ?? null])`. A new item has the id
`null`. `baseURL` is `useRuntimeConfig().app.baseURL`.

```ts
interface PlutoContentBackup {
  v: 1
  savedAt: number // ms, the time of the last change (browser clock)
  device: string // the id of this browser (`pluto:device`, kept after sign-out)
  tab: string // a random id for each form mount
  version?: string // the `_version` of the item at the baseline
  slugTouched?: boolean // new item: the user typed in the slug field
  changes: Record<string, { base?: unknown; value?: unknown }>
}
```

`changes` holds only the fields that differ from the baseline: `base` is the baseline value and
`value` is the form value. The backup fields are the declared fields with `inForm !== false`,
except the status field (`backupFields`). Virtual fields and the slug are in. The record has no
`_version`, `_frontmatter`, id or timestamps.

## Files

- `shared/utils/content-backup.ts` (pure, no Nuxt imports): the record type, `backupFields`,
  `backupChanges`, `parseBackup`, `planRestore`, `pickBackup`, `keepaliveBody`, and the limits
  (`PLUTO_BACKUP_*`). `pickBackup`, `needsServerCopy` and `keepaliveBody` serve the server copy.
- `shared/utils/stable-stringify.ts`: `stableStringify`, moved out of `content-dirty.ts`. A module
  of `app/composables/internal` cannot import a runtime value from `shared/` (a layer build cannot
  resolve it, pluto#433). Use it by auto-import, or pass it in.
- `app/composables/internal/content-backup-store.ts` (pure): `backupKey`, `isBackupKeyOf`,
  `createBackupStore(storage, limits)`, `createBackupScheduler`, `deviceId`, `clearUserBackups`.
  It takes a `Storage`-like object, timers and the `parse` function as parameters.
- `app/composables/internal/content-backup-sync.ts` (pure): `createBackupSync`, the rules of the server
  requests, and `errorStatus`. Test: `test/content-backup-sync.test.ts`.
- `app/composables/pluto-content-backup-server.ts`: `usePlutoContentBackupServer`, the `server` slot with
  `$fetch` and the `keepalive` `fetch`.
- `server/utils/pluto-content-handlers.ts`: `getContentBackup`, `putContentBackup`, `removeContentBackup`,
  `removeBackupQuietly`. Routes: `server/api/_pluto/content-backup/[type].{get,put,delete}.ts`.
- `app/composables/pluto-content-backup.ts`: `usePlutoContentBackup(options)`, the Vue glue.
- `app/components/Pluto/Content/PlutoContentBackupAlert.vue`: the offer.
- `app/components/Pluto/Content/PlutoContentChanges.vue`: the diff list, shared with History.
- `shared/utils/content-revision.ts`: `diffFields(fields, current, next)`.
  `diffRevision` calls it.

Tests (`test/content-backup.test.ts`, `test/content-backup-store.test.ts`) use a fake storage and
fake timers. No DOM, no Nuxt.

## When the form writes

A write needs all of these: the item loaded, the offer decided, the form is dirty, the user can
save, a user id exists, `type.form.backup` is not `false`, the state is `idle`, and the browser did
not refuse a write.

- 1 s after the last change, or every 5 s at most during continuous typing
  (`createBackupScheduler`).
- Synchronously on `pagehide` and on `visibilitychange` to `hidden`.
- Before each write the form runs the `onFlush` callbacks of the widgets, so an editor with a
  debounce writes its text to the item first.
- When the form is not dirty any more (the user undid the change), the backup is removed.

## The states

- `idle`: backups write.
- `offered`: the alert shows. Writes pause, so the old backup stays until the user chooses. The
  state ends on Restore, Discard, "Save and discard", and when `planRestore` finds nothing to
  restore (the backup is removed).
- `stopped`: after "Leave without saving", a delete, or a save that navigates (a new item or an id
  change). A stopped form writes nothing, so the unmount flush of an editor cannot write a removed
  backup again.

## The offer

When the item loads (at once for a new item), the form reads the record, drops the fields that the
type does not declare, and runs `planRestore` against the baseline (`JSON.parse(dirtyState.baseline)`,
so it follows the normal form that an editor writes after it mounts).

If a field differs, `PlutoContentBackupAlert` shows above the fields (color `warning`, icon
`lucide:triangle-alert`):

- Title "Unsaved changes found". Text "Changes from {time} were not saved." The text for a record from
  another device ("Changes from another device, from {time}, were not saved.") is in the locale
  files and in the alert. The form shows it when the record
  comes from the server copy and its `device` is not this browser.
- **Restore** sets `slugTouched` (new item), calls `tracker.interact()` (so the baseline stays and
  the form is dirty), puts the backup `value` of each changed field in the form, and shows a toast.
  A field that only the stored item changed keeps the stored value. A field that changed in both
  places (the stored value is not the backup `base`) takes the backup value, and the alert named it
  before: "The item was saved again after that. These fields changed in both places: {fields}."
- **Discard** removes the backup.
- A **save while the offer is open** (the Save, Save draft, Publish and Unpublish buttons, and Ctrl+S) does
  not save at once. A dialog asks "Discard the unsaved changes?" with Cancel (nothing happens),
  Restore (runs Restore, no save) and "Save and discard" (removes the backup, then saves).
  Escape and a click outside mean Cancel. If the save then fails, the backup is already gone: keep
  it simple. With no offer open, a save works as before.
- **Show changes** opens a slideover with `PlutoContentChanges` ("Saved" against "Backup"), the
  same diff as History, and the Restore and Discard buttons.

A second alert (warning, closable) shows when another tab wrote the backup of this item (a
`storage` event with another `tab`): "This item has unsaved changes in another tab. The tab that
saves last wins." There is no lock.

## The 409 conflict

An adapter with a `_version` token (the Git CMS) answers a save of a stale item with a 409 and the
key `pluto.errors.conflict`. `errorKey(error)` (`internal/error-text.ts`, for a `$fetch` error and a
Nuxt error) tells it from a taken slug (`pluto.github.errors.slugTaken`), which keeps its error
toast. On the conflict key the form calls `onConflict()` of `usePlutoContentBackup`:

- It writes the browser backup now (and sends the server copy at once with `server.write`), and sets
  `conflict`. A warning `UAlert` shows above the fields instead of the toast: "Someone saved this
  item after you opened it. Your changes are safe." with the button "Load the latest and keep my
  changes".
- The button runs `reloadAndMerge()`: it writes and reads the backup record, calls `refresh()`
  (the `reload` option), resets the dirty baseline two times as a save does (the `resetBaseline`
  option: now and after `nextTick`), runs `planRestore` at once (no offer), calls
  `tracker.interact()` and puts the backup values on the new item. A toast lists the fields that
  changed in both places (the backup wins there). It then clears `conflict`, and the form resets
  the "Save failed" status. The next save carries the new `_version`.
- While it merges, writes pause (`merging`), so the reset baseline cannot remove the backup.
- If the refresh fails, the backup stays, the toast "Could not load this item" shows, and the
  conflict alert stays.
- A save that works, "Leave without saving" and a delete clear `conflict`.
- Supabase returns no `_version`, so this flow never shows there.

## Save, delete, leave

- A save that worked removes the backup of the item (and the `+new` key of a new item). A rename
  removes the key of the old id before the form navigates.
- A failed save keeps the backup.
- A delete that worked removes it.
- "Leave without saving" removes it, so the text of the dialog stays true.
- A tab close or a reload keeps it. That is the recovery case.

## Limits

One backup holds at most 1,000,000 characters of JSON, all backups together at most 2,500,000, so
the other keys of the origin still fit. A larger change is not backed up (a dev-only warning shows).
Each user has at most 20 backups: the oldest `savedAt` goes first. A backup expires after 14 days.
Pruning runs when a form mounts. On a quota error, the store makes room (the expired backups, then
the oldest backup of the user) and tries once more. If the second write fails too, the form stops
backups for the page view, with no message. The store catches every storage error (private mode,
`SecurityError`).

## Sign-out, privacy, opt-out

- `usePlutoAuth().signOut()` reads the user id first. After the driver signs out, it removes every
  backup of that user under this base path (`clearUserBackups`). A session that expires keeps the
  backups, so the same user gets them back after the next sign-in. With no user id, the form reads
  and writes nothing.
- Backups are plain text in the browser profile. The mitigations: a key for each user, removal on
  sign-out, a 14-day expiry, and the opt-out.
- `PlutoContentType.form.backup?: boolean` (default `true`). `false` turns the backups off for the
  type. There is no app-wide switch.
- In development, two apps on `localhost:3000` share `localStorage`. The same user, type and id can
  give an offer from the other app. The user discards it.

## The server copy

An adapter with `backups` (content-model skill, "Backups") keeps a copy of the unsaved changes for each
user, type and item. It gives recovery on another device. The record is the same as the browser one. The
`server` slot of `usePlutoContentBackup` is `usePlutoContentBackupServer(typeName)` (`read`, `write`, `remove`,
`keepalive`, `forget`, `settled`). The key is the item id as text, or `+new`.

- **Open.** The form reads the browser backup, then `GET /api/_pluto/content-backup/<type>?id=<id>` (3 s at
  most). `pickBackup` takes the newer `savedAt`, and a tie goes to the browser. A GET that fails gives the
  browser backup only. A record with another `device` shows the "another device" text. `read` gives
  `{ record }` when the server answered (`record` is `null` for no copy), and `null` for no answer.
- **Send an offered browser backup.** The form writes nothing while it offers a backup. So when the form
  offers its browser backup and the server answered with no copy or an older one (`needsServerCopy`), the
  form sends that record at once. A browser backup from before the server kept copies, or from a time
  when the server was down, then reaches the other devices. With no answer, nothing goes: a failed read
  must not replace a newer copy of another device.
- **Not supported.** The Git CMS has no `backups`. The GET answers `{ supported: false }`, and then this page
  view sends no PUT and no DELETE.
- **Write.** 10 s after the last change, 30 s at most (`createBackupScheduler`). No write while
  `navigator.onLine` is false: the `online` event schedules one. A record equal to the last one sent is
  skipped (the time and the tab id do not count). One write at a time. A clean form sends nothing and keeps the
  server copy until a save, Discard or Leave.
- **Stop.** A 401, 403 or 413 stops the server copy for the page view. Another error tries again with the next
  change. A request never throws into the form.
- **Hide.** On `pagehide` and `visibilitychange` to hidden, one `fetch(..., { method: 'PUT', keepalive: true })`
  when the body is 60,000 bytes or less (`keepaliveBody`). A larger body relies on the last timed write.
- **Save.** Before `save`, the form calls `prepareSave()`: it cancels the server timer and waits at most 2 s for
  a write in progress. The core write handlers remove the copy of the caller after the save, so the form sends no
  DELETE then. A copy that stays (a removal error) is stale: `planRestore` finds nothing to restore and removes it.
- **Discard and Leave** send a DELETE. The 409 flow sends the server copy at once.
- **Other device.** A PUT answer `{ otherDevice: true }` shows a closable warning: "This item has unsaved changes
  on another device. The device that saves last wins."
- **Privacy.** The route needs the write capability of the type. The adapter returns only the copy of the caller.
  Sign-out keeps the server copy, so the user gets it back after the next sign-in.

## Known limits

- A widget that holds edits outside its `modelValue` must register `onFlush`
  (`usePlutoContentForm()?.onFlush(...)`). The core widgets need nothing.
- Edits made while the offer shows are not backed up. The leave guard still protects them.
- An item that is gone (404) keeps its backup until it expires. There is no "Create from backup".

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.