Skip to the page

Observers, workers and the platform · mid

History API and client-side routing

Client routers change the URL with history.pushState (no reload) and react to back/forward through the popstate event; the server must return the app for every route so deep links work.

The notes

What you must know

  • pushState(state, '', url) changes the URL and adds an entry; replaceState replaces it; neither fires popstate.
  • popstate fires on back/forward; read location and render the matching view.
  • Intercept same-origin link clicks (preventDefault) and pushState instead. The Navigation API (navigation.navigate, navigate event) is the newer interface.
  • Restore scroll and move focus to the new content for accessibility.

The red pen

Common traps: mistake → fix

  • Mistake: "pushState triggers popstate"

    Fix: only back/forward do.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does a client-side router work?
  2. Why do deep links 404 on some static hosts?
  3. What should happen to focus after a route change?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview