completefrontend

Predict the outputMedium

Which images does this page request before you scroll?

The tea shop's menu page has seven pictures, each 400 by 250 px: an eager hero at the top, a lazy picture of green tea under it, a closed Brewing tab with display: none that holds an eager and a lazy picture, a lazy picture of mint tea 800 px further down, and, after a 12,000 px gap, a lazy kettle and an eager map. Which pictures does Chrome ask for before you scroll? Half a second after the load event, the page logs the connection type Chrome reports, how far below the screen the mint and the kettle start, and the pictures it found in Resource Timing.

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: the tea menu</title>
  <style>
    body { margin: 0; }
    img { display: block; width: 400px; height: 250px; }
    .closed { display: none; }
    #mint { margin-top: 800px; }
    #kettle { margin-top: 12000px; }
  </style>
</head>
<body>
  <img id="hero" src="hero.svg" alt="This week's tea: oolong" width="400" height="250">
  <img id="green" src="green.svg" alt="Green tea" width="400" height="250" loading="lazy">
  <div class="closed" id="brewing-tab">
    <img id="steeping" src="steeping.svg" alt="Steeping times" width="400" height="250">
    <img id="teapot" src="teapot.svg" alt="Warming the teapot" width="400" height="250" loading="lazy">
  </div>
  <img id="mint" src="mint.svg" alt="Mint tea" width="400" height="250" loading="lazy">
  <img id="kettle" src="kettle.svg" alt="A copper kettle" width="400" height="250" loading="lazy">
  <img id="map" src="map.svg" alt="How to find the shop" width="400" height="250">
  <script>
    addEventListener("load", () => {
      setTimeout(() => {
        const file = (url) => url.split("/").pop();
        const below = (id) => Math.round(document.getElementById(id).getBoundingClientRect().top - innerHeight);
        const requested = performance.getEntriesByType("resource").map((entry) => file(entry.name)).filter((name) => name.endsWith(".svg")).sort();
        const skipped = [...document.images].map((image) => file(image.src)).filter((name) => !requested.includes(name)).sort();
        console.log("Connection: " + navigator.connection.effectiveType);
        console.log("Below the screen: mint.svg " + below("mint") + " px, kettle.svg " + below("kettle") + " px");
        console.log("Requested: " + requested.join(", "));
        console.log("Not requested: " + skipped.join(", "));
      }, 500);
    });
  </script>
</body>
</html>
hero.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#8a5a2b"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Oolong</text>
</svg>
green.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#5f8f3a"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Green tea</text>
</svg>
steeping.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#3f6f7a"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Steeping</text>
</svg>
teapot.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#7a4f6f"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Teapot</text>
</svg>
mint.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#3f8f6a"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Mint tea</text>
</svg>
kettle.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#b0502a"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Kettle</text>
</svg>
map.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250" width="400" height="250">
  <rect width="400" height="250" fill="#4a5a8a"/>
  <text x="200" y="125" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="40" fill="#fff">Find us</text>
</svg>

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. The server holds back hero.svg for 300 ms, steeping.svg for 400 ms, map.svg for 500 ms, green.svg for 600 ms and mint.svg for 700 ms before it answers.

  1. You open the page in Chrome and don't scroll.
  2. Half a second after the load event, its script logs what it found.

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…