The browser reports Largest Contentful Paint as a series of candidates: each time something larger paints, a new largest-contentful-paint entry. Write finalLCP(timeline, options) that returns the LCP web-vitals 6.2.3 reports, as { value, entries } with the one candidate it reports, or null when no candidate counts.
timeline lists what web-vitals saw, in order. A candidate is { type: "largest-contentful-paint", startTime, url }, with startTime the time it painted. An event is { type, timeStamp, isTrusted }, such as a "click", a "keydown", or a "pointermove"; isTrusted is false for an event a script made, as button.click() does. A "visibilitychange" event also has the visibilityState the page changed to. { type: "idle" } is the browser running an idle callback.
Each candidate that counts replaces the one before. A trusted keydown, click, or visibilitychange tells web-vitals to stop, but it stops in its next idle callback, so it doesn't slow the interaction down: at the next idle item, or at a visibilitychange if one comes first. A hidden page doesn't wait: a trusted visibilitychange to "hidden" stops it at once. Candidates that come before it stops still count, and nothing after counts. Events with isTrusted: false and other events, like pointermove, change nothing. If the timeline ends first, return what it holds then: that's what it reports when the page is hidden.
options.activationStart (0 unless given) is the navigation entry's activationStart. A prerendered page is measured from when the user opened it, so a candidate's value is startTime - activationStart, and 0 if it painted before that. options.firstHiddenTime (Infinity unless given) is when the page was first hidden before the code ran, as web-vitals finds it from a visibility-state entry, and 0 for a page that loaded in a background tab. A candidate counts only if its startTime is earlier than firstHiddenTime.
finalLCP([
{ type: "largest-contentful-paint", startTime: 820, url: "" }, // the shop's name
{ type: "click", timeStamp: 1500, isTrusted: true }, // "Add to basket"
{ type: "largest-contentful-paint", startTime: 1460, url: "/teas/oolong.avif" }, // painted before the click
{ type: "idle" }, // web-vitals stops here
{ type: "largest-contentful-paint", startTime: 1490, url: "/offers/banner.avif" },
]);
// { value: 1460, entries: [the oolong candidate] }
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const heading = paint(820);
const photo = paint(1460, "/teas/oolong.avif");
const banner = paint(1490, "/offers/banner.avif");
const result = finalLCP([heading, photo, { type: "click", timeStamp: 2100, isTrusted: true }, { type: "idle" }, banner]);
expect(result).toEqual({ value: 1460, entries: [photo] });
expect(result.entries[0]).toBe(photo);
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const heading = paint(820);
const photo = paint(1460, "/teas/oolong.avif");
const banner = paint(1490, "/offers/banner.avif");
expect(finalLCP([heading, { type: "click", timeStamp: 1500, isTrusted: true }, photo, { type: "idle" }, banner])).toEqual({ value: 1460, entries: [photo] });
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const hidden = (timeStamp) => ({ type: "visibilitychange", timeStamp, isTrusted: true, visibilityState: "hidden" });
const heading = paint(820);
const photo = paint(1250, "/teas/oolong.avif");
expect(finalLCP([heading, { type: "keydown", timeStamp: 1300, isTrusted: true }, { type: "idle" }, photo])).toEqual({ value: 820, entries: [heading] });
expect(finalLCP([heading, hidden(1300), photo, { type: "idle" }])).toEqual({ value: 820, entries: [heading] });
expect(finalLCP([heading, { type: "click", timeStamp: 1280, isTrusted: true }, hidden(1300), photo, { type: "idle" }])).toEqual({ value: 820, entries: [heading] });
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const heading = paint(820);
const photo = paint(1460, "/teas/oolong.avif");
const timeline = [
heading,
{ type: "click", timeStamp: 900, isTrusted: false },
{ type: "keydown", timeStamp: 950, isTrusted: false },
{ type: "pointermove", timeStamp: 1000, isTrusted: true },
{ type: "idle" },
photo,
];
expect(finalLCP(timeline)).toEqual({ value: 1460, entries: [photo] });
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const heading = paint(820);
const photo = paint(1460, "/teas/oolong.avif");
expect(finalLCP([heading, photo])).toEqual({ value: 1460, entries: [photo] });
expect(finalLCP([heading, { type: "click", timeStamp: 1500, isTrusted: true }, photo])).toEqual({ value: 1460, entries: [photo] });
expect(finalLCP([])).toBe(null);
expect(finalLCP([{ type: "click", timeStamp: 300, isTrusted: true }, { type: "idle" }, photo])).toBe(null);
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const early = paint(600);
const late = paint(1800, "/teas/green.avif");
expect(finalLCP([early], { activationStart: 1000 })).toEqual({ value: 0, entries: [early] });
expect(finalLCP([early, late], { activationStart: 1000 })).toEqual({ value: 800, entries: [late] });
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const before = paint(2000, "/teas/mint.avif");
expect(finalLCP([before, paint(3000, "/teas/mint-large.avif"), paint(3500, "/teas/mint-hero.avif")], { firstHiddenTime: 3000 })).toEqual({ value: 2000, entries: [before] });
const paint = (startTime, url = "") => ({ type: "largest-contentful-paint", startTime, url });
const timeline = [paint(400), paint(900, "/teas/oolong.avif"), { type: "visibilitychange", timeStamp: 1500, isTrusted: true, visibilityState: "visible" }, { type: "idle" }];
expect(finalLCP(timeline, { firstHiddenTime: 0 })).toBe(null);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with The final LCP. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.