A tiny hash router

Show a different view for each #/path without reloading the page.

JavaScript · A tiny hash routerIntermediate
const routes = {
  '/':      () => '<h1>Home</h1>',
  '/about': () => '<h1>About</h1>',
};

function render() {
  const path = location.hash.slice(1) || '/';
  const view = routes[path] ?? (() => '<h1>Not found</h1>');
  document.querySelector('#app').innerHTML = view();
}

window.addEventListener('hashchange', render);
render();
routerhashchangespanavigation