DEV Community

Our buy button says "Network error" for a server error, and for five seconds after you click it a second click buys again

The buy button on notifio.app/pricing is one small client-side handler. I have written about the server half of the flow before, but never about what the page does when the request misbehaves.

So I opened the live pricing page in a browser, intercepted POST /api/checkout and answered it myself. Nothing in this post touched the real endpoint. Every response below was fabricated in the browser, so no Stripe session was created and no email was sent. The page still ran its real code.

The handler:

setLoading(true);
try {
  const res = await fetch("/api/checkout", { method: "POST", /* ... */ });
  const data = await res.json();
  if (!res.ok) {
    setError(data.error ?? "Something went wrong. Please try again.");
    return;
  }
  window.location.href = data.url;
} catch {
  setError("Network error. Please try again.");
} finally {
  setLoading(false);
}

Two things stand out before running anything. res.json() sits inside the same try as the fetch, so a body that is not JSON throws into the catch block. And finally runs the moment window.location.href is assigned, not when the page has actually gone.

Nine responses

I typed an address into the form, pressed the button, and read what the page showed.

What I returned What the visitor sees
Request aborted Network error. Please try again.
500, empty body Network error. Please try again.
502, an HTML "Bad Gateway" page Network error. Please try again.
504, plain text Network error. Please try again.
429 with {"error": "Too many requests..."} Too many requests. Please try again later.
409 with {"error": "A license already exists..."} A license already exists for this email address.
400 with {} Something went wrong. Please try again.
200 with {} Nothing, then the browser lands on /undefined
200 with {"url": null} Nothing, then the browser lands on /null

Rows 1 to 4 are the interesting group. A 500 that arrives from a healthy network is described to the visitor as a network error.

That matters because of the route behind it. The route has one try/catch, and it wraps only the final Stripe call. The rate limiter lookup and the licence lookup above it are plain awaits. If either of them throws, Next answers with a 500 and no JSON body, which lands in row 2.

The last two rows need a server that returns success with no URL. I do not think ours does today, since the route returns session.url straight from Stripe. But the client has no opinion about it, and /undefined is a 404 page on a sales flow.

The button unlocks before the page leaves

The disabled state is the only guard against double submission. I recorded it from inside the page with a MutationObserver, holding the destination page back for five seconds so the redirect stayed pending:

  • 1,372 ms: click
  • 1,375 ms: label becomes "Redirecting...", button disabled
  • 1,379 ms: label is back to "Buy now", button enabled
  • 6,499 ms: the page finally unloads

For the first four milliseconds the button does what it says. For the next five seconds it is an ordinary live button. I clicked it again from a timer inside the page and got a fresh request each time: five clicks produced five requests to /api/checkout and five navigations.

Against the real endpoint each of those would create its own Checkout session. On a fast connection the window is short, because the redirect is quick. On a slow phone it is not.

The control: the other form

The contact form on /help has the same shape, and the same catch block. I ran the same interceptions on it:

  • Abort, 500, 502: all three say "Network error. Please try again."
  • 429 with a JSON error: shows that error
  • 200: replaces the form with "Message sent"
  • Five clicks while a response was held for three seconds: one request

So the lie in the error text is shared, and the double submit is not. The contact form disables itself for the whole time its status is sending, and it only leaves that state by replacing the form. The pricing form leaves it too early because success and failure both fall through finally.

I covered the validation split between the browser and the server in Our contact form runs four checks in the browser and seven on the server, and what the page does with JavaScript off in I turned JavaScript off on all 38 pages of our site. This is the third side: what happens when the server answers badly.

What I would change

I have not edited the page. The fixes are each a few lines, and I would pick them together:

  • Parse the response defensively, so a non-JSON failure reads "Something went wrong on our side" and a thrown fetch reads "Network error".
  • Check for a string url before assigning it, and treat anything else as an error.
  • Only clear loading on failure. On success leave the button disabled until the browser navigates away.
  • Wrap the whole route body in the try, so the limiter and the database lookup get the same JSON error shape.

The pages are live if you want to look at them: pricing, help and download. To reproduce the table you need your own browser's request interception, not the real buy button.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.