|
| 1 | +--- |
| 2 | +title: "The Navigation API — Finally a Sane Router Primitive" |
| 3 | +description: "history.pushState was the only routing tool for a decade — and it was awful. The Navigation API is the actual primitive routers should have been built on." |
| 4 | +date: 2026-05-10 |
| 5 | +slug: navigation-api |
| 6 | +tags: [javascript, navigation, architecture] |
| 7 | +--- |
| 8 | + |
| 9 | +Every SPA router on npm — react-router, vue-router, @tanstack/router — exists because `history.pushState` is unusable as a routing primitive. No event for clicked links. No way to intercept navigation. Async lifecycle? Roll your own. The Navigation API is the platform fix. |
| 10 | + |
| 11 | +## The fundamental win: navigate event |
| 12 | + |
| 13 | +```js |
| 14 | +navigation.addEventListener("navigate", (event) => { |
| 15 | + if (!event.canIntercept || event.hashChange) return; |
| 16 | + |
| 17 | + const url = new URL(event.destination.url); |
| 18 | + if (url.origin !== location.origin) return; |
| 19 | + |
| 20 | + event.intercept({ |
| 21 | + handler: async () => { |
| 22 | + const html = await fetch(url, { signal: event.signal }).then((r) => r.text()); |
| 23 | + document.querySelector("main").innerHTML = html; |
| 24 | + } |
| 25 | + }); |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +That's a complete client-side router. ~10 lines. |
| 30 | + |
| 31 | +What it gives you: |
| 32 | +- Fires for every navigation: clicks, `navigation.navigate()`, back/forward, even `<form action>` submissions |
| 33 | +- `event.intercept(handler)` — take over the navigation, do async work, the URL bar updates |
| 34 | +- `event.signal` — automatic AbortSignal that cancels if the user navigates away mid-load |
| 35 | +- `event.hashChange` lets you skip in-page anchor jumps |
| 36 | +- `event.canIntercept` returns false for cross-origin / download requests |
| 37 | + |
| 38 | +## Compare to the old way |
| 39 | + |
| 40 | +```js |
| 41 | +// pre-Navigation API |
| 42 | +window.addEventListener("popstate", handlePopState); |
| 43 | +document.addEventListener("click", (e) => { |
| 44 | + const link = e.target.closest("a[href]"); |
| 45 | + if (!link) return; |
| 46 | + if (link.target === "_blank") return; |
| 47 | + if (link.href.startsWith("mailto:")) return; |
| 48 | + if (link.host !== location.host) return; |
| 49 | + if (e.metaKey || e.ctrlKey) return; |
| 50 | + // ... 20 more edge cases ... |
| 51 | + e.preventDefault(); |
| 52 | + history.pushState(null, "", link.href); |
| 53 | + handleRoute(link.href); |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +Every framework router has this 200-line click interceptor. Navigation API replaces it with `addEventListener("navigate", ...)`. |
| 58 | + |
| 59 | +## Async navigations with state |
| 60 | + |
| 61 | +```js |
| 62 | +navigation.addEventListener("navigate", (event) => { |
| 63 | + if (!event.canIntercept) return; |
| 64 | + |
| 65 | + event.intercept({ |
| 66 | + handler: async () => { |
| 67 | + showLoadingBar(); |
| 68 | + try { |
| 69 | + await renderRoute(event.destination.url); |
| 70 | + } finally { |
| 71 | + hideLoadingBar(); |
| 72 | + } |
| 73 | + }, |
| 74 | + // Tell the browser: focus management, scroll restoration |
| 75 | + focusReset: "after-transition", |
| 76 | + scroll: "after-transition" |
| 77 | + }); |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +`focusReset` moves keyboard focus to the new view. `scroll` restores scroll position on back/forward. Both are A11y wins you usually have to hand-build. |
| 82 | + |
| 83 | +## Programmatic navigation with await |
| 84 | + |
| 85 | +```js |
| 86 | +const result = await navigation.navigate("/lessons/42").finished; |
| 87 | +console.log("navigation done"); |
| 88 | +``` |
| 89 | + |
| 90 | +`navigation.navigate()` returns `{ committed, finished }` — two promises. `committed` resolves when the URL changes; `finished` when the handler finishes. So you can `await` a route load like any other async operation. |
| 91 | + |
| 92 | +## Pair with View Transitions |
| 93 | + |
| 94 | +```js |
| 95 | +navigation.addEventListener("navigate", (event) => { |
| 96 | + if (!event.canIntercept || event.hashChange) return; |
| 97 | + |
| 98 | + event.intercept({ |
| 99 | + handler: async () => { |
| 100 | + if (!document.startViewTransition) { |
| 101 | + return renderRoute(event.destination.url); |
| 102 | + } |
| 103 | + const transition = document.startViewTransition(() => |
| 104 | + renderRoute(event.destination.url) |
| 105 | + ); |
| 106 | + await transition.finished; |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +Click → fetch → swap DOM → animate. The combo gives you SPA-feel without a router framework. |
| 113 | + |
| 114 | +## Going back / forward |
| 115 | + |
| 116 | +`navigation.entries()` returns every entry in the history (just for your origin): |
| 117 | + |
| 118 | +```js |
| 119 | +const entries = navigation.entries(); |
| 120 | +const currentIdx = navigation.currentEntry.index; |
| 121 | +const previous = entries[currentIdx - 1]; |
| 122 | + |
| 123 | +if (previous) { |
| 124 | + navigation.traverseTo(previous.key); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +No more guessing what's in `history.state` — you have a real list of entries with stable keys. |
| 129 | + |
| 130 | +## Form intercepts |
| 131 | + |
| 132 | +```js |
| 133 | +navigation.addEventListener("navigate", (event) => { |
| 134 | + if (event.formData) { |
| 135 | + // It's a form submission, formData is the submitted FormData |
| 136 | + event.intercept({ |
| 137 | + handler: async () => { |
| 138 | + const response = await fetch(event.destination.url, { |
| 139 | + method: "POST", |
| 140 | + body: event.formData |
| 141 | + }); |
| 142 | + renderRoute(event.destination.url, await response.text()); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +You can intercept form submits *and* GET navigations through one event. The old way needed separate handlers for `submit` events. |
| 150 | + |
| 151 | +## Browser support |
| 152 | + |
| 153 | +- Chrome / Edge 102+ (May 2022) |
| 154 | +- Safari 18+ (September 2024) |
| 155 | +- Firefox: in development as of 2026 |
| 156 | + |
| 157 | +For Firefox today: `if (!window.navigation) { /* fall back to old click-intercept */ }`. Most existing routers do this internally already, but if you're hand-rolling, write the fallback. |
| 158 | + |
| 159 | +## When NOT to reach for it |
| 160 | + |
| 161 | +- **Multi-document apps** (server-rendered without hydration) — Speculation Rules cover that case better |
| 162 | +- **Tiny apps with 2 routes** — keep using `<a>` and full reloads, not worth the wiring |
| 163 | +- **Cross-origin SSO redirects** — `canIntercept` returns false anyway, the browser owns this |
| 164 | + |
| 165 | +## What this kills |
| 166 | + |
| 167 | +- Most of `history.pushState` direct usage |
| 168 | +- Click-interceptor boilerplate in vanilla SPAs |
| 169 | +- The motivation for "minimal SPA router" libraries |
| 170 | +- Hand-rolled scroll/focus restoration |
| 171 | + |
| 172 | +When the Navigation API is universally available (Firefox roadmap suggests 2027), the question "which router should I use?" becomes "do I need a router at all, or just `addEventListener('navigate', ...)`?" |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +Practice JS basics in the [`js-events`](/js-events/0/) module on [Code Crispies](/) — covers `addEventListener` and event delegation. |
0 commit comments