On State as a Promise

A three-part session recovery architecture: Cloudflare D1 persistence, an email-cron-worker, and sequenced React hydration, built on the insight that a promise to the user is a contract.

On State as a Promise
If you promise a user she can return to her session, that promise must hold regardless of device, browser, time elapsed, or network failure.
Product link: https://fertilityawareness.ifsdnetwork.com/

The FEMM quiz had a problem that looked like a UX failure but was, at its root, a philosophical one.

A lady arrives at question 14 of 20. Her phone dies. She returns three days later on a different device — and finds herself back at question 1. Her session is gone. Her answers are gone. And with them, quietly, her trust.

Here is what most engineers reach for at this point: a cookie. It is fast, it is simple, and it is wrong. A cookie lives on one device, expires on a schedule it did not negotiate with the user, and knows nothing about the server. It is a note left on one chair in a room with many chairs. If the user sits somewhere else, the note is not there.

The real solution required thinking about the state as a contract rather than a front-end/ on-device convenience, and contracts have two parties.

What followed was a three-part architecture: a persistence layer writing quiz state to Cloudflare D1 on every answer submission, an email cron worker detecting abandoned sessions and dispatching encrypted recovery links, and a hook on the frontend that suspended rendering until the D1 session had been fetched and fully resolved.

That last step was the hardest. React's reconciliation expects server and client state to agree at the moment of hydration. When they do not, even by a single answer, the component tree either crashes silently or renders the wrong data with complete confidence. The fix required strict sequencing: fetch server state first, suspend rendering, then hand the hydrated state to the hook.

A flowmap of session data
A 3-way handshake to secure your personal session.

A quiz that cannot be recovered is a lady whose sensitive reproductive endocrinology concerns cannot be answered. That single human fact was the architectural constraint that determined every other decision in this phase. If you promise a user she can return to her session, that promise must hold regardless of device, browser, time elapsed, or network failure.

That is not an engineering preference. It is a moral obligation dressed in TypeScript.

An email with a button to resume a session
Session resumption can make the difference between an over-the-counter bandage and an evidence-based long term medical solution.