Skip to content

Commit e4aaba4

Browse files
matthiasrohmerwolfib
authored andcommitted
feat: add skill to debug and optimize LCP (ChromeDevTools#993)
This is a draft for new skill to debug and optimize LCP scores, going hand in hand with ChromeDevTools#831. Base for this skill is key authoritative learning material from web.dev, about debugging and optimizing LCP, mainly: - http://web.dev/articles/lcp - http://web.dev/articles/optimize-lcp - https://web.dev/articles/top-cwv I gave the content of those articles together with the [tool reference](https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/docs/tool-reference.md), the README and the chrome-devtools core skill to both Claude Opus 4.6 and Gemini 3 Pro and their respective skill creator skills. The final skill in this PR is a merge of multiple runs with both models. The snippets to run with `evaluate_script` is something only Opus came up with, but I think it's quite nice, though our own `performance_*` tools might give the same insight but likely with more overhead (meaning tokens).
1 parent 8b09583 commit e4aaba4

File tree

5 files changed

+288
-0
lines changed

5 files changed

+288
-0
lines changed

skills/debug-optimize-lcp/SKILL.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
name: debug-optimize-lcp
3+
description: Guides debugging and optimizing Largest Contentful Paint (LCP) using Chrome DevTools MCP tools. Use this skill whenever the user asks about LCP performance, slow page loads, Core Web Vitals optimization, or wants to understand why their page's main content takes too long to appear. Also use when the user mentions "largest contentful paint", "page load speed", "CWV", or wants to improve how fast their hero image or main content renders.
4+
---
5+
6+
## What is LCP and why it matters
7+
8+
Largest Contentful Paint (LCP) measures how quickly a page's main content becomes visible. It's the time from navigation start until the largest image or text block renders in the viewport.
9+
10+
- **Good**: 2.5 seconds or less
11+
- **Needs improvement**: 2.5–4.0 seconds
12+
- **Poor**: greater than 4.0 seconds
13+
14+
LCP is a Core Web Vital that directly affects user experience and search ranking. On 73% of mobile pages, the LCP element is an image.
15+
16+
## LCP Subparts Breakdown
17+
18+
Every page's LCP breaks down into four sequential subparts with no gaps or overlaps. Understanding which subpart is the bottleneck is the key to effective optimization.
19+
20+
| Subpart | Ideal % of LCP | What it measures |
21+
| ----------------------------- | -------------- | ---------------------------------------------- |
22+
| **Time to First Byte (TTFB)** | ~40% | Navigation start → first byte of HTML received |
23+
| **Resource load delay** | <10% | TTFB → browser starts loading the LCP resource |
24+
| **Resource load duration** | ~40% | Time to download the LCP resource |
25+
| **Element render delay** | <10% | LCP resource downloaded → LCP element rendered |
26+
27+
The "delay" subparts should be as close to zero as possible. If either delay subpart is large relative to the total LCP, that's the first place to optimize.
28+
29+
**Common Pitfall**: Optimizing one subpart (like compressing an image to reduce load duration) without checking others. If render delay is the real bottleneck, a smaller image won't help — the saved time just shifts to render delay.
30+
31+
## Debugging Workflow
32+
33+
Follow these steps in order. Each step builds on the previous one.
34+
35+
### Step 1: Record a Performance Trace
36+
37+
Navigate to the page, then record a trace with reload to capture the full page load including LCP:
38+
39+
1. `navigate_page` to the target URL.
40+
2. `performance_start_trace` with `reload: true` and `autoStop: true`.
41+
42+
The trace results will include LCP timing and available insight sets. Note the insight set IDs from the output — you'll need them in the next step.
43+
44+
### Step 2: Analyze LCP Insights
45+
46+
Use `performance_analyze_insight` to drill into LCP-specific insights. Look for these insight names in the trace results:
47+
48+
- **LCPBreakdown** — Shows the four LCP subparts with timing for each.
49+
- **DocumentLatency** — Server response time issues affecting TTFB.
50+
- **RenderBlocking** — Resources blocking the LCP element from rendering.
51+
- **LCPDiscovery** — Whether the LCP resource was discoverable early.
52+
53+
Call `performance_analyze_insight` with the insight name and the insight set ID from the trace results.
54+
55+
### Step 3: Identify the LCP Element
56+
57+
Use `evaluate_script` with the **"Identify LCP Element" snippet** found in [references/lcp-snippets.md](references/lcp-snippets.md) to reveal the LCP element's tag, resource URL, and raw timing data.
58+
59+
The `url` field tells you what resource to look for in the network waterfall. If `url` is empty, the LCP element is text-based (no resource to load).
60+
61+
### Step 4: Check the Network Waterfall
62+
63+
Use `list_network_requests` to see when the LCP resource loaded relative to other resources:
64+
65+
- Call `list_network_requests` filtered by `resourceTypes: ["Image", "Font"]` (adjust based on Step 3).
66+
- Then use `get_network_request` with the LCP resource's request ID for full details.
67+
68+
**Key Checks:**
69+
70+
- **Start Time**: Compare against the HTML document and the first resource. If the LCP resource starts much later than the first resource, there's resource load delay to eliminate.
71+
- **Duration**: A large resource load duration suggests the file is too big or the server is slow.
72+
73+
### Step 5: Inspect HTML for Common Issues
74+
75+
Use `evaluate_script` with the **"Audit Common Issues" snippet** found in [references/lcp-snippets.md](references/lcp-snippets.md) to check for lazy-loaded images in the viewport, missing fetchpriority, and render-blocking scripts.
76+
77+
## Optimization Strategies
78+
79+
After identifying the bottleneck subpart, apply these prioritized fixes.
80+
81+
### 1. Eliminate Resource Load Delay (target: <10%)
82+
83+
The most common bottleneck. The LCP resource should start loading immediately.
84+
85+
- **Root Cause**: LCP image loaded via JS/CSS, `data-src` usage, or `loading="lazy"`.
86+
- **Fix**: Use standard `<img>` with `src`. **Never** lazy-load the LCP image.
87+
- **Fix**: Add `<link rel="preload" fetchpriority="high">` if the image isn't discoverable in HTML.
88+
- **Fix**: Add `fetchpriority="high"` to the LCP `<img>` tag.
89+
90+
### 2. Eliminate Element Render Delay (target: <10%)
91+
92+
The element should render immediately after loading.
93+
94+
- **Root Cause**: Large stylesheets, synchronous scripts in `<head>`, or main thread blocking.
95+
- **Fix**: Inline critical CSS, defer non-critical CSS/JS.
96+
- **Fix**: Break up long tasks blocking the main thread.
97+
- **Fix**: Use Server-Side Rendering (SSR) so the element exists in initial HTML.
98+
99+
### 3. Reduce Resource Load Duration (target: ~40%)
100+
101+
Make the resource smaller or faster to deliver.
102+
103+
- **Fix**: Use modern formats (WebP, AVIF) and responsive images (`srcset`).
104+
- **Fix**: Serve from a CDN.
105+
- **Fix**: Set `Cache-Control` headers.
106+
- **Fix**: Use `font-display: swap` if LCP is text blocked by a web font.
107+
108+
### 4. Reduce TTFB (target: ~40%)
109+
110+
The HTML document itself takes too long to arrive.
111+
112+
- **Fix**: Minimize redirects and optimize server response time.
113+
- **Fix**: Cache HTML at the edge (CDN).
114+
- **Fix**: Ensure pages are eligible for back/forward cache (bfcache).
115+
116+
## Verifying Fixes & Emulation
117+
118+
- **Verification**: Re-run the trace (`performance_start_trace` with `reload: true`) and compare the new subpart breakdown. The bottleneck should shrink.
119+
- **Emulation**: Lab measurements differ from real-world experience. Use `emulate` to test under constraints:
120+
- `emulate` with `networkConditions: "Fast 3G"` and `cpuThrottlingRate: 4`.
121+
- This surfaces issues visible only on slower connections/devices.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Elements and Size for LCP
2+
3+
## What Elements are Considered?
4+
5+
The types of elements considered for Largest Contentful Paint (LCP) are:
6+
7+
- **`<img>` elements**: The first frame presentation time is used for animated content like GIFs.
8+
- **`<image>` elements** inside an `<svg>` element.
9+
- **`<video>` elements**: The poster image load time or first frame presentation time, whichever is earlier.
10+
- **Background images**: Elements with a background image loaded using `url()`.
11+
- **Block-level elements**: Containing text nodes or other inline-level text element children.
12+
13+
## Heuristics to Exclude Non-Contentful Elements
14+
15+
Chromium-based browsers use heuristics to exclude:
16+
17+
- Elements with **opacity of 0**.
18+
- Elements that **cover the full viewport** (likely background).
19+
- **Placeholder images** or low-entropy images.
20+
21+
## How is an Element's Size Determined?
22+
23+
- **Visible Area**: Typically the size visible within the viewport. Extending outside, clipped, or overflow portions don't count.
24+
- **Image Elements**: Either the visible size or the intrinsic size, whichever is smaller.
25+
- **Text Elements**: The smallest rectangle containing all text nodes.
26+
- **Exclusions**: Margin, padding, and borders are not considered toward the size.
27+
- **Containment**: Every text node belongs to its closest block-level ancestor element.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Largest Contentful Paint (LCP) Breakdown
2+
3+
LCP measures the time from when the user initiates loading the page until the largest image or text block is rendered within the viewport. To provide a good user experience, sites should strive to have an LCP of 2.5 seconds or less for at least 75% of page visits.
4+
5+
## The Four Subparts of LCP
6+
7+
Every page's LCP consists of these four subcategories. There's no gap or overlap between them, and they add up to the full LCP time.
8+
9+
| LCP subpart | % of LCP (Optimal) | Description |
10+
| ----------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
11+
| **Time to First Byte (TTFB)** | ~40% | The time from when the user initiates loading the page until the browser receives the first byte of the HTML document response. |
12+
| **Resource load delay** | <10% | The time between TTFB and when the browser starts loading the LCP resource. If the LCP element doesn't require a resource load (e.g., system font text), this time is 0. |
13+
| **Resource load duration** | ~40% | The duration of time it takes to load the LCP resource itself. If the LCP element doesn't require a resource load, this time is 0. |
14+
| **Element render delay** | <10% | The time between when the LCP resource finishes loading and the LCP element rendering fully. |
15+
16+
## Why the Breakdown Matters
17+
18+
Optimizing for LCP requires identifying which of these subparts is the bottleneck:
19+
20+
- **Large delta between TTFB and FCP**: Indicates the browser needs to download a lot of render-blocking assets or complete a lot of work (e.g., client-side rendering).
21+
- **Large delta between FCP and LCP**: Indicates the LCP resource is not immediately available for the browser to prioritize or the browser is completing other work before it can display the LCP content.
22+
- **Large resource load delay**: Indicates the resource is not discoverable early or is deprioritized.
23+
- **Large element render delay**: Indicates rendering is blocked by stylesheets, scripts, or long tasks.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# LCP Debugging Snippets
2+
3+
Use these JavaScript snippets with the `evaluate_script` tool to extract deep insights from the page.
4+
5+
## 1. Identify LCP Element
6+
7+
Use this snippet to identify the LCP element and get raw timing data from the Performance API.
8+
9+
```javascript
10+
async () => {
11+
return await new Promise(resolve => {
12+
new PerformanceObserver(list => {
13+
const entries = list.getEntries();
14+
const last = entries[entries.length - 1];
15+
resolve({
16+
element: last.element?.tagName,
17+
id: last.element?.id,
18+
className: last.element?.className,
19+
url: last.url,
20+
startTime: last.startTime,
21+
renderTime: last.renderTime,
22+
loadTime: last.loadTime,
23+
size: last.size,
24+
});
25+
}).observe({type: 'largest-contentful-paint', buffered: true});
26+
});
27+
};
28+
```
29+
30+
## 2. Audit Common Issues
31+
32+
Use this snippet to check for common DOM-based LCP issues (lazy loading, priority).
33+
34+
```javascript
35+
() => {
36+
const issues = [];
37+
38+
// Check for lazy-loaded images in viewport
39+
document.querySelectorAll('img[loading="lazy"]').forEach(img => {
40+
const rect = img.getBoundingClientRect();
41+
if (rect.top < window.innerHeight) {
42+
issues.push({
43+
issue: 'lazy-loaded image in viewport',
44+
element: img.outerHTML.substring(0, 200),
45+
fix: 'Remove loading="lazy" from this image — it is in the initial viewport and may be the LCP element',
46+
});
47+
}
48+
});
49+
50+
// Check for LCP-candidate images missing fetchpriority
51+
document.querySelectorAll('img:not([fetchpriority])').forEach(img => {
52+
const rect = img.getBoundingClientRect();
53+
if (rect.top < window.innerHeight && rect.width * rect.height > 50000) {
54+
issues.push({
55+
issue: 'large viewport image without fetchpriority',
56+
element: img.outerHTML.substring(0, 200),
57+
fix: 'Add fetchpriority="high" to this image — it is large and visible in the initial viewport',
58+
});
59+
}
60+
});
61+
62+
// Check for render-blocking scripts in head
63+
document
64+
.querySelectorAll(
65+
'head script:not([async]):not([defer]):not([type="module"])',
66+
)
67+
.forEach(script => {
68+
if (script.src) {
69+
issues.push({
70+
issue: 'render-blocking script in head',
71+
element: script.outerHTML.substring(0, 200),
72+
fix: 'Add async or defer attribute, or move to end of body',
73+
});
74+
}
75+
});
76+
77+
return {issueCount: issues.length, issues};
78+
};
79+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# LCP Optimization Strategies
2+
3+
## 1. Eliminate Resource Load Delay
4+
5+
**Goal**: Ensure the LCP resource starts loading as early as possible.
6+
7+
- **Early Discovery**: Ensure the LCP resource is discoverable in the initial HTML document response (not dynamically added by JS or hidden in `data-src`).
8+
- **Preload**: Use `<link rel="preload">` with `fetchpriority="high"` for critical images or fonts.
9+
- **Avoid Lazy Loading**: Never set `loading="lazy"` on the LCP image.
10+
- **Fetch Priority**: Use `fetchpriority="high"` on the `<img>` tag.
11+
- **Same Origin**: Host critical resources on the same origin or use `<link rel="preconnect">`.
12+
13+
## 2. Eliminate Element Render Delay
14+
15+
**Goal**: Ensure the LCP element can render immediately after its resource has finished loading.
16+
17+
- **Minimize Render-Blocking CSS**: Inline critical CSS and defer non-critical CSS. Ensure the stylesheet is smaller than the LCP resource.
18+
- **Minimize Render-Blocking JS**: Avoid synchronous scripts in the `<head>`. Inline very small scripts.
19+
- **Server-Side Rendering (SSR)**: Deliver the full HTML markup from the server so image resources are discoverable immediately.
20+
- **Break Up Long Tasks**: Prevent large JavaScript tasks from blocking the main thread during rendering.
21+
22+
## 3. Reduce Resource Load Duration
23+
24+
**Goal**: Reduce the time spent transferring the bytes of the resource.
25+
26+
- **Optimize Resource Size**: Serve optimal image sizes, use modern formats (AVIF, WebP), and compress images/fonts.
27+
- **Geographic Proximity (CDN)**: Use a Content Delivery Network to get servers closer to users.
28+
- **Reduce Contention**: Use `fetchpriority="high"` to prevent lower-priority resources from competing for bandwidth.
29+
- **Caching**: Use efficient `Cache-Control` policies.
30+
31+
## 4. Reduce Time to First Byte (TTFB)
32+
33+
**Goal**: Deliver the initial HTML as quickly as possible.
34+
35+
- **Minimize Redirects**: Avoid multiple redirects from advertisements or shortened links.
36+
- **CDN Caching**: Cache static HTML documents at the edge.
37+
- **Edge Computing**: Move dynamic logic to the edge to avoid trips to the origin server.
38+
- **Back/Forward Cache**: Ensure pages are eligible for bfcache.

0 commit comments

Comments
 (0)