JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptSingle Page Applications
PrevNext
javascript
beginner
10 min read

Single Page Applications

architecture
client-side
history-api
routing
spa
ssr

A Single Page Application loads one HTML document and uses JavaScript to dynamically rewrite content and manage navigation via the History API, avoiding full page reloads.

Key Points

1Single Page Architecture

Loads one HTML document and uses JavaScript to dynamically rewrite content on navigation — no full page reloads after the initial load.

2Client-Side Routing

The History API (pushState, replaceState, popstate) changes the URL without server requests. Hash routing (#/path) is the legacy alternative.

3SPA Tradeoffs

Fast navigation and rich UX after initial load, but larger JS bundles, SEO challenges, and potential memory leaks in long-running sessions.

4SPA vs MPA

SPAs handle navigation client-side for fluid UX; MPAs load fresh HTML per page for better SEO and simpler architecture.

5Modern Hybrid Solutions

SSR, SSG, and frameworks like Next.js combine SPA-style navigation with server-rendered initial loads, solving the traditional SPA vs MPA tradeoffs.

What You'll Learn

  • Understand how Single Page Applications work and how client-side routing is implemented
  • Know the tradeoffs between SPAs and Multi-Page Applications
  • Explain how SSR and SSG address SPA limitations

Deep Dive

A Single Page Application (SPA) loads a single HTML page and dynamically updates content as the user navigates, rather than requesting new HTML pages from the server. This architecture powers apps like Gmail, Google Maps, and Spotify Web, delivering fluid, app-like experiences in the browser.

How SPAs Work

The browser loads one HTML file containing a JavaScript bundle. When the user clicks a link, JavaScript intercepts the click, prevents the default navigation, fetches any needed data via API calls, and updates the DOM — all without a full page reload. The URL changes to reflect the new view, but the page itself never unloads.

Client-Side Routing

SPAs need to update the URL without triggering a server request. Two approaches exist:

  • History API routing (modern): Uses history.pushState(state, title, url) to add entries to browser history and history.replaceState() to update the current entry — both change the URL without a server request. The popstate event fires when the user clicks back/forward buttons, allowing the app to restore the previous view. Produces clean URLs like /about but requires server configuration to redirect all paths to index.html.
  • Hash routing (legacy): Uses the URL fragment (#/about). The browser ignores the hash when making server requests, so refreshing always works without server config. Uses the hashchange event for navigation. Considered legacy due to less clean URLs and weaker SEO.

SPA vs Multi-Page Application (MPA)

SPA advantages: fast navigation after initial load (no round-trips to the server), rich interactive UX, clean separation between frontend and backend, and potential for offline functionality with service workers. SPA disadvantages: larger initial JavaScript bundle means slower first page load, SEO challenges because the initial HTML is mostly empty, potential memory leaks in long-running sessions from uncleaned event listeners and detached DOM nodes, and the back button can behave unexpectedly if routing isn't implemented carefully.

MPA advantages: each page is a separate HTML document that search engines can crawl easily, smaller per-page JavaScript bundles, and simpler architecture for content-heavy sites. MPA disadvantages: full page reloads on every navigation feel slower, shared state across pages requires cookies or server sessions, and the user experience is less fluid.

Modern Solutions to SPA Limitations

Frameworks have evolved to address SPA weaknesses:

  • Server-Side Rendering (SSR): The server renders the initial HTML with full content, then the browser "hydrates" it by attaching event listeners. Improves SEO and first contentful paint. Used by Next.js (React), Nuxt (Vue), and SvelteKit.
  • Static Site Generation (SSG): Pre-renders pages at build time into static HTML. Best for content that rarely changes (blogs, docs). Fastest possible initial load.
  • Hybrid approaches: Modern frameworks like Next.js combine SPA-style client navigation with SSR/SSG for initial loads, getting the best of both worlds. React Server Components take this further by streaming server-rendered content without shipping component JavaScript to the client.

Key Interview Distinction

SPAs trade initial load time for faster subsequent navigation. The History API (pushState/replaceState + popstate) is the mechanism that makes client-side routing possible. Modern meta-frameworks (Next.js, Nuxt, SvelteKit) have largely solved the SPA vs MPA tradeoff by offering SSR, SSG, and client-side navigation in a single framework.

Fun Fact

Gmail, launched in 2004, was one of the first mainstream SPAs — it used XMLHttpRequest (later known as AJAX) to load emails without page reloads, a technique so novel at the time that the term 'AJAX' wasn't even coined until a year later.

Learn These First

DOM

beginner

Continue Learning

DOM

beginner

Events

beginner

Practice What You Learned

What is a Single Page Application (SPA) and how does it work?
junior
spa
A Single Page Application loads one HTML page and dynamically updates content using JavaScript without full page reloads. The browser handles routing client-side, fetching data via APIs and re-rendering only the parts that change.
Previous
Closures
Next
Data Structures
PrevNext