#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Routing and Navigation

Introduction to Single Page Applications

A single-page application, or SPA, is a web app that loads one HTML page and then updates the content with JavaScript as the user moves around, instead of asking the server for a whole new page on every click. The address bar still changes and the back button still works, but the browser never does a full reload. Apps built with React, such as dashboards, email clients and project boards, often work this way.

Traditional pages versus a single-page app

AspectMulti-page siteSingle-page app
Clicking a linkBrowser requests a new HTML page from the server.JavaScript swaps the visible components.
What the server sendsFinished HTML for each page.One HTML shell, a JavaScript bundle, then data as JSON.
Page transitionsFull reload; screen may flash.Instant change; state such as a playing video can survive.
First loadUsually fast for a single page.Can be slower, as the app's code must download first.

How an SPA changes the URL without reloading

The trick is the browser's History API. JavaScript can call history.pushState() to change the address shown in the bar without contacting the server, and listen for the popstate event when the user presses back or forward. A router library watches the URL and decides which component to show. Here is the idea stripped down to plain JavaScript:

const routes = {
  '/': () => '<h2>Home</h2>',
  '/about': () => '<h2>About us</h2>',
};

function render() {
  const view = routes[location.pathname] || (() => '<h2>Not found</h2>');
  document.getElementById('app').innerHTML = view();
}

document.addEventListener('click', (event) => {
  const link = event.target.closest('a[data-spa]');
  if (!link) return;
  event.preventDefault();                 // stop the normal page load
  history.pushState({}, '', link.href);   // change the URL
  render();                               // draw the new view
});

window.addEventListener('popstate', render);  // back/forward buttons
render();

React Router, covered in the next lessons, does the same job in a structured way, mapping paths to components instead of HTML strings.

Benefits and costs

  • Smooth navigation. Only the part of the page that changes is redrawn, and data already loaded can be reused.
  • App-like feel. Sidebars, players and open panels persist between views.
  • Clear split between front end and back end. The server becomes an API that returns JSON, which a mobile app can also use.
  • Larger first download. Code splitting and lazy loading help reduce it.
  • Extra care for search engines and sharing. Pages built entirely in the browser show little content until scripts run, so public content-heavy sites often use server rendering frameworks instead.

The 404-on-refresh problem

A classic SPA issue appears after deployment. Navigation works when you click around, but refreshing the browser on /about shows a server 404. The reason: clicking changed the URL in the browser only, while a refresh asks the server for a real file at /about, which does not exist. The fix is to configure the server or hosting platform to return index.html for any unknown path, so the app loads and the router handles the URL. Most static hosts offer a rewrite or "fallback" setting for this.

Another pitfall is forgetting the browser's built-in behaviours. When you take over navigation, you must restore scroll position, update the page <title> and move focus sensibly so keyboard and screen reader users know the view changed.

Main ideas

A single-page application loads once and swaps views with JavaScript, using the History API to keep URLs and the back button working. It feels fast after the first load but needs care with bundle size, server fallbacks and accessibility. Routing libraries manage these details, which is where the next lesson begins.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course