日本語

How CLS Groups Layout Shifts in Core Web Vitals

​​Core Web Vitals​JavaScript,Playwright,web-vitals

The Cumulative Layout Shift (CLS) in Core Web Vitals is not the sum of all layout shifts on a page. It adds up shifts that occur less than 1 second after the previous one, in groups lasting up to 5 seconds, and reports the value of the largest group. (According to web.dev's explanation of CLS, the earlier definition was the sum of all shifts while the page was open.)

In my test environment, I caused three shifts of the same size. When the gap between them was 0.9 seconds, CLS was 0.1386. At 1.1 seconds, it was 0.0464. This article explains how to read CLS values on pages where ads, images, or notice bars are inserted one after another while the page loads, causing the layout to move.

Note

The test environment was Ubuntu 24.04, Node.js 22, and web-vitals 5.3.0. I used Chromium 157 (headless) bundled with Playwright 1.63.0, and the viewport was 412px wide and 823px high. The test page was served from a small server I ran locally at localhost:8080. I measured CLS with onCLS from web-vitals, loaded on the page. I did not test production Chrome, browsers other than Chromium, Lighthouse, Chrome DevTools, or real user data such as PageSpeed Insights field data or Search Console.

The test page

The page inserts a gray bar 40px high below the header at set times. Each insertion pushes the content box below it down, causing one shift. The URL parameter at lists "milliseconds since page open:bar height (px)" pairs, which control when and by how much the layout moves.

html
<header>猫の手日記</header>
<div id="bars"></div>
<main id="main"></main>
<script src="/web-vitals.iife.js"></script>
<script>
// List entries like at=1000:40,1900:40, each "milliseconds since page open:bar height (px)"
const p = new URLSearchParams(location.search);
const plan = (p.get('at') || '').split(',').filter(Boolean).map((s) => s.split(':').map(Number));
…
for (const [t, h] of plan) {
  setTimeout(() => {
    const d = document.createElement('div');
    d.className = 'bar'; d.style.height = h + 'px';
    document.getElementById('bars').prepend(d);
  }, Math.max(0, t - performance.now()));
}
</script>

(I omitted the <head> section, the code that builds the content box, and the recording code.)

On the page, I used PerformanceObserver to receive layout-shift entries and recorded the time and value of each shift. I also registered onCLS from web-vitals with reportAllChanges: true. Each time CLS was reported, I recorded its value and the times of the shifts it was based on (entries).

I ran the measurements with a Playwright script. The script regroups the recorded shifts using two rules, "less than 1 second after the previous shift" and "less than 5 seconds after the first shift of the group," and calculates the total for each group. In the output below, each window# line is one group. All times are milliseconds since the page was opened.

A 0.9-second vs. 1.1-second gap made a threefold difference in CLS

I inserted bars at 1 second, 1.9 seconds, and 2.8 seconds. The shifts occurred three times, 0.9 seconds apart. All three fell into one session window, and CLS was the total of the three, 0.1386.

console
== gap-900 (?at=1000:40,1900:40,2800:40)
  shift t=1002 value=0.0457 hadRecentInput=false
  shift t=1901 (前のずれから+899ms) value=0.0464 hadRecentInput=false
  shift t=2801 (前のずれから+900ms) value=0.0464 hadRecentInput=false
  window#1 1002〜2801ms(1799ms) shifts=3 sum=0.1386
  total=0.1386 max(window)=0.1386
  onCLS report at=46 value=0.0000 entries=[]
  onCLS report at=1003 value=0.0457 entries=[1002]
  onCLS report at=1902 value=0.0921 entries=[1002,1901]
  onCLS report at=2801 value=0.1386 entries=[1002,1901,2801]

I moved the bars to 1 second, 2.1 seconds, and 3.2 seconds, which widened the gap to 1.1 seconds. The shift values were the same, but each of the three shifts landed in its own window. CLS was 0.0464, the value of a single shift. The sum of all three is still 0.1386, the same as with the 0.9-second gap.

console
== gap-1100 (?at=1000:40,2100:40,3200:40)
  shift t=1002 value=0.0457 hadRecentInput=false
  shift t=2101 (前のずれから+1099ms) value=0.0464 hadRecentInput=false
  shift t=3201 (前のずれから+1100ms) value=0.0464 hadRecentInput=false
  window#1 1002〜1002ms(0ms) shifts=1 sum=0.0457
  window#2 2101〜2101ms(0ms) shifts=1 sum=0.0464
  window#3 3201〜3201ms(0ms) shifts=1 sum=0.0464
  total=0.1386 max(window)=0.0464
  onCLS report at=49 value=0.0000 entries=[]
  onCLS report at=1003 value=0.0457 entries=[1002]
  onCLS report at=2102 value=0.0464 entries=[2101]

The web.dev explanation of CLS calls these groups "session windows." A session window is a run of shifts where each shift is less than 1 second after the previous one and the whole run lasts at most 5 seconds. CLS is the total of the session window with the largest sum. The same explanation gives 0.1 or less as the threshold for a "good" CLS score. The page with a 0.9-second gap exceeded that threshold, and the page with a 1.1-second gap stayed within it.

Windows were cut off at 5 seconds

Even when the gap between shifts is under 1 second, a window does not keep growing indefinitely. I shifted the layout 14 times, every 0.6 seconds from 1 second to 8.8 seconds. The shifts formed two windows.

console
== every-600 (?at=1000:40,1600:40,2200:40,…,8200:40,8800:40)
  shift t=1001 value=0.0457 hadRecentInput=false
  shift t=1601 (前のずれから+600ms) value=0.0464 hadRecentInput=false
  …
  shift t=5801 (前のずれから+600ms) value=0.0464 hadRecentInput=false
  shift t=6401 (前のずれから+600ms) value=0.0464 hadRecentInput=false
  …
  shift t=8801 (前のずれから+600ms) value=0.0464 hadRecentInput=false
  window#1 1001〜5801ms(4800ms) shifts=9 sum=0.4171
  window#2 6401〜8801ms(2400ms) shifts=5 sum=0.2321
  total=0.6493 max(window)=0.4171
  …
  onCLS report at=5202 value=0.3707 entries=[1001,1601,2201,2801,3401,4001,4601,5201]
  onCLS report at=5802 value=0.4171 entries=[1001,1601,2201,2801,3401,4001,4601,5201,5801]

(I omitted the ?at= sequence in the output, the middle shift lines, and the middle onCLS reports.)

The shift at 6.4 seconds came only 0.6 seconds after the previous one. However, it was more than 5 seconds after the first shift of the current window (at 1.0 seconds), so it started a new window. CLS was 0.4171, the sum of the first nine shifts, not 0.6493, the sum of all 14. The last onCLS report came at the 5.8-second shift, so nothing was reported while the second window was building.

Three timelines. The three shifts 0.9 seconds apart fall into one box, giving a CLS of 0.1386. The three shifts 1.1 seconds apart are in separate boxes, giving a CLS of 0.0464. The 14 shifts 0.6 seconds apart split into two boxes: one from 1 to 5.8 seconds with 9 shifts, and one from 6.4 to 8.8 seconds with 5 shifts. The CLS is 0.4171 from the first box.
The value of each shift (bars) and the windows they form (boxes). The pink box is the window that became the CLS

The web.dev article on evolving CLS, which explains the change to how CLS is measured, gives two reasons for the 5-second limit. First, if windows were too short, pages that load or respond slowly would split their shifts into several windows and get a better score. Second, on pages where small shifts keep happening, such as a sports scoreboard, the discomfort for the viewer does not keep increasing over time, so a limit on window length was needed. The article says the 1-second gap was chosen based on initial research and worked well in large-scale analysis.

CLS is the largest window, not the total

No matter how many windows there are, only one of them counts toward CLS. At 1 second, I inserted a 150px bar to cause a large shift (0.1683). Then I caused three small shifts at 3 seconds, spaced 0.5 seconds apart. The small-shift window totaled 0.1393, which is less than the first large shift (0.1683), so CLS stayed at 0.1683.

console
== big-then-3small (?at=1000:150,3000:40,3500:40,4000:40)
  shift t=1002 value=0.1683 hadRecentInput=false
  shift t=3001 (前のずれから+2000ms) value=0.0464 hadRecentInput=false
  shift t=3502 (前のずれから+500ms) value=0.0464 hadRecentInput=false
  shift t=4002 (前のずれから+500ms) value=0.0464 hadRecentInput=false
  window#1 1002〜1002ms(0ms) shifts=1 sum=0.1683
  window#2 3001〜4002ms(1000ms) shifts=3 sum=0.1393
  total=0.3076 max(window)=0.1683
  onCLS report at=46 value=0.0000 entries=[]
  onCLS report at=1002 value=0.1683 entries=[1002]

When I increased the small shifts to five, the second window's total reached 0.1857 at the fourth shift (4.5 seconds), passing the first window's 0.1683. At that point, onCLS reported for the first time, and its entries held only the shifts from the second window. The large shift at 1 second was no longer in entries.

console
== big-then-5small (?at=1000:150,3000:40,3500:40,4000:40,4500:40,5000:40)
  …
  window#1 1002〜1002ms(0ms) shifts=1 sum=0.1683
  window#2 3002〜5001ms(2000ms) shifts=5 sum=0.2321
  total=0.4005 max(window)=0.2321
  onCLS report at=39 value=0.0000 entries=[]
  onCLS report at=1002 value=0.1683 entries=[1002]
  onCLS report at=4502 value=0.1857 entries=[3002,3501,4001,4502]
  onCLS report at=5002 value=0.2321 entries=[3002,3501,4001,4502,5001]

(I omitted the middle shift lines.)

When CLS is high and you want to find the shifts causing it, the fastest approach is to look at the entries passed by onCLS. They contain only the shifts in the window that became the CLS, not a list of every shift on the page.

A shift right after a click did not link windows

A shift within 0.5 seconds of a click or key press gets hadRecentInput=true and is not counted toward CLS. Such a shift also did not link windows. On a page with shifts at 1 second, 1.75 seconds, and 2.6 seconds, I clicked the header just before the 1.75-second shift (at 1.67 seconds). The 1.75-second shift was excluded. The two remaining shifts were 1.6 seconds apart, so they formed two separate windows.

console
== input-between (?at=1000:40,1750:40,2600:40)
  inputs: pointerdown@1667 pointerup@1667 click@1667
  shift t=1013 value=0.0457 hadRecentInput=false
  shift t=1751 (前のずれから+738ms) value=0.0464 hadRecentInput=true lastInputTime=1667
  shift t=2601 (前のずれから+850ms) value=0.0464 hadRecentInput=false
  window#1 1013〜1013ms(0ms) shifts=1 sum=0.0457
  window#2 2601〜2601ms(0ms) shifts=1 sum=0.0464
  total=0.1386 max(window)=0.0464
  onCLS report at=47 value=0.0000 entries=[]
  onCLS report at=1014 value=0.0457 entries=[1013]
  onCLS report at=2602 value=0.0464 entries=[2601]

These rules can be confirmed in the web-vitals source, in LayoutShiftManager.ts. Shifts with hadRecentInput set are skipped before window calculations. For the remaining shifts, if the gap from the previous shift is less than 1000 ms and the gap from the first shift in the window is less than 5000 ms, the shift is added to the current window. Otherwise, a new window starts. The following code is an excerpt of that section.

ts
// Only count layout shifts without recent user input.
if (entry.hadRecentInput) return;
…
if (
  this._sessionValue &&
  firstSessionEntry &&
  lastSessionEntry &&
  entry.startTime - lastSessionEntry.startTime < 1000 &&
  entry.startTime - firstSessionEntry.startTime < 5000
) {
  this._sessionValue += entry.value;
  this._sessionEntries.push(entry);
} else {
  this._sessionValue = entry.value;
  this._sessionEntries = [entry];
}

In onCLS.ts, a report is sent, with its CLS and entries, only when the current window's total exceeds the previous CLS. This comparison explains why no report came during the second window in my test, and why the large shift disappeared from entries.

​Posted by 86