Bookmarks

SPA — single-page application

A web application made of one page: the browser loads the HTML with the application code once, and then fetches data and switches “pages” over HTTP without a full reload.

Also: single page application, SPA, single-page app

checked 9 Nov

All terms

On a classic website every navigation is a new request to the server and a new HTML page. In an SPA there are no such navigations: the browser loads the application once, the code then redraws the content itself, and it contacts the server only for data, usually JSON.

How it works

The first request returns almost empty HTML: a shell of markup and a link to JavaScript. The browser downloads and runs the code, which builds the interface in the DOM and asks the server for data. From then on the application lives on the client: JavaScript handles clicks, fetch retrieves new data, and the page is never fully reloaded.

javascriptThis is what a “page update” looks like in an SPA: no navigation to a new document
const list = await fetch("/api/tasks").then((r) => r.json());document.querySelector("#app").innerHTML = render(list);  // redrew the list without a reload

Routing in the browser

The address changes without navigating to a new document. The old way stores the route after #: the URL fragment is not sent to the server, so changing it does not load a page. The modern way is the History API: pushState adds an entry to the browser history and changes the address bar without reloading the document. React Router, Vue Router and Angular routers use exactly this.

javascriptHistory API: the address changes, the page does not reload
history.pushState({ page: "task" }, "", "/tasks/42");// the address bar shows /tasks/42, the document is the same; when “Back” is pressed// the browser fires a popstate event and the router renders the previous view

The History API has a restriction: the new address must be in the same origin as the current document. It also has a cost: the server gives a full response only for the application’s entry address and does not know the other routes, so opening a link in a new tab or refreshing the page requires server configuration: serving the same HTML shell for any application route.

SEO drawbacks

A search crawler first scans the HTML. In an SPA the first HTML is almost empty, so the crawler puts the page in a separate rendering queue and runs the JavaScript in a headless browser on its own resources; the queue takes from a few seconds to longer. The second problem is server responses: an SPA answers every address with the same shell and a 200 status, so a non-existent page looks real to the crawler, producing a soft 404. There are also meta tags: all “pages” share one title and description unless the code replaces them on the client.

  • The first HTML is almost empty: content appears only after JavaScript runs, and rendering in the crawler’s queue can take more than a few seconds.
  • The server answers 200 for any route, so non-existent addresses look like a soft 404.
  • Links must be real <a href> elements: navigation built on clicks on a div does not let the crawler find pages.
  • Metadata is shared across the whole application unless the code replaces title and description for each route.

The remedy is to return HTML from the server: SSR or prerendering, which are built into Next.js and Nuxt. An SPA is best built as an application rather than a site: dashboards, editors and admin panels are interfaces behind a login, where content changes often and search traffic is not needed. Content sites and storefronts are usually built with SSR or static generation, so that HTML with the content arrives immediately, for people and crawlers alike.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • SSRserver-side rendering

    SSR sends ready-made HTML from the server on every request; an SPA sends a shell and builds the interface with JavaScript in the browser.

In depth in the references

where the topic is covered in detail