Skip to the code

The final LCP

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] }

What the tests check

  1. the last candidate before web-vitals stops after a click is the LCP
    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);
    
  2. a candidate that arrives between the click and the idle callback still counts
    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] });
    
  3. a key press waits for the idle callback too, and hiding the page stops it at once
    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] });
    
  4. events a script made, and other events, don't stop it
    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] });
    
  5. until it stops, the last candidate so far is the LCP, and with no candidate it's null
    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);
    
  6. a prerendered page is measured from activationStart, and never below 0
    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] });
    
  7. a candidate from after the page was first hidden doesn't count
    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] });
    
  8. a page that loaded in a background tab reports no LCP
    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);
    

Learn more

solution.jsSaved in this browser

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).