Page Routing

In modern web development, single-page applications (SPAs) have become a popular way to build fast, interactive interfaces. A key part of this architecture is page routing, sometimes called routing. Routing decides which view a user sees when they visit a URL, manages transitions between views, keeps browser history in sync, and supports deep linking.

It is not just about moving between pages. It is about creating a clear and predictable relationship between URLs and interface states.

From Multi-Page Apps to Client-Side Routing

Traditional websites often rely on multi-page architecture. When a user clicks a link, the browser sends a request to a server, and the server returns a new HTML page. The whole page reloads, which can interrupt the experience.

Client-side routing changes this flow. Instead of reloading, the application listens for URL changes and renders the relevant component. The result is faster navigation and a more fluid feel.

Common benefits include:

  • Seamless experience: View switches feel immediate, without blank screens.
  • State preservation: Global state can survive route changes.
  • Frontend-backend separation: The frontend handles rendering and matching, while the backend focuses on APIs.

How Client-Side Routing Works

Modern routing usually depends on the HTML5 History API or hash routing.

  • Hash routing: Uses window.location.hash. URLs may include a fragment, such as http://example.com/#/about. Changing the hash does not reload the page. A hashchange event fires, and the app renders the matching component.
  • History routing: Uses standard URL paths, such as http://example.com/about. Methods like history.pushState() and history.replaceState() update the URL without reloading. The popstate event handles browser back and forward actions.

A React Router Example

React Router is one of the most widely used routing libraries in the React ecosystem. The following example demonstrates a basic setup.

npm install react-router-dom
import React from 'react';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

function Home() {
  return <h2>Home Page</h2>;
}

function About() {
  return <h2>About Page</h2>;
}

function Profile() {
  return <h2>User Profile</h2>;
}

function App() {
  return (
    <BrowserRouter>
      <div>
        <nav>
          <ul>
            <li><Link to="/">Home</Link></li>
            <li><Link to="/about">About</Link></li>
            <li><Link to="/profile">Profile</Link></li>
          </ul>
        </nav>

        <hr />

        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
          <Route path="/profile" element={<Profile />} />
        </Routes>
      </div>
    </BrowserRouter>
  );
}

export default App;

In this example:

  • BrowserRouter enables routing through the HTML5 History API.
  • Link provides client-side navigation without full reloads.
  • Route maps a path to a component through element.

Advanced Routing Features

Basic navigation is useful, but production applications often need more control.

  • Dynamic segments: Routes can accept parameters, such as /user/:id. Components can read values with hooks like useParams.
  • Nested routing: Parent routes can render child routes. Layouts may stay stable while content changes.
  • Route guards: Guards can run before navigation, check permissions, and redirect users.

Choosing a Routing Strategy

The right approach depends on the deployment environment, URL structure, and performance requirements. Hash routing can be simpler for static hosting. History routing offers cleaner URLs but may require server configuration.

Conclusion

Page routing is a foundational part of modern web applications. Understanding how hash and History APIs work, and mastering routing tools in popular frameworks, helps developers build clear, smooth, and maintainable experiences.