Skip to content

Instantly share code, notes, and snippets.

@StephenFluin
Created April 29, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenFluin/b0a9cf3e555dca1441f89d4e0db0370d to your computer and use it in GitHub Desktop.
Save StephenFluin/b0a9cf3e555dca1441f89d4e0db0370d to your computer and use it in GitHub Desktop.
let lcp = await page.evaluate(() => new Promise((resolve, reject) => {
try {
// Create a variable to hold the latest LCP value (since it can change).
let lcp;
// Create the PerformanceObserver instance.
const po = new PerformanceObserver((entryList) => {
const entries = entryList.getEntries();
const lastEntry = entries[entries.length - 1];
lcp = (<any>lastEntry).renderTime || (<any>lastEntry).loadTime;
resolve(lcp);
po.disconnect();
});
// Observe entries of type `largest-contentful-paint`, including buffered
// entries, i.e. entries that occurred before calling `observe()`.
po.observe({ type: 'largest-contentful-paint', buffered: true });
} catch (e) {
// Do nothing if the browser doesn't support this API.
}
}));
console.log('lcp is',lcp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment