I Shipped Back Buttons That Were Just Links to a Guess
A bug report came in that looked tiny: open a test case, go to its Runs tab, open one of the runs, press the back chevron next to the run’s title — and you land on the global run log instead of the test case’s Runs tab you came from. The browser’s own back button did the right thing. Mine didn’t.
It wasn’t tiny. It was the same mistake repeated at roughly twenty call sites, and I wrote all of them.
What I actually wrote
Every on-page back control in our shared UI package was a link to a fixed parent route:
<BackChevron to={routes.runs()} section="runs" label="All runs" />
That’s it. The chevron on a run detail page pointed at /runs, because /runs is “where runs live.” The chevron on a model page pointed at /models. The chevron on a plan editor pointed at the list of plans. Each page decided, at author time, where “back” meant — and that decision had nothing to do with how the person using it got there.
When the first complaints about this arrived, I patched it instead of fixing it. The patch kept a small module that remembered the last index tab you’d seen per “section” (runs, testCases, other, coverage), and let a couple of coverage lists “claim” the back target of the next run page you opened:
export function sectionReturnTo(section, fallback) {
if (section && claimedReturn) return claimedReturn.url;
if (!section) return fallback;
return lastIndexUrl.get(section) ?? fallback; // last /runs* tab seen, else /runs
}
Read that honestly and it says: back means the last list page of this same section you happened to visit. It only ever worked when you entered a detail page from its own section’s list. Every cross-section path still fell straight through to the hard-coded parent:
- test case’s Runs tab → run → chevron →
/runs(the reported bug) - model’s Runs tab → run → chevron →
/runs - run → click the test case title → chevron → the catalog, not the run
- account Reviews tab → a review → “← All reviews” → the run’s Verdict tab
- a plan → its edit page → chevron → the list of all plans, not the plan you were editing
- analysis view entered from anywhere → brand back arrow → always
/runs
And because the chevron pushed a new history entry, pressing back and then the browser’s back button bounced you right back into the detail page you had just left. The control didn’t just point somewhere wrong; it corrupted the history trail on the way.
Why it was wrong, not just buggy
The root error is that I modeled back navigation as a property of the page rather than of the visit. “Where is the parent of a run?” has a clean, static answer, which is exactly what made it so comfortable to hard-code. But nobody pressing a back chevron is asking about parentage. They’re asking to undo their last step. Those two questions coincide often enough — maybe 70% of the time — that the wrong model looks correct in casual testing and only fails on the paths that cross between areas of the app.
The claim/section patch is the part I’m least proud of. It was a lookup table of special cases bolted onto a wrong model, and it had the particular property of making the remaining failures harder to reason about: now a chevron’s destination depended on invisible state from a page you’d visited minutes ago. I traded a predictable wrong answer for an unpredictable one and called it progress.
What I should have written
A back control should resolve its target from the tab’s real navigation history and move back through it, falling back to a fixed parent only when there is no in-app history at all — a fresh deep link:
<BackChevron
scope={routes.runDetail(run.id)} // this page; its tabs live under it
fallback={routes.runs()} // only for a deep link
label="All runs"
/>
Three ideas carry the whole fix:
A page spans every route under its own path. A run’s Logs, Events, and Verdict tabs are all the same page, so back should skip past all of them to whatever came before the run. That’s what scope encodes. No more “press back, land on the previous tab.”
Back is a history move, not a link. A provider mounted once at the app root records the tab’s in-app history (push, replace, pop, keyed by router location key) and mirrors it to session storage so it survives a reload. useBackTarget(scope, fallback) finds the most recent entry outside the current scope and steps there with a real navigate(-n). Because it’s a genuine history move, pressing back repeatedly walks the trail backwards instead of ping-ponging between two pages. The link still carries a real href to the same destination so Ctrl/Cmd/middle-click keeps working.
Make the wrong version unwritable. This is the part that actually prevents the regression. BackChevron now requires scope plus fallback (or an explicit onBack). The old to and section props are gone. You can no longer express “back is a link to this fixed route,” so the bug can’t be reintroduced by a tired developer at 6pm — including me. I also added a BackLink so text-style back links get identical behaviour instead of growing their own ad-hoc version. The section/claim module was deleted outright, along with every call into it.
I wrote the rule into the component docs first, before touching code: every on-page back control returns to the previous page, a page spans every route under its own path, and a fixed parent applies only when no in-app history exists. If I’d had that paragraph written down the first time, I don’t think I would have shipped twenty fixed-parent links.
What an adversarial review caught afterward
I had the first version of the fix reviewed hard, and it found real problems. Worth listing, because each one is a lesson about where my confidence was unearned:
- The step count drifted from the browser’s history. The router renders navigations inside transitions, so two navigations in one handler can render once — the browser holds an entry my record never observed, and
navigate(-n)counted from my own record landed an entry short. The step is now measured against the router’s own position counter rather than my bookkeeping. There’s a browser-level test that reproduces this and fails without the fix. Lesson: when you shadow a browser-owned data structure, your copy will diverge; measure against the real thing. - Back after creating something reopened a blank form. Saving a comparison, saving a model, and launching a run all pushed the result page, which made the form the “previous page.” Those navigations now replace the form, and the docs state the rule. A correct back button immediately exposes every place where you polluted history.
- An embedded build iframe could swallow a back step. Re-pointing an already-loaded iframe adds a history entry the router never sees. It’s now keyed by its source so it remounts instead.
- A model opened by an alias could go “back” to itself, because its scope was built from only one of its two valid paths. A scope can now list several paths.
- The provider was reading a ref during render; it uses derived state now.
I also accepted one limitation rather than papering over it: when a control returns through history, its label reads “Back” instead of naming the destination. The specific wording (“All runs”) only shows when that fallback actually is the destination. Truthfully naming an arbitrary previous page would require every page to register a title, and nothing does that today. A back control that confidently names the wrong place is worse than one that says “Back” — that’s the whole bug, in miniature.
How I’m changing my habits
Write the invariant before the component. The real failure wasn’t any single to={routes.runs()}; it was that no one had ever written down what “back” means in this app, so each page author invented an answer. One paragraph of documentation would have been cheaper than twenty call-site fixes.
Treat a second special case as a design signal. When the section/claim patch needed per-section state and an escape hatch for two specific lists to override the next page’s target, that was the moment to stop and question the model. Instead I shipped the escape hatch. Special-case counts are a smell with a very short fuse.
Don’t model user intent as static structure. “Where did you come from” is runtime state. Any time I find myself answering it at author time, I’m guessing on the user’s behalf.
Make the API refuse the old mistake. Fixing the call sites fixes today. Removing the props that expressed the wrong idea fixes next quarter. If a fix doesn’t close the door behind it, I’ve scheduled the bug’s return.
Test the crossings, not the happy path. My tests all walked list → detail → back, the one route where the broken model looked fine. The new tests cover entering a detail page from a different area, skipping sibling tabs, pressing back twice, deep-link fallback, and modified clicks. The reported scenario now has a test that fails against the old code.
The fix is in, the gates are green, and I’ve manually walked the five paths from the report. The part that stings is that the browser’s own back button got this right the entire time — I had a correct reference implementation sitting in the toolbar and chose to guess instead.