completefrontend

Predict the outputMedium

In what order do these scripts run?

The tea shop's page has five scripts in its head: an inline script, then a classic <script src>, a defer script, an async script, and a type="module" script, in that order. Each one logs its name and document.readyState when it runs, and the inline script also logs when DOMContentLoaded and load fire. The server holds each file back for a different time: classic.js 50 ms, module.js 200 ms, async.js 400 ms, and defer.js 800 ms. In what order are the lines logged?

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>
  <script>
    console.log("inline script ran, readyState " + document.readyState);
    document.addEventListener("DOMContentLoaded", () => console.log("DOMContentLoaded fired, readyState " + document.readyState));
    addEventListener("load", () => console.log("load fired, readyState " + document.readyState));
  </script>
  <script src="classic.js"></script>
  <script src="defer.js" defer></script>
  <script src="async.js" async></script>
  <script src="module.js" type="module"></script>
</head>
<body>
  <h1>Leaf and Kettle</h1>
  <p>This week: mint tea.</p>
</body>
</html>
classic.js
console.log("classic.js ran, readyState " + document.readyState);
defer.js
console.log("defer.js ran, readyState " + document.readyState);
async.js
console.log("async.js ran, readyState " + document.readyState);
module.js
console.log("module.js ran, readyState " + document.readyState);

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 classic.js for 50 ms, module.js for 200 ms, async.js for 400 ms and defer.js for 800 ms before it answers.

  1. You open the page in Chrome and don't touch it.
  2. Each script logs a line when it runs, and the page logs when DOMContentLoaded and load fire.

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…