completefrontend

Predict the outputMedium

What CLS does this page log?

The tea shop's list of teas has a 64 px header and eight tea cards of 120 px each, more than fit on a phone's screen. A second after the load event, a script puts a 100 px offer between the header and the cards, and two seconds later it removes the offer again. Nobody touches the page.

The script collects layout-shift entries with a buffered PerformanceObserver. A second after the offer has gone, it logs the screen's size, how many shifts there were, each shift's value, how many session windows they make, and the CLS, added up the way web-vitals does it. 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>Today's teas | Leaf and Kettle</title>
  <style>
    body { margin: 0; font: 16px/1.5 system-ui, sans-serif; }
    header { height: 64px; padding: 0 16px; background: #2f5d3a; color: #fff; }
    header h1 { margin: 0; font-size: 22px; line-height: 64px; }
    #offer { height: 100px; margin: 0; padding: 0 16px; background: #f6e7c8; }
    .tea { height: 120px; margin: 0; padding: 16px; box-sizing: border-box; border-bottom: 1px solid #ddd; }
    .tea h2 { margin: 0; font-size: 18px; }
  </style>
</head>
<body>
  <header><h1>Leaf and Kettle</h1></header>
  <main id="teas">
    <article class="tea"><h2>Green tea</h2><p>Fresh and grassy.</p></article>
    <article class="tea"><h2>Mint tea</h2><p>Cool and bright.</p></article>
    <article class="tea"><h2>Oolong</h2><p>Toasty and floral.</p></article>
    <article class="tea"><h2>Black tea</h2><p>Strong, with milk.</p></article>
    <article class="tea"><h2>White tea</h2><p>Light and sweet.</p></article>
    <article class="tea"><h2>Jasmine</h2><p>Green tea and flowers.</p></article>
    <article class="tea"><h2>Rooibos</h2><p>Red bush, no caffeine.</p></article>
    <article class="tea"><h2>Chai</h2><p>Black tea and spices.</p></article>
  </main>
  <script src="shifts.js"></script>
</body>
</html>
shifts.js
// Every layout-shift entry, including any from before this script ran.
const shifts = [];
new PerformanceObserver((list) => {
  shifts.push(...list.getEntries());
}).observe({ type: "layout-shift", buffered: true });
 
// CLS as web-vitals adds it up: skip shifts right after input, group the rest into session windows
// (less than 1 s apart, less than 5 s from the window's first), and keep the largest window.
function cumulativeLayoutShift(entries) {
  const windows = [];
  for (const entry of entries) {
    if (entry.hadRecentInput) continue;
    const current = windows.at(-1);
    if (current && entry.startTime - current.last < 1000 && entry.startTime - current.first < 5000) {
      current.value += entry.value;
      current.last = entry.startTime;
    } else {
      windows.push({ first: entry.startTime, last: entry.startTime, value: entry.value });
    }
  }
  return { windows: windows.length, cls: Math.max(0, ...windows.map((item) => item.value)) };
}
 
addEventListener("load", () => {
  // A second after the load event, an offer appears between the header and the teas.
  setTimeout(() => {
    const offer = document.createElement("p");
    offer.id = "offer";
    offer.textContent = "Free delivery on orders over £30";
    document.getElementById("teas").before(offer);
  }, 1000);
 
  // Two seconds later, the offer takes itself away.
  setTimeout(() => document.getElementById("offer").remove(), 3000);
 
  // A second after that, the page logs what it saw.
  setTimeout(() => {
    const { windows, cls } = cumulativeLayoutShift(shifts);
    console.log("Screen: " + innerWidth + " x " + innerHeight);
    console.log("Layout shifts: " + shifts.length);
    shifts.forEach((entry, index) => console.log("Shift " + (index + 1) + ": " + entry.value.toFixed(3)));
    console.log("Session windows: " + windows);
    console.log("CLS: " + cls.toFixed(2));
    document.body.dataset.logged = "true";
  }, 4000);
});

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 and don't touch it.
  2. A second after the load event, the offer appears above the teas.
  3. Two seconds later, the offer removes itself.
  4. A second after that, the script logs what it saw.

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…