• Build a reliable SPA router in vanilla JavaScript without frameworks.
001

Single-page applications (SPAs) have revolutionized web development by offering smoother user experiences with smooth navigation. At the heart of every SPA lies a routing system that manages page transitions without full page reloads. In this guide, we’ll explore how to build a reliable routing system using vanilla JavaScript, understanding the core concepts and implementing them from scratch.

In a Single Page Application (SPA), routing is primarily about managing browser URLs and rendering the corresponding views. There are two common routing modes: Hash Mode and History Mode.

Hash Mode

This mode utilizes the hashchange event to update the page content without requiring a full refresh.

What is the hash property? The hash property of a URL contains a fragment identifier starting with #. This fragment is not sent to the server, meaning hash-based navigation is purely client-side.