The in-app Back button goes to the last page you were on, not the last URL.

The problem

Putting tabs, filters and dialogs in the URL is right: they’re linkable and they survive a refresh. The cost is that the browser’s Back now replays every tab you clicked. Flip through four tabs and it takes four presses to leave the page.

The obvious fix is to replace the history entry on tab changes, so tabs never add entries. That breaks something else. Tabs stop being something Back can undo, and plenty of people do use Back to return to the tab they were just on.

What I concluded

Keep pushing entries, so the browser’s Back behaves exactly as people expect. Then give the app’s own Back button a different job: walk history for the nearest entry that’s on a different page and isn’t a dialog.

function smartBack(entries, index) {
  for (let i = index - 1; i >= 0; i--) {
    const e = entries[i];
    if (!e.modal && e.page !== entries[index].page) return i;
  }
  return -1;   // nothing earlier in this session
}

Two buttons, two jobs. The browser’s Back undoes one step. The app’s Back says where it goes (“Back to Orders”) and takes you there. A breadcrumb trail uses the same idea and treats a run of entries on one page as a single stop.

-1 means nothing earlier is on another page, which is what you get when someone opens a shared link. Going back would leave the app, so in a real app the button goes to the page’s parent route instead (the list above a detail page) and replaces the entry. The demo keeps it simpler and disables the button there.

Dialogs mostly take care of themselves: closing one pops its entry. The dialog check covers the leftovers, like a dialog that linked somewhere else on its way out.

The demo shows the walk. Change pages, flip tabs, open and close a dialog, and the entry list marks where each Back would land.

Rules

  • Don’t break the browser’s Back. Tabs and filters still push entries.
  • The app’s Back goes to a page. It skips tab, filter and dialog entries on the current page.
  • Name the destination. The button’s label and accessible name say where it goes.
  • Never leave the app. With nothing behind you, go to the parent route.

What you get

People get both kinds of back without having to think about it. Undoing a tab is one press of the browser button, and leaving a page you’ve been poking at is one press of the app’s.

Related: URL state, Navigation, Typed routes.