JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Single Page Applications

javascript
junior
spa

What is a Single Page Application (SPA) and how does it work?

spa
routing
client-side
architecture
fundamentals
Quick Answer

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.

Detailed Explanation

A Single Page Application (SPA) is a web application that loads a single HTML document and dynamically rewrites the page content as the user interacts with it.

How SPAs work:

  1. Browser loads one HTML file with a JavaScript bundle
  2. JavaScript takes over rendering — no server round-trips for new pages
  3. Client-side router handles URL changes (pushes to browser history)
  4. Data is fetched via APIs (REST/GraphQL) and rendered dynamically
  5. Only changed components re-render, not the full page

SPA vs Traditional (MPA): | SPA | Multi-Page App | |-----|---------------| | One HTML page | Multiple HTML pages | | Client-side routing | Server-side routing | | Faster navigation | Full page reloads | | Heavier initial load | Lighter per-page load | | SEO requires SSR/SSG | SEO built-in |

Examples: Gmail, Google Maps, Trello, Spotify Web

Frameworks: React, Vue, Angular, Svelte

Code Examples

SPA routing conceptJavaScript
// SPA entry point — one HTML file
// <div id="root"></div>

// Client-side routing (simplified)
window.addEventListener('popstate', () => {
  renderPage(window.location.pathname);
});

function navigate(path) {
  window.history.pushState({}, '', path);
  renderPage(path);
}

function renderPage(path) {
  const root = document.getElementById('root');
  switch (path) {
    case '/':
      root.innerHTML = '<h1>Home</h1>';
      break;
    case '/about':
      root.innerHTML = '<h1>About</h1>';
      break;
    default:
      root.innerHTML = '<h1>404</h1>';
  }
}

Real-World Applications

Use Cases

Admin Dashboards

Complex CRUD interfaces with navigation between views without full page reloads

Real-time Collaboration

Apps like document editors and chat where maintaining persistent client state across views is critical

Progressive Web Apps

SPAs combined with service workers for offline-capable mobile-like experiences in the browser

Mini Projects

Mini SPA Router

beginner

Build a simple hash-based router that renders different views without page reloads

History API SPA

intermediate

Build a SPA using pushState and popstate with dynamic content loading and URL management

Industry Examples

React Router

Standard client-side routing library for React SPAs with nested routes, data loading, and code splitting

Vue Router

Official Vue.js routing library with navigation guards, lazy loading, and dynamic route matching

Resources

MDN - SPA (Single-page application)

docs

MDN - History API

docs
Previous
What is the ternary operator and how does it differ from an if-else statement?
Next
How do destructuring assignment and spread/rest operators work in JavaScript?
PrevNext