Created
June 14, 2024 13:41
-
-
Save thinog/9bb3592c6768c1996a50df03fefe9312 to your computer and use it in GitHub Desktop.
Script minified in https://github.com/thinog/bad-web-vitals-workshop to force people to defer the execution of non-critical scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function run() { | |
const delayMs = 1000; | |
const startedAt = Date.now(); | |
console.log("[Workshop] Starting a third-party script not so important 😃"); | |
if (document.currentScript.defer) { | |
await new Promise((resolve) => setTimeout(resolve, delayMs)); | |
} else { | |
const waitUntil = Date.now() + delayMs; | |
while (Date.now() < waitUntil) continue; | |
} | |
const finishedAt = Date.now() - startedAt; | |
console.log( | |
`[Workshop] The not-so-important third-party script has been executed! It only took ${finishedAt} ms 🙏`, | |
); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment