日本語

Core Web Vitals LCP Is Also Measured Inside Shadow DOM

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

On pages with a hero image or body text inside Web Components (custom elements), web-vitals may report an LCP value without telling you which element it came from. I checked the reason and how to identify these cases in Chromium, which I used as the test environment.

Images and paragraphs inside Shadow DOM were counted toward Core Web Vitals LCP (Largest Contentful Paint). I compared an image that arrived 1000ms late when placed in the normal DOM and when placed inside an open Shadow DOM. The LCP values (median of 5 runs) were 1024ms and 1028ms, which were almost the same. What differed was the information identifying the LCP element. For an element inside Shadow DOM, the entry's element was null, id was empty, and web-vitals' attribution.target was not set either.

Note

The test environment was Ubuntu 24.04, Node.js 22, web-vitals 6.2.3, and Chromium 157 (headless) from Playwright 1.63.0. The pages were served from a local server I ran (localhost:8080), measured at a 412px × 823px viewport, and each page configuration was measured 5 times. I did not test Firefox, Safari, real mobile devices, Lighthouse, field data from real users (CrUX, PageSpeed Insights field data), or the Element Timing API.

Pages used for the test

The base page had a heading and an 800×600 image. The server delayed the image by 1000ms, so the heading appeared first and the image appeared later. I compared the following eight variations, focusing on where the image was placed.

  • An <img> in the normal DOM
  • An <img> inside the open Shadow DOM of a custom element
  • An <img> inside a closed Shadow DOM
  • An <img> inside a declarative Shadow DOM (<template shadowrootmode="open">)
  • An <img> inside the Shadow DOM of a custom element that is itself inside another Shadow DOM (nested)
  • A normal-DOM <img> inserted into a custom element's <slot>
  • A long paragraph inside Shadow DOM instead of an image
  • Both the heading and the image inside Shadow DOM

The page with an open Shadow DOM was written like this:

html
<h1>今週のおすすめ</h1>
<p id="lead">商品の説明が入る段落です。</p>
<hero-banner id="banner"></hero-banner>
<script>
customElements.define('hero-banner', class extends HTMLElement {
  constructor() {
    super();
    const root = this.attachShadow({ mode: 'open' });
    root.innerHTML = '<style>img{display:block;max-width:100%;height:auto}</style><img id="hero" src="/hero.png" alt="おすすめの商品" width="800" height="600">';
  }
});
</script>

On the page, I used PerformanceObserver to collect largest-contentful-paint entries and recorded startTime, size, element, id, and url. I also loaded the web-vitals attribution build and sent the onLCP report (value, attribution.target, attribution.url) to the server with navigator.sendBeacon. I opened the page, waited 3000ms, and read the entries. Then I navigated to another page (about:blank) and received the web-vitals report.

element was recorded in the form "tagname#id", and when it was null I printed element=null. When web-vitals' target was missing (undefined), I changed it to null before sending. The command output below shows only the first of the 5 runs.

Images inside Shadow DOM also became LCP at nearly the same time

First, the case with the image in the normal DOM. There were two entries, the heading (h1) and the image, and the LCP was the image's value. element, id, and url were all present, and web-vitals' target was #hero.

console
$ node measure.cjs light 5
light run1:
  LCPのエントリ: h1(28ms,size=8244,id="",url="") img#hero(1024ms,size=117612,id="hero",url="/hero.png")
  web-vitals の報告: LCP=1024 target="#hero" url="/hero.png" element=img#hero

When the same image was placed inside an open Shadow DOM, there were again two entries. The size of the second image's entry matched the normal-DOM case. The timing was also nearly the same: the median of 5 runs was 1024ms in the normal DOM and 1028ms in Shadow DOM. The image is larger than the heading in the normal DOM, so it became the LCP. However, for that entry, element was null and id was an empty string. Although I set id="hero" on the <img>, it did not appear in the entry. web-vitals' target was also not set, and only the image's url remained.

console
$ node measure.cjs open-shadow 5
open-shadow run1:
  LCPのエントリ: h1(32ms,size=8244,id="",url="") element=null(1028ms,size=117612,id="",url="/hero.png")
  web-vitals の報告: LCP=1028 target=null url="/hero.png" element=null

The closed Shadow DOM, declarative Shadow DOM, and nested Shadow DOM gave the same results in all 5 runs. The image counted toward LCP, element was null, id was empty, and target was not set.

On the other hand, the image inserted into a <slot> gave the same results as the normal DOM case. The <img> is displayed inside the Shadow DOM, but the element itself is a child of the normal DOM. So element was img#hero and target was #hero.

Table of LCP and web-vitals target by page structure. The normal-DOM image is 1024ms with #hero. The images in open, closed, declarative, and nested Shadow DOM are 1024ms to 1032ms with no target. The image inserted into a slot is 1024ms with #hero. The long paragraph inside Shadow DOM is 36ms with no target. The page with all content in Shadow DOM is 1028ms with no target
LCP (median of 5 runs) and web-vitals target by page structure. Elements inside Shadow DOM have their timing measured, but no target is set

The same happened with paragraphs and with pages where everything was inside Shadow DOM

Even when a long paragraph inside Shadow DOM was used instead of an image, the paragraph counted toward LCP. There was only one entry for the paragraph, and no entry for the heading. The LCP was 32ms to 44ms (median 36ms). Text has no url, so the entry leaves no clue for identifying the paragraph.

console
$ node measure.cjs shadow-text 5
shadow-text run1:
  LCPのエントリ: element=null(44ms,size=202669,id="",url="")
  web-vitals の報告: LCP=44 target=null url=null element=null

On the page where both the heading and the image were inside Shadow DOM, the entries for both the heading and the image had element set to null. The heading's entry never pointed to the host element (<hero-banner>) or to body.

console
$ node measure.cjs shadow-only 5
shadow-only run1:
  LCPのエントリ: element=null(32ms,size=8244,id="",url="") element=null(1032ms,size=117612,id="",url="/hero.png")
  web-vitals の報告: LCP=1032 target=null url="/hero.png" element=null

Why element is null, and the current state of the spec

In the W3C Largest Contentful Paint spec source on GitHub, the element getter returns null unless the element is "exposed for paint timing" as defined in the Paint Timing spec. This check returns false when the root of the element's tree is not a Document. An element inside Shadow DOM has a Shadow Root as its root, so it fails here. A note in the spec also says that elements inside Shadow DOM are not returned by element.

However, the procedure in the same spec for selecting LCP candidates also has a sentence that skips elements using the same check. Read literally, an element inside Shadow DOM would not even become a candidate and so would not count toward LCP. The Chromium 157 I tested included such elements as candidates but set only element to null.

This discrepancy is discussed in the spec issue "What is the behavior when paints emit from inside the Shadow DOM?", and at the time of writing this article no conclusion has been reached. According to comments on the issue, Chromium and Firefox report LCP inside Shadow DOM with timing and size, and set the element to null to preserve encapsulation. WebKit made a change to report LCP inside Shadow DOM, then reverted it, and a comment says it is unclear from the spec whether this behavior is correct. A proposal to point to the host element instead of the element has also been raised. Browsers and future spec versions may change the results I confirmed here.

Identifying LCP inside Shadow DOM from web-vitals data

The web-vitals 6.2.3 attribution build (src/attribution/onLCP.ts) creates a selector from the entry's element if it exists, and uses id otherwise. If neither is available, it sets nothing for target.

ts
const node = entry.element;
if (node) {
  const customTarget = opts.generateTarget?.(node) ?? getSelector(node);
  lcpTargetMap.set(entry, customTarget);
} else if (entry.id) {
  // Use the LargestContentfulPaint.id property when the element has been
  // removed from the DOM (and so node is null), but still has an ID.
  lcpTargetMap.set(entry, `#${entry.id}`);
}

The generateTarget option, which changes how target is built, is only called when element is present. So no option setting can point to an element inside Shadow DOM.

If your collected data shows an LCP value with an empty target, the LCP element may be inside Shadow DOM. As the code comment says, element is also null when the element has been removed from the DOM. If that element has no id, target is empty in the same way. For images, attribution.url contains the image URL, so you can trace which component the image came from using the URL. For text, no clue remains, so you need to narrow down which pages show the problem using the page URL sent with the report, and then search the Shadow DOM of those pages yourself for a large paragraph.

Point

If you want to find the LCP element on pages that use Web Components, collect attribution.url together with web-vitals' target. When an image inside Shadow DOM becomes the LCP, the image URL is the only clue left.

​Posted by 86