Save and continue later

Restore a half-filled form after a reload with localStoragePlugin, and keep a server-side draft with autosavePlugin sending only what the visitor edited.

View as Markdown

Save and continue later

What it combines

localStoragePlugin and autosavePlugin on one two-step form:

  • localStoragePlugin — the visitor reloads or comes back tomorrow and the values are still there (Plugins)
  • autosavePlugin({ payload: 'dirty' }) — after a pause in typing, the server gets a draft with only the fields the visitor changed

Type in a field, wait a moment, then reload the page. The demo sends nothing: the autosave is replaced by a plugin that shows the body it would POST.

What the autosave would POST after 1.5 s idle (nothing was sent):

Type something…
import type { FormPlugin } from '@saastro/forms';
import { autosavePlugin, FormBuilder, localStoragePlugin, PluginManager } from '@saastro/forms';

interface Options {
  /** Where the dirty diff is POSTed. */
  endpoint?: string;
  /** Idle milliseconds before a save (a debounce, not a timer). */
  interval?: number;
  /** Replace the autosave plugin (the live demo swaps in one that sends nothing). */
  autosave?: FormPlugin;
}

export function createConfig({ endpoint = '/api/autosave', interval = 5000, autosave }: Options = {}) {
  const plugins = new PluginManager();
  // 1. Local resume: restores values as field defaults on reload, writes them
  //    (debounced) on every change and clears them after a successful submit.
  plugins.register(localStoragePlugin);
  // 2. Server draft: after `interval` ms without typing, POSTs only what the
  //    visitor edited — { formId, dirtyPaths, values }.
  plugins.register(autosave ?? autosavePlugin({ endpoint, interval, payload: 'dirty' }));

  return FormBuilder.create('save-and-continue')
    .layout('manual')
    .columns(12)
    .addField('name', (f) => f.type('text').label('Full name').required().columns({ default: 12 }))
    .addField('email', (f) =>
      f.type('email').label('Email').required().email().columns({ default: 12 }),
    )
    .addField('password', (f) =>
      f.type('password').label('Password').required().columns({ default: 12 }),
    )
    .addField('story', (f) =>
      f.type('textarea').label('Tell us your story').optional().rows(5).columns({ default: 12 }),
    )
    .addStep('about', ['name', 'email', 'password'], { defaultNext: 'story' })
    .addStep('story', ['story'])
    .initialStep('about')
    .usePlugins(plugins)
    .build();
}

The code

The Code tab above is the whole recipe: createConfig({ endpoint, interval }) returns the form config with both plugins registered. Render it as usual:

import { Form } from '@saastro/forms';
import { createConfig } from './recipe';

const config = createConfig({ endpoint: '/api/drafts', interval: 5000 });

export function ApplyForm() {
  return <Form config={config} components={uiComponents} />;
}

With <HubForm>, pass the same plugins through its plugins prop instead.

How it works

  • Restore — localStoragePlugin reads form-<formId> when the config is built and uses the saved values as field defaults. Writes are debounced (300 ms) and the entry is removed after a successful submit.
  • Draft — autosavePlugin restarts its timer on every change and POSTs once the visitor has been idle for interval ms (60 000 by default).
  • payload: 'dirty' — the body is { formId, dirtyPaths, values }, where values holds only the top-level fields the visitor edited. Your endpoint can PATCH a draft instead of overwriting it. Nothing is sent when nothing is dirty. The default, 'all', posts every value.
  • Sensitive fields — password, otp, file and signature fields are never written to localStorage and are skipped on restore.

Caveats

  • Unauthenticated endpoint — autosavePlugin posts from the browser, so the endpoint must identify the draft itself (a session cookie or a token in the URL). Do not trust formId alone.
  • Autosave values are not filtered — only localStorage drops sensitive fields. The autosave body includes every dirty field, passwords too, so keep such fields out of a form you autosave or strip them server-side.
  • One form at a time — localStoragePlugin is a single shared instance: its save timer and the cleanup after submit are not per form, so don’t mount two forms with it on the same page.
  • Shared device — localStorage outlives the tab. Anything the visitor typed stays on the machine until they submit.

Variations

  • Use autosavePlugin({ endpoint, interval: 30000 }) without payload to post the full values every 30 s of idle time.
  • Drop localStoragePlugin when the draft lives only on the server and you load it yourself as the form’s initial values.
  • Combine with Multi-step lead form with a webhook to save progress between steps.