Skip to content

Instantly share code, notes, and snippets.

@jpnelson
Created December 1, 2021 08:08
Show Gist options
  • Save jpnelson/958cf566e752c8d24a6273576ea6dd6b to your computer and use it in GitHub Desktop.
Save jpnelson/958cf566e752c8d24a6273576ea6dd6b to your computer and use it in GitHub Desktop.
​​let lastHeroElement;
// Simplified version of our TTFMP instrumentation
function recursiveCheckForFirstMeaningfulPaint() {
const heroElement = document.getElementById("FMP-target");
// To prevent against false positives during client-side route requests
const isSameHeroElement = lastHeroElement === heroElement;
const isImageHeroLoading =
heroElement.tagName === "IMG" && !heroElement.complete;
if (!heroElement || isSameHeroElement || isImageHeroLoading) {
requestAnimationFrame(recursiveCheckForFirstMeaningfulPaint);
} else {
requestAnimationFrame(() => {
lastHeroElement = heroElement;
performance.measure("TTFMP");
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment