Mini Router
Build a client-side router: history, route matching and ranking, links, nested routes, guards and data loaders.
What you will build
You will write a small client-side router in plain JavaScript, in the style of React Router's data routers. It starts with a memory history that has the API of the history package, matches URLs against route patterns and ranks them by specificity, keeps the result in a state you can subscribe to, and turns clicks on ordinary links into navigations.
Then you add nested routes that render into their parent's outlet, guards that cancel or redirect a navigation (back/forward included), and data loaders that run in parallel and are aborted when a newer navigation starts. In the last step a small team directory runs on your router in the preview.
What you will learn
- How session history works: entries, push and replace, back/forward, and why `pushState` fires no event
- How a router matches URL patterns and ranks routes by specificity
- How to intercept link clicks without breaking new tabs, downloads and links to other sites
- How nested routes become branches of a tree and render through outlets
- How guards and data loaders fit into one navigation pipeline, and how `AbortController` cancels a stale navigation
Steps
- 01Memory historyBuild a session history in memory: a list of entries with push, replace, go and listeners, like the history package.
- 02Match and rank routesMatch URL patterns with params and splats, and rank the routes so that the most specific one wins.
- 03Router and linksConnect the history and matching in a router with a state you can subscribe to, and turn clicks on plain links into navigations.
- 04Nested routesFlatten a route tree into branches, match the whole chain from the root and render every level into its parent's outlet.
- 05Navigation guardsRun route guards before a navigation is committed: cancel it or redirect it, also for back/forward and the initial location.
- 06Data loadersLoad the data of every matched route in parallel before the page changes, and abort the loading of a navigation that a newer one replaced.
- 07Run it: a team directoryRun a small app on your router and mark the active links with aria-current, like NavLink.
Practise this
Other hands-on work on the same topics.
Incident Simulator
System Design
Weekly Challenges
Frontend History
See it in real code
Guided tours through the source of real libraries that rely on it.