Skip to main content

How does SSR work with SPA routing?

1. What routing does in an SPA

In a classic SPA (without SSR):

  • The server always serves the same file, index.html.
  • It loads a JS bundle (for example, React Router).
  • When navigating between pages, the browser does not make new requests to the server, it just changes the URL and renders a different component.

The server doesn't know about routes at all, the client handles everything.


2. What SSR adds

With SSR, the server becomes a participant in the process again:

  1. The user requests /about.
  2. The server resolves the route (/about → the <About /> component).
  3. The server renders the HTML for that page and sends the finished markup.
  4. After the JS loads, the client hydrates the application, and routing continues to work just like in an SPA.

The idea is simple:

  • the first route (the first request) is rendered on the server,
  • every subsequent transition happens on the client.

3. In detail: how SSR and SPA routing interact

Stage 1: The first request (server-side routing)

  • The browser makes a request to /products/42.
  • The server (for example, Next.js) resolves that this route maps to the <ProductPage id={42}> component.
  • It performs SSR → generates HTML → sends it to the browser.

The user sees an already-rendered page (SSR HTML).


Stage 2: Hydration

  • After receiving the HTML, the browser loads the JS bundles for the routes.
  • React (or another framework) hydrates the page, attaching handlers and state, and initializes the router.
  • From this point on, client-side routing works.

Stage 3: Client-side transitions (client-side routing)

  • The user clicks a <Link to="/contact">Contact</Link> link.
  • The router (for example, React Router, the Next.js router) intercepts the click.
  • Instead of a server request:
    • the history is updated (history.pushState()),
    • the needed component (<ContactPage />) is loaded,
    • and it renders without a page reload.

The server is no longer involved, the JS in the browser does everything.


4. SSR on reload or direct navigation

If the user:

  • reloads the page (F5),
  • or navigates directly to /contact,

the server has to render the needed page again, because the browser makes a new HTTP request.

So SSR routing and SPA routing work like mirrors of each other:

  • The server handles the initial request.
  • The client handles navigation inside the application.

5. How this is implemented in frameworks

Next.js

  • Each page (app/about/page.tsx or pages/about.tsx) is a separate route.
  • On the first request, SSR renders the page component.
  • After hydration, next/router works like an SPA router, transitions are instant.
javascript
import Link from 'next/link'; export default function Nav() { return ( <nav> <Link href="/">Home</Link> <Link href="/about">About us</Link> </nav> ); }

The first navigation to /about goes through the server, and clicks on <Link> go through client-side routing.


Remix / React Router 6.4+

  • The server and client use the same router.
  • On the server, createStaticHandler() prepares the data and renders the HTML.
  • On the client, createBrowserRouter() takes the same routes and continues SPA navigation.

6. Advantages of this approach

AdvantageExplanation
Fast first loadSSR serves HTML instantly
No reload on transitionsAfter hydration, it behaves like an SPA
SEO optimizationThe search engine sees ready-made HTML
Improved UXNavigation is smooth, like in native apps
Universal routingOne set of routes works on both the server and the client

7. Potential difficulties

ProblemDescription
The router needs to be synchronizedThe same routing must work on the server and the client
A slow SSR render blocks TTFBYou need to use Streaming SSR
Hydration mismatches on routesDifferent state on the server and client → mismatch
Auth routesAccess checks must work on both the server and the client

Summary

SSR and SPA routing work together:

StageWho handles itWhat it does
First requestServerRenders the HTML for the chosen route
JS loadingClientPerforms hydration and initializes the router
Further transitionsClientChanges routes without a reload
Page reloadServerPerforms SSR again

SSR is responsible for the first render, and SPA routing is responsible for navigation after hydration.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.