DEV Community

Ian
Ian

Posted on

A silent data-loss bug in a paid product: the draft was in memory the whole time

The bug had been live for weeks and no one had reported it, because it only fired for people who had just paid us money.

What the product does

It's a small tool that generates wedding speeches. Nine questions, three drafts, one payment. The kind of product where the whole value is the output — the text is the thing you buy.

The bug

After checkout, the payment provider redirects the user back to /success. That page verifies the payment and then does this:

<Link href={`/${locale}/form`}>
  {copy.okCta} →
</Link>
Enter fullscreen mode Exit fullscreen mode

Back to the form. Makes sense — the drafts live on the form page.

Except the drafts were stored like this:

const [result, setResult] = useState(null);
// ...
setResult(data);
Enter fullscreen mode Exit fullscreen mode

In memory. Nowhere else.

The sequence, for a paying customer:

  1. Fill in nine questions
  2. Read three drafts
  3. Get redirected to checkout
  4. Pay
  5. Get redirected back
  6. See an empty questionnaire
  7. Fill in the same nine questions again
  8. Wait another 60 seconds for generation

Nothing was broken, technically. Every request returned 200. There were no errors in the logs. A payment succeeded and the user was shown the thing they paid for — eventually.

Why it survived

I found it by reading the code, not by using the product. That's the part that bothers me.

The happy path in testing was: generate → look at drafts → done. Every test I'd run started from a fresh form and ended at the drafts. Nobody I'd shown it to had actually completed a purchase, because we'd had zero purchases.

A payment flow nobody has exercised is a payment flow that doesn't work. Traffic and conversion were so low that the downstream half of the funnel had never been walked.

The fix, part one: persist locally

const RESULT_PREFIX = "ws-result-";

// on success
try {
  localStorage.setItem(RESULT_PREFIX + locale, JSON.stringify(data));
} catch {}

// on mount
try {
  const rawResult = localStorage.getItem(RESULT_PREFIX + locale);
  if (rawResult) {
    const cached = JSON.parse(rawResult);
    if (cached?.drafts?.length > 0) {
      setResult(cached);
      setTimeout(() => document.getElementById("result")?.scrollIntoView(), 300);
    }
  }
} catch {}
Enter fullscreen mode Exit fullscreen mode

Also changed the success link to /${locale}/form#result so the user lands on the drafts rather than the top of the questionnaire.

The fix, part two: persist on the server

localStorage is better than nothing, but it's still wrong for a paid artifact:

  • new device → gone
  • cleared cache → gone
  • never returns to the site → gone

So generation results now also POST to an endpoint that writes to the database:

void fetch("/api/generations", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ email, locale, checkoutId, answers, drafts }),
}).catch(() => {});
Enter fullscreen mode Exit fullscreen mode

Two deliberate choices:

  1. Fire and forget. Archival failure must never block the user from seeing their drafts. The catch(() => {}) is intentional, not sloppy.
  2. Degrade silently if the table doesn't exist yet. The endpoint returns { ok: false, reason } instead of 500, because a missing table is a deployment issue, not a user-facing error.

The fix, part three: let them take it with them

The drafts had a "copy" button and nothing else. If you paid for text, you should be able to keep it as a file.

const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `speechkobo-${i + 1}.txt`;
a.click();
URL.revokeObjectURL(url);
Enter fullscreen mode Exit fullscreen mode

PDF would look more impressive. txt is what someone actually needs when they're standing backstage holding a phone.

What I took from this

1. Test the funnel backwards from the money. Not "does generation work" but "does a person who paid end up holding the thing they paid for". Those are different tests.

2. useState is a cache, not storage. If a value survives a redirect, it needs to be in localStorage, a cookie, or the database. There is no fourth option.

3. Silent failure has to be a decision. Every catch {} in that fix is a choice about what the user sees. The archival one is fine to swallow. The verification one is not.

If you're building something with a paywall and you've never bought your own product: buy it. You will find something.

(I write about building small AI products for non-English markets. The one in this post is Speech Kobo, and the open dataset behind its etiquette checks is on GitHub.)

Top comments (0)