Engineering

The Router I Did Not Install

Two routes do not need a routing library. They need about forty lines and a hydration contract you can state in one sentence.

3 min readPublished May 28, 2026
Share on X

This site has two route shapes. The feed at `/`, and an article at `/posts/<slug>`. That is the entire routing requirement, and I wrote forty lines for it instead of installing a routing library.

The interesting part is not the forty lines. It is that the route table has to be true in two places at once — at build time and in the browser — and that is the constraint a library would have hidden from me.

A prerendered site has no server to ask

On a server-rendered site the router is resolved per request, so the server is the single source of truth. Here the build step is the server: it walks a list of slugs and writes a directory per slug. The browser then loads one of those files.

That means the route matching has to agree with itself, or a visitor gets a page whose HTML was rendered for a different URL than the one in their address bar. That is not a subtle bug. It is a page that renders twice and looks wrong in between.

The hydration contract, in one sentence

The path is a prop, passed in from outside the component tree, and both renderers receive the same value.

// server: the path is an argument
export function render(path = "/") {
  return renderToString(<App initialPath={path} />);
}

// client: the path is read from the address bar
hydrateRoot(root, <App initialPath={window.location.pathname} />);

Reading `window.location` inside render would have worked on the client and broken on the server, because the server has no window. Worse, it would have broken silently: the client would render the right thing, just later, after a hydration mismatch warning nobody reads.

Permissive matchers fail loudly

The slug matcher originally only accepted lowercase letters, digits and hyphens. So a request for `/posts/UPPER`, or a typo, matched nothing and fell through to the feed — a 200 at a URL that does not exist, showing seven real-looking articles.

A loose pattern fails loudly. A strict one fails silently into the wrong view.

The fix is to match any single segment and let the data decide what exists. `/posts/UPPER` now reaches the not-found branch, which is honest. A route matcher should do the matching; the content should decide the answer.

Every internal link is an `<a href>`. Only a plain left click with no modifier keys is intercepted, so cmd-click, middle-click, and "copy link address" all behave the way a browser user expects. A router that rewrites everything into click handlers loses the open-in-new-tab menu for no reason.

  • React
  • Architecture
  • Web Development
KI

Kadirul IslamVerified author

Founder, A.K Solution

Follow on X

Founder of A.K Solution. Full-stack engineer building AI agent tools like KiCode, JSON form engines like ki-forms, and high-scale SEO systems. Obsessed with shipping fast, zero-bloat software that lasts.

More from Kadirul Islam

Cache Policy Is a Design Decision

Immutable, revalidate, or no-store. Choosing wrong is how you ship a 200 for a URL that should not exist.

2 min readCaching

Delete the Framework From Your Blog

A blog is the one project where the runtime is pure overhead. Here is the argument, and the build that settles it.

2 min readStatic Sites

The CSS Bug That Silently Deleted a Design System

One attribute selector outranked every component class in the build. Nothing threw, nothing warned, and the page just looked wrong.

2 min readCSS

Prerender Everything and Ship a Static Site

If two visitors receive identical bytes, a server runtime is a tax you agreed to pay for nothing.

2 min readWeb Development