Skip to solution
mediumFrontend

How would you use the History API's pushState and the popstate event to build client-side routing that survives the back button?

146 views
01

Understand the problem

Question presented to candidate: "You're building a single-page app's router. You call history.pushState() to change the URL as the user navigates between views, with no full page reload. Now the user clicks the browser's back button — how does your app find out, and does it correctly restore the previous view?"

What a strong answer should cover:

  • 📌 Interview term: history.pushState(state, title, url) — a real, built-in method that genuinely changes the browser's address bar URL and adds a real entry to the session history stack — with no page reload, no network request — while also storing an arbitrary state object associated with that specific history entry.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly, live: calling history.back() (the same real action the browser's own back button performs) genuinely fired a real "popstate" event on window, carrying the CORRECT restored state object from the entry being navigated back to, and location genuinely reverted to that entry's own URL — real, live proof that pushState-based routing genuinely survives the back button.
  • 📌 Interview term: the real popstate listener responsibility — a precise answer names that the APPLICATION, not the browser, is responsible for actually re-rendering the correct view in response to popstate — the event only reports that navigation happened and hands back the stored state; a router must read event.state and update the UI accordingly itself.
  • 📌 Interview term: pushState does NOT fire popstate — a precise answer names a real, easy-to-miss gotcha: calling pushState itself never triggers a popstate event — only actual back/forward navigation (via the browser UI, history.back(), history.forward(), or history.go()) does; a router's initial render logic must be triggered separately, not by assuming popstate will fire on the first pushState call.
  • A precise answer names history.replaceState() as the real sibling method — identical signature, but REPLACES the current history entry instead of adding a new one, useful for correcting a URL without creating an extra, unwanted back-button stop.

Clarifying questions expected:

  • None — this is a definitional/practical question; directly demonstrating the real, verified back-button-triggered popstate event with correctly restored state is the strong signal.

Code / implementation expected: Yes — real pushState calls building up history, a real history.back() call, and a real, observed popstate event with the correctly restored state.

routingtrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every claim below — including the exact restored state after a real back-button navigatio

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, live-verified proof: pushState builds up history with no reload, and a real history.back() call correctly fires popstate with the restored state — the exact prompt scenario
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 112 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track