When someone goes back to a page, the browser can bring it back whole from the back/forward cache instead of loading it again. For the reader that's a new visit, but the page's code doesn't run again and no new paint entries arrive. web-vitals 6.2.3 handles that in every metric; write it for First Contentful Paint, as onFCP(report).
FCP is the paint entry named "first-contentful-paint". Observe { type: "paint", buffered: true }, and when that entry arrives, disconnect the observer and call report once with a plain object { name: "FCP", value, navigationType }. Skip other paint entries, like "first-paint". Where PerformanceObserver.supportedEntryTypes doesn't list "paint", report nothing at all, not even after a restore.
Read the navigation entry, performance.getEntriesByType("navigation")[0], when onFCP is called. value is the paint entry's startTime minus the navigation entry's activationStart (0 without one), and never below 0: a prerendered page counts from when the reader opened it. navigationType is "prerender" when activationStart is above 0, and otherwise the navigation entry's type with each _ turned into - ("navigate", "reload", or "back-forward"), or "navigate" when there's no navigation entry.
A restore fires pageshow on window with event.persisted true; the first load's pageshow has it false. Listen in the capture phase. At each restore, start over: note performance.now(), wait two animation frames (a requestAnimationFrame callback that requests another), and in the second, report { name: "FCP", value, navigationType: "back-forward-cache" }, where value is the time since the pageshow. (web-vitals measures from the event's timeStamp, the same moment.)
onFCP((metric) => console.log(metric.navigationType, metric.value));
// The tea shop's home page paints at 640 ms: navigate 640
// The reader opens a tea, then taps Back. The page comes back from the cache,
// and its first frame after that is painted 24 ms later: back-forward-cache 24
const perf = usePerformance();
perf.buffer("paint", [{ name: "first-paint", startTime: 610 }, { name: "first-contentful-paint", startTime: 640 }]);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
expect(perf.observers().map((observer) => [observer.types, observer.buffered])).toEqual([[["paint"], true]]);
await flush();
expect(reports).toEqual([{ name: "FCP", value: 640, navigationType: "navigate" }]);
expect(perf.observers()[0].disconnected).toBe(true);
const perf = usePerformance();
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
perf.emit("paint", { name: "first-paint", startTime: 610 });
await flush();
expect(reports).toEqual([]);
perf.emit("paint", { name: "first-contentful-paint", startTime: 640 });
await flush();
expect(reports).toEqual([{ name: "FCP", value: 640, navigationType: "navigate" }]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.navigation({ type: "back_forward", responseStart: 180 });
perf.buffer("paint", [{ name: "first-contentful-paint", startTime: 420 }]);
clock.tick(3000);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
await flush();
expect(reports).toEqual([{ name: "FCP", value: 420, navigationType: "back-forward" }]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.navigation({ type: "navigate", responseStart: 120, activationStart: 1000 });
perf.buffer("paint", [{ name: "first-contentful-paint", startTime: 1250 }]);
clock.tick(3000);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
await flush();
expect(reports).toEqual([{ name: "FCP", value: 250, navigationType: "prerender" }]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.navigation({ type: "navigate", responseStart: 120, activationStart: 1000 });
perf.buffer("paint", [{ name: "first-contentful-paint", startTime: 600 }]);
clock.tick(3000);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
await flush();
expect(reports).toEqual([{ name: "FCP", value: 0, navigationType: "prerender" }]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.navigation({ type: "reload", responseStart: 180 });
perf.buffer("paint", [{ name: "first-contentful-paint", startTime: 640 }]);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
await flush();
clock.tick(5000);
pageHide({ persisted: true });
setVisibility("hidden");
clock.tick(60000); // a minute on another page
setVisibility("visible");
pageShow({ persisted: true });
await clock.tickAsync(8); // the first frame, at 65008 ms
expect(reports).toHaveLength(1);
await clock.tickAsync(16); // the second, at 65024 ms
expect(reports).toEqual([
{ name: "FCP", value: 640, navigationType: "reload" },
{ name: "FCP", value: 24, navigationType: "back-forward-cache" },
]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.buffer("paint", [{ name: "first-contentful-paint", startTime: 640 }]);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
await flush();
pageShow({ persisted: false });
await clock.runAllAsync();
expect(reports).toHaveLength(1);
clock.tick(4000 - clock.now());
pageShow({ persisted: true }); // frames at 4016 and 4032 ms
await clock.runAllAsync();
clock.tick(14040 - clock.now());
pageShow({ persisted: true }); // frames at 14048 and 14064 ms
await clock.runAllAsync();
expect(reports).toEqual([
{ name: "FCP", value: 640, navigationType: "navigate" },
{ name: "FCP", value: 32, navigationType: "back-forward-cache" },
{ name: "FCP", value: 24, navigationType: "back-forward-cache" },
]);
const clock = useFakeTimers();
const perf = usePerformance();
perf.supported(["event", "first-input", "largest-contentful-paint", "layout-shift", "mark", "measure", "navigation", "resource"]);
const reports = [];
onFCP((metric) => reports.push({ ...metric }));
expect(perf.observers()).toEqual([]);
pageShow({ persisted: true });
await clock.runAllAsync();
expect(reports).toEqual([]);
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 Metrics after a back/forward cache restore. 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.