DocsForms & drafts
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Forms & drafts

draftRecovery()

Restore a form after an interruption.

Try the example

Write a sample project title and note, save the draft, then clear the editor. Restore brings your text back from this tab’s session storage. Delete draft removes the stored copy.

Project note

No changes saved in this example yet.

Actual npm package · local demonstration

Add it to your project

  1. Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
  2. Connect the behavior. Copy the JavaScript tab and call its exported mount(element) after your component’s elements exist. Replace simulated delays and data operations with your own application code.
  3. Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.

API and lifecycle

Create an instance with a user/form-specific key and an explicit fields allowlist. save(data) queues a sessionStorage write; load() returns allowed values or null when no valid draft exists. Defaults: 24-hour TTL, 300 ms delay and version 1.

Interface
key, fields allowlist, ttl, delay, version; load/save/flush/clear/destroy
Lifecycle
flush() persists pending data immediately. clear() removes the draft. destroy() flushes pending data and removes the pagehide listener. Clear after a confirmed save and on logout; change version when the form shape changes.

Accessibility and safety

Implementation notes

Store only explicit non-sensitive fields. Never persist passwords, payment details or access tokens. Session storage is available to same-origin scripts and may be blocked or unavailable. Clear drafts on logout and after a confirmed save.

What to check

Save allowed and unlisted fields, then restore and verify only the allowlisted fields return. Check expired drafts, version changes, blocked storage and a confirmed save.