跪拜 Guibai
← All articles
JavaScript · Frontend Frameworks · Full Stack

Frontend Routing Is Just a Mapping Table and a DOM Swap

By 烬羽 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Modern routers hide the machinery behind configuration DSLs, but every SPA navigation still boils down to a lookup table and a DOM swap. Understanding the raw `hashchange` mechanism makes debugging routing bugs, choosing between hash and history mode, and reasoning about SSR versus client-side rendering straightforward instead of magical.

Summary

A hand-written HashRouter in vanilla JavaScript strips frontend routing down to its core: a path-to-callback mapping table and a `hashchange` event listener. The `#` in a URL was originally designed for in-page anchors, so browsers never send a request when it changes. Frontend frameworks exploit this to intercept navigation and replace only the relevant DOM fragment, eliminating the white flash of a full page load. The implementation exposes a classic JavaScript pitfall where event handlers rebind `this` to `window`, breaking access to the route table unless explicitly bound with `bind`, `call`, or an arrow function. Hash routing's limitations — ugly URLs, poor SEO, anchor conflicts, and server invisibility — are what later pushed the ecosystem toward the History API, but the underlying mental model stays the same.

Takeaways
— Hash changes (`#/page`) never send an HTTP request; the browser treats them as in-page anchor navigation.
— A frontend router is fundamentally an object mapping URL paths to render callbacks, plus an event listener that fires those callbacks.
— The `hashchange` event rebinds `this` to `window` inside handlers, so methods that access instance properties must be explicitly bound.
— Hash routing replaces the traditional click→HTTP request→full re-render cycle with click→hashchange→innerHTML swap, removing the white screen.
— Hash URLs are invisible to servers and search crawlers, which makes SSR and SEO impossible without switching to History API routing.
Conclusions

The article's framing — that modern routers are just prettier wrappers around a mapping table and an event — is a useful corrective to framework mystique, though it understates how much complexity nested routes, lazy loading, and guards add in practice.

Treating the `this` binding pitfall as a routing-specific lesson is smart pedagogy; it anchors a dry language mechanic to a concrete, breakable demo that every frontend developer will encounter.

The open question about coexisting anchor links and hash routes hints at a real-world tension the article doesn't resolve: once you hijack the hash for routing, you lose native in-page scrolling unless you build a custom scroll manager on top.

Concepts & terms
Hash routing
A frontend routing strategy that uses the URL fragment after `#` to represent application paths. Because browsers never send hash changes to the server, navigation happens entirely client-side without page reloads.
hashchange event
A browser event fired on `window` whenever the URL hash changes. Frontend routers listen for this event to trigger DOM updates matching the new hash path.
History API routing
A newer routing approach using `pushState` and `popstate` to change the URL path without a hash, producing cleaner URLs that the server can see, enabling SSR and better SEO.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗