Sub topic 10
Saving drafts and resuming later
This chapter assumes you know basic HTML forms, JavaScript or TypeScript, and how browsers submit form data. You will learn how to preserve a partially completed multi-step form without making stale or invalid data part of the final submission.
A draft has two main parts:
- The current step.
- The values entered so far.
It should usually not contain temporary display state such as an old error message, a loading flag, or whether a field currently has focus. Rebuild that state when the draft loads.
Choose where the draft lives
The storage choice depends on how long the draft should survive and whether the user needs it on another device.
| Storage | Survives a tab close | Works across devices | Typical use |
|---|---|---|---|
| `sessionStorage` | No | No | A short flow in one tab |
| `localStorage` | Yes | No | A form resumed on the same browser |
| Server-side draft | Yes | Yes | Account-based or long-running applications |
sessionStorage is scoped to one browser tab. It is useful when retaining data after a refresh matters, but leaving a form open overnight should not create a permanent local record.
localStorage persists until your application removes it. Add an expiration time and a clear-draft action. Do not store passwords, payment card data, identity documents, or other sensitive values in browser storage. A server-side draft needs authentication, authorization, encryption, and an explicit retention policy.
Save a small, versioned payload
Save after a meaningful change, such as a field change or step transition. A short debounce prevents writing on every keystroke.
Include a schema version so future releases can reject or migrate old drafts. Store only fields that the current flow needs.
const DRAFT_KEY = "signup-draft";
const DRAFT_VERSION = 1;
type Draft = {
version: number;
savedAt: string;
step: string;
values: Record<string, string>;
};
function saveDraft(step: string, values: Record<string, string>): void {
const draft: Draft = {
version: DRAFT_VERSION,
savedAt: new Date().toISOString(),
step,
values,
};
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(draft));
}
function loadDraft(): Draft | null {
const raw = sessionStorage.getItem(DRAFT_KEY);
if (!raw) return null;
try {
const draft: unknown = JSON.parse(raw);
if (
typeof draft !== "object" ||
draft === null ||
(draft as Draft).version !== DRAFT_VERSION ||
typeof (draft as Draft).step !== "string" ||
typeof (draft as Draft).values !== "object"
) {
return null;
}
return draft as Draft;
} catch {
return null;
}
}The catch handles malformed JSON. The validation before the cast matters because parsing JSON does not prove that the result has the shape your application expects.
Restore deliberately
When the page opens, load the draft only after confirming that its step is still valid. If the flow changed between releases, migrate the draft or start fresh. Show a clear choice such as “Resume saved application” and “Start over” instead of silently replacing the user’s current input.
When the user completes the flow:
- Send the active values to the server.
- Wait for a confirmed success response.
- Remove the local draft.
- Show the success state.
Do not clear the draft when the request begins. A network failure should leave the user able to retry.
Make resume behavior visible
Tell the user when the draft was saved and provide a way to discard it. If a draft may contain private information, explain where it is stored and how long it remains. When the user changes an answer that controls conditional fields, remove values from fields that are no longer active before saving.
Exercise
Why should a form keep its draft after a failed submission but clear it after a successful submission?
Show answer
A failed submission may be caused by a network or server problem, so keeping the draft prevents data loss and allows a retry. A successful submission means the draft is no longer needed, so clearing it prevents stale data from being restored later.