Our forms asked "Discard what you typed?" right after you clicked Create
I wrote this bug and shipped it, and it hit almost every form in the console.
Every create and edit page in our cloud console uses one shared form component. That component protects typed input: if you try to leave a form with unsaved changes, it asks “Discard what you typed?” and offers Cancel and Discard. That part was correct. What I got wrong is that the dialog also opened when you submitted the form. You clicked Create, the request went through, and then the page asked whether you wanted to throw away what you’d just saved. “Discard” only finished the navigation to the new object. “Cancel” left you on a form for something that already existed.
The operator hit it on the first form they used in production, the new project page, and said it was hard to take a platform seriously if it got something this basic wrong. I agree. It was in the first commit, it survived three releases, and it affected about twenty form pages across every service.
What I did wrong
The guard release was a race, and my comment said otherwise
The form registered a router blocker that fired when the form was dirty, wasn’t marked as “leaving”, and the path was changing. On submit I did this:
const outcome = await onSend();
if (outcome === false) return;
setLeaving(true);
// The guard is let go before the page returns.
queueMicrotask(() => navigate(target));
The comment describes what I meant to happen. The code doesn’t do it. setLeaving only schedules a render, and the router doesn’t pick up the new blocker predicate until an effect runs after that render commits. The microtask runs before either one. So the router checked the old closure, where leaving was still false, and blocked the navigation.
I stated an ordering guarantee in a comment and never checked it. That was the root mistake.
“Dirty” never got cleared
On create forms, dirty became true at the first keystroke and nothing set it back to false after a successful send. But dirty is supposed to mean “has unsaved input”. Once the input is saved, it isn’t dirty. If I’d cleared that state on success, the guard couldn’t have fired even with the race still there.
I rewrote a working component
Our admin panel already had this guard working correctly. It holds the guard in a ref, releases it synchronously, and calls release() before navigating. The console’s design docs even point to the admin panel’s behaviour. I wrote my own version instead of carrying that one over, and the rewrite is where the race came from.
My tests could not fail on this bug
- No test imported the shared form component, and the guard had no test at all.
- The project create test typed a name, clicked Create, and checked only that the right
PUTwas sent. It never checked where the page went or whether a dialog appeared. Every other create test had the same shape. - We had a browser end-to-end suite, but it never submitted a form.
I tested the part I knew the code did (sending the request) and not what the user actually experiences (landing on the new object with no prompt).
What I should have done
- Release the guard synchronously with the navigation. Use a ref, or navigate only once the guard is actually gone.
setStatefollowed by a microtask, timer or immediate navigate doesn’t work for this. - Clear typed state on a successful send before doing anything else.
- Copy the working sibling. When a correct implementation already exists, start from its source.
- Test the user’s outcome. For a create flow, the last assertion should be “you’re on the new object’s page and no dialog is showing”, not “the request was sent”.
- Run the first user flow in a real browser before release. We already had a rule that a user-facing screen isn’t done until someone has looked at it rendered in the browsers people use. I treated that rule as being about other kinds of screens. It applies to this one, and the first flow of a product is the one to check first.
- Say it in the spec. Our design implied that submitting a form isn’t the same as leaving it, but never said so. One clause (“a sent form returns without a prompt”) would have given both the code and the tests something concrete to check.
What I’ll look for in review from now on
- A comment that claims an ordering (“released before…”) next to
setStateplus a microtask, timer or navigation. - A
dirtyflag that turns on at the first keystroke and has no path back to false on success. - A route blocker whose predicate depends on state the submit handler is changing.
- A create or edit test whose final assertion is the request that was sent.
- A shared component taken from a sibling product with its guard logic rewritten.
The fix is small. What went wrong was my process: I trusted my own comment, wrote tests that stopped before the outcome that mattered, and shipped a flow nobody had clicked through in a browser.