liveBook · Manning

Single-page application

As explained in Build a Frontend Web Framework (From Scratch)

A web application that updates its displayed content and navigation within one initially loaded document rather than requesting a new page for each route.

How can a URL change and show a new view without loading another page? In a single-page application (SPA), the browser loads one HTML file. JavaScript creates the view, then updates it as the application state or route changes. The browser does not reload the page for each route. section 1.3.2 section 1.4

The work happens in a few steps:

  • The server returns a mostly empty HTML file containing references to JavaScript and CSS bundles. section 1.3.2
  • The browser loads those files. Framework code uses the Document API to create the first view; this is called mounting. section 1.3.2
  • When the route changes, a router updates the address bar and the view instead of reloading the application. section 1.3.2

Creating part of a view

Starting with an empty HTML body, JavaScript can create a paragraph, set its text to “Hello, World!”, and append it to the document. Mounting an SPA uses the same Document API principle to create its view from components. section 1.3.2

const paragraph = document.createElement('p')
paragraph.textContent = 'Hello, World!'
document.append(paragraph)

This small listing shows the element creation and text-setting operations behind the paragraph example. section 1.3.2

The HTML file is only the starting point, not a separate page for every view. A build can bundle application code, third-party code, and CSS into files the browser loads; the framework then updates the displayed HTML as needed. section 1.3.1 section 1.4

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Walks through loading, mounting, and route updates, but does not provide a router implementation.
Examples
Illustrates Document API element creation with a “Hello, World!” paragraph and diagrams of loading, mounting, and navigation.
What it assumes
Introduces Document API element creation while using basic HTML, JavaScript, and CSS terminology.
What you can do after
Evaluate/understand it by tracing how one loaded document becomes a view and changes when the route changes.
How it connects
Connects routing and application state to component rendering, the Document API, and build bundles.

Also explained in

Compare how 5 books explain single-page application →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.