Sub topic 10
Saving drafts and resuming later
This chapter assumes you know the FlowState type from earlier chapters: a current step id, a history stack, collected data, and errors. It also assumes you can read basic TypeScript and know how forms hold values.
Long flows get interrupted. A user closes the tab, loses signal, or needs a document from another room. If the flow loses their input, many will not start again. Saving a draft lets them come back where they stopped.
Decide what to save
Not all of FlowState belongs in storage. Save the parts that cannot be rebuilt:
data: the answers collected so far.current: the step the user was on.history: the steps they passed through, so Back still works after resuming.
Do not save errors. They can be recomputed by validating the current step, and a stale error message on a restored screen is confusing.
Add a version number and a timestamp. The version lets you detect drafts written by an older flow definition. The timestamp lets you expire old drafts.
type Data = Record<string, string>;
type FlowState = {
current: string;
history: string[];
data: Data;
errors: string[];
};
type Draft = {
version: number;
savedAt: number;
current: string;
history: string[];
data: Data;
};
const DRAFT_VERSION = 1;
function toDraft(state: FlowState, now: number): Draft {
return {
version: DRAFT_VERSION,
savedAt: now,
current: state.current,
history: state.history,
data: state.data,
};
}Choose where to save
The storage location decides who can resume and on which device.
| Location | Survives reload | Works across devices | Notes |
|---|---|---|---|
| Memory only | No | No | Lost on any refresh. |
| Browser storage | Yes | No | Simple. Readable by anyone using that browser profile. |
| Server | Yes | Yes | Needs an account or a draft token. |
Use browser storage for short flows with low-risk data. Use the server when the flow is long, when users switch devices, or when the data is sensitive. Never store passwords, payment card numbers, or similar secrets in a draft.
Save and load safely
Reading a draft is a trust boundary. The stored text may be corrupted, edited, or written by an older version of your code. Parse it defensively and check that the saved step still exists before using it.
type Storage = {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
};
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
function saveDraft(store: Storage, key: string, state: FlowState): void {
store.setItem(key, JSON.stringify(toDraft(state, Date.now())));
}
function loadDraft(
store: Storage,
key: string,
stepIds: string[],
now: number,
): FlowState | null {
const raw = store.getItem(key);
if (raw === null) return null;
let draft: Draft;
try {
draft = JSON.parse(raw) as Draft;
} catch {
store.removeItem(key);
return null;
}
const valid =
draft.version === DRAFT_VERSION &&
now - draft.savedAt <= MAX_AGE_MS &&
stepIds.includes(draft.current) &&
draft.history.every((id) => stepIds.includes(id));
if (!valid) {
store.removeItem(key);
return null;
}
return {
current: draft.current,
history: draft.history,
data: draft.data,
errors: [],
};
}
function clearDraft(store: Storage, key: string): void {
store.removeItem(key);
}Discarding an invalid draft is the safe default. A more careful flow could migrate old versions, but only do that when losing the draft would cost users real work.
Decide when to save
You have two common choices.
- Save when the user advances. This is simple and saves only data that passed validation. The user can lose the answers on the step they were typing.
- Save on every change, with a short delay. This loses almost nothing, but the draft may hold invalid or half-typed values.
Both work. If you save invalid values, remember that a restored step has not been validated. Validate it again when the user presses Next, as usual. Never let a restored draft skip validation, because the draft may have been edited or the rules may have changed since it was saved.
Resume and clean up
When the flow starts, check for a draft. If one exists, tell the user and offer a choice: continue, or start over. Restoring silently can surprise someone who expected a blank form on a shared computer.
Delete the draft after a successful submission. If you skip this step, the next visit offers to resume a flow that is already finished.
Branching adds one more check. A draft may hold data for a branch the user later abandoned, such as a company name after switching from a business to a personal account. Keep the data if you want Back to feel natural, but make sure the submission step only sends answers on the path the user actually finished.
Exercise
Exercise
A user resumes a draft saved two weeks ago, and the flow now has a new required step called "phone" that did not exist then. Which fields in the draft should you check on load, and what should happen if the check fails?
Show answer
Check the version field, since the flow definition changed, and check that the saved current step and every id in history still exist in the current step list. Also check savedAt against the expiry. If any check fails, delete the draft and start at the first step, or migrate the draft if losing it would cost real work. In either case, the new "phone" step is validated normally when the user reaches it, because a restored draft never skips validation.