Skip to content
Back to blog
Performance5 minute read

Measuring Layout Shift in Real User Sessions

Turning cumulative layout shift from a lab score into a signal you can trace back to a specific component and release.

ASRaka Mahendra
  • Performance
  • Web Vitals
  • Observability
  • CSS
Cover art for “Measuring Layout Shift in Real User Sessions”

Lighthouse gives you one CLS number for one visit on one machine. Real users arrive on slower phones, with extensions, and in the middle of a page transition. Field data tells you whether the score you celebrated is the score people experience.

Capture shifts with attribution

PerformanceObserver with type: 'layout-shift' reports entries with a value and, in Chromium, a sources list. The sources point at the nodes that moved. Log their selectors: a shift you can attribute is a shift you can fix.

let cls = 0;
new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.hadRecentInput) continue;
    cls += entry.value;
    const nodes = (entry as PerformanceEntry & { sources?: { node?: Node }[] }).sources;
    report({ cls, nodes: nodes?.map((s) => describe(s.node)) ?? [] });
  }
}).observe({ type: 'layout-shift', buffered: true });

Ignore input-driven movement

A click that legitimately changes the layout should not be counted. The hadRecentInput flag exists for this reason. Dropping those entries keeps the metric focused on movement the user did not ask for.

Segment by route and build

An average across the whole site hides the one page that regressed. Tag each sample with the route template and release identifier, then compare percentiles release over release. A p75 that jumps by 0.05 after a deploy is more useful than a quarterly average.

Reserve space before you need it

Most fixes are unglamorous: explicit dimensions on images, a fixed height for async banners, and skeletons that match the final row height. Field data tells you which of those to do first.

Copied