completefrontend

Predict the outputMedium

What does Chrome report when a single-page app changes route?

Leaf and Kettle's shop is a single-page app. Its router handles a tap on Mint tea itself: it stops the browser from following the link, changes the address to /teas/mint with history.pushState(), and draws the tea's view, whose paragraph is bigger than anything on the first screen. The page logs its navigation entry when it loads, and half a second after the tap it logs what the performance timeline holds. What does it log in Chrome?

index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Leaf and Kettle</title>
  <style>
    body { margin: 0; padding: 16px; font-family: system-ui, sans-serif; color: #2f3b2a; }
    h1 { margin: 0 0 12px; font-size: 28px; color: #3f6f31; }
    nav a { display: inline-block; padding: 8px 12px; border-radius: 6px; background: #e8f0df; color: #2f3b2a; }
    main p { font-size: 18px; line-height: 1.5; }
  </style>
</head>
<body>
  <h1>Leaf and Kettle</h1>
  <nav><a id="mint" href="/teas/mint">Mint tea</a></nav>
  <main id="view">
    <h2>Our teas</h2>
    <p>Green, mint, and oolong.</p>
  </main>
  <script src="router.js"></script>
</body>
</html>
router.js
// The shop's router: a tap on a link changes the address and draws the new view, without loading a page.
const paints = [];
const softNavigations = [];
new PerformanceObserver((list) => paints.push(...list.getEntries())).observe({ type: "largest-contentful-paint", buffered: true });
new PerformanceObserver((list) => softNavigations.push(...list.getEntries())).observe({ type: "soft-navigation", buffered: true });
 
const tag = (element) => (element ? element.tagName.toLowerCase() : "nothing");
let pageshows = 0;
 
addEventListener("pageshow", (event) => {
  pageshows += 1;
  const [navigation] = performance.getEntriesByType("navigation");
  console.log("Loaded: navigation type " + navigation.type + ", activationStart " + navigation.activationStart + ", persisted " + event.persisted);
});
 
document.getElementById("mint").addEventListener("click", (event) => {
  event.preventDefault();
  const tappedAt = event.timeStamp;
  history.pushState({ tea: "mint" }, "", "/teas/mint");
  document.getElementById("view").innerHTML =
    "<h2>Mint tea</h2><p>Peppermint leaves from Kent, dried by hand. Brew 2 g in 250 ml of water just off the boil for 4 minutes. " +
    "It has no caffeine, so it makes a calm cup after dinner as well as after lunch.</p>";
 
  // Half a second later, the new view has long been painted: read what the timeline says about it.
  setTimeout(() => {
    const navigations = performance.getEntriesByType("navigation");
    console.log("Address " + location.pathname + ": navigation entries " + navigations.length + ", type " + navigations[0].type + ", pageshow events " + pageshows);
    const late = paints.filter((entry) => entry.startTime > tappedAt).length;
    console.log("largest-contentful-paint entries: " + paints.length + ", the last for the " + tag(paints.at(-1)?.element) + ", " + late + " after the tap");
    console.log("soft-navigation entries: " + softNavigations.length);
    for (const entry of softNavigations) {
      const largest = entry.getLargestInteractionContentfulPaint();
      console.log("Soft navigation to " + new URL(entry.name).pathname + ": navigationType " + entry.navigationType + ", largest new paint for the " + tag(largest?.largestContentfulPaint?.element));
    }
  }, 500);
});

Chrome loads these files from a server on the same computer, index.html at /, in a fresh profile with an empty cache, on a phone's screen (412 by 823 CSS pixels), with no network or CPU slowdown.

  1. You open the page in Chrome.
  2. When it has loaded, you tap Mint tea, and the router draws the Mint tea view and changes the address without loading a page.
  3. Half a second after the tap, the page logs what the performance timeline holds.

Practice it in the editor, with tests

The answer

Work out what Chrome does with the page: every line it logs, in order, and how it treats each file. Then check.

Checking your sign-in…