When to Use
- Use when: Lighthouse score < 70 on mobile or desktop
- Use when: LCP > 2.5s, INP > 200ms, or CLS > 0.1
- Use when: bundle size review before shipping new dependencies
- Use when: images are unoptimized or causing layout shift
- Do NOT use for: database query optimization — that's a backend concern
- Do NOT use for: React rendering performance (memo, useMemo) — that's a separate profiling task
Core Web Vitals — Targets and Causes
LCP — Largest Contentful Paint (target: < 2.5s)
What causes LCP to fail:
| Cause |
Fix |
| Render-blocking CSS/JS |
<link rel="preload"> critical CSS; defer non-critical JS |
| Unoptimized hero image |
WebP/AVIF + srcset + fetchpriority="high" on LCP image |
| Slow server response (TTFB > 600ms) |
CDN, edge caching, server-side rendering |
| Lazy-loaded LCP element |
Remove loading="lazy" from the above-fold image |
| Web font blocking render |
font-display: swap + preload critical font file |
INP — Interaction to Next Paint (target: < 200ms)
Replaced FID as Core Web Vital in March 2024 — measure this, not FID.
What causes INP to fail:
| Cause |
Fix |
| Long event handlers (> 50ms) |
Break into smaller tasks with scheduler.yield() / setTimeout(0) |
| Heavy JS on main thread |
Move to Web Worker or defer until after interaction |
| Forced synchronous layout |
Batch DOM reads/writes; avoid read-then-write patterns |
| Third-party scripts blocking |
Load third-party scripts with async/defer, or lazy-load |
CLS — Cumulative Layout Shift (target: < 0.1)
What causes CLS to fail:
| Cause |
Fix |
| Images without dimensions |
Always set width + height on <img>, or use aspect-ratio in CSS |
| Web fonts causing FOUT |
font-display: optional or reserve space with size-adjust |
| Dynamic content inserted above existing |
Reserve space with min-height before content loads |
| Ads / embeds without dimensions |
Fixed-size containers for ad slots |
Bundle Optimization
Code splitting
// Route-level splitting (React)
const Dashboard = lazy(() => import('./Dashboard'));
const Settings = lazy(() => import('./Settings'));
// Wrap in Suspense with skeleton fallback
<Suspense fallback={<DashboardSkeleton />}>
<Dashboard />
</Suspense>
Webpack / Vite vendor chunk split
// vite.config.js
build: {
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
charts: ['recharts'],
}
}
}
}
Tree shaking — what breaks it
import * as X from 'lib' — prevents tree shaking; use named imports
- CommonJS
require() — not tree-shakeable; prefer ESM
- Side-effectful imports — mark packages as
"sideEffects": false in package.json
Bundle analysis
# Vite
npx vite-bundle-analyzer
# Webpack
npx webpack-bundle-analyzer stats.json
Image Optimization
<!-- Modern format with fallback -->
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg"
width="1200" height="630"
alt="..."
fetchpriority="high" <!-- LCP image only -->
decoding="async">
</picture>
<!-- Below-fold images: lazy load -->
<img src="card.webp" loading="lazy" decoding="async" width="400" height="300" alt="...">
Rules:
- LCP image: never
loading="lazy", always fetchpriority="high"
- Every
<img>: always set width + height to prevent CLS
- Prefer AVIF > WebP > JPEG for photos; SVG for icons/illustrations
- Responsive
srcset: provide 1×, 1.5×, 2× versions
Caching Strategies
HTTP cache headers
# Immutable assets (hashed filenames) — cache forever
Cache-Control: public, max-age=31536000, immutable
# HTML — always revalidate
Cache-Control: no-cache
# API responses — short cache
Cache-Control: public, max-age=60, stale-while-revalidate=300
Service Worker (cache-first for assets, network-first for API)
// Cache-first for static assets
self.addEventListener('fetch', (e) => {
if (e.request.destination === 'image' || e.request.url.includes('/assets/')) {
e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
}
});
Resource Hints
<!-- DNS + TLS early for external origins -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.example.com" crossorigin>
<!-- Preload critical resources (LCP image, key font) -->
<link rel="preload" href="/fonts/inter-400.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/hero.avif" as="image">
<!-- Prefetch next-page resources (low priority, idle time) -->
<link rel="prefetch" href="/dashboard-chunk.js">
Optimization Priority
| Effort |
Actions |
| Quick (1–2 days) |
Enable gzip/Brotli, defer non-critical JS, add image width/height, font-display: swap |
| Medium (1–2 weeks) |
Route-level code splitting, service worker, WebP/AVIF conversion, preconnect hints |
| Long-term (1–3 months) |
CDN, edge rendering, architecture refactor, performance budget enforcement |
Measurement
# Lighthouse CLI
npx lighthouse https://yoursite.com --output=html --view
# Field data (web-vitals library in production)
import {onLCP, onINP, onCLS} from 'web-vitals';
onLCP(console.log);
onINP(console.log);
onCLS(console.log);
Always test on throttled mobile (Lighthouse preset: Moto G4, slow 4G).
Desktop scores mislead — real users are on slower devices.
Anti-Fake-Pass Rules
Before claiming performance is optimized, you MUST show:
Reference: gates/anti-fake-pass-gate.md
1---
2name: web-performance
3description: >
4 Audit and optimize web performance — Core Web Vitals (LCP, INP, CLS),
5 bundle splitting, lazy loading, image optimization, caching strategies,
6 and resource hints. Use when asked to "improve performance", "fix LCP/CLS/INP",
7 "optimize bundle", "reduce load time", "lazy load images", "add caching",
8 or before marking any public-facing page as production-ready.
9 Do NOT use for server-side database performance — this covers frontend and
10 network-layer only.
11origin: adapted:secondsky/claude-skills
12license: MIT © 2025 Claude Skills Maintainers
13version: 1.0.0
14compatibility: "Any web frontend. Framework-agnostic patterns + Webpack/Vite/Rollup examples."
15metadata:
16 source: https://skillmd.com/skills/yanacuti1121/web-performance
17---
18
19<!-- Adapted from secondsky/claude-skills (MIT) — web-performance-optimization and
20 web-performance-audit skills. Core Web Vitals targets, audit process, optimization
21 timeline. YAMTAM structure, per-metric diagnosis tables, and Anti-Fake-Pass are original. -->
22
23## When to Use
24
25- Use when: Lighthouse score < 70 on mobile or desktop
26- Use when: LCP > 2.5s, INP > 200ms, or CLS > 0.1
27- Use when: bundle size review before shipping new dependencies
28- Use when: images are unoptimized or causing layout shift
29- Do NOT use for: database query optimization — that's a backend concern
30- Do NOT use for: React rendering performance (memo, useMemo) — that's a separate profiling task
31
32---
33
34## Core Web Vitals — Targets and Causes
35
36### LCP — Largest Contentful Paint (target: < 2.5s)
37
38What causes LCP to fail:
39| Cause | Fix |
40|---|---|
41| Render-blocking CSS/JS | `<link rel="preload">` critical CSS; defer non-critical JS |
42| Unoptimized hero image | WebP/AVIF + `srcset` + `fetchpriority="high"` on LCP image |
43| Slow server response (TTFB > 600ms) | CDN, edge caching, server-side rendering |
44| Lazy-loaded LCP element | Remove `loading="lazy"` from the above-fold image |
45| Web font blocking render | `font-display: swap` + preload critical font file |
46
47### INP — Interaction to Next Paint (target: < 200ms)
48*Replaced FID as Core Web Vital in March 2024 — measure this, not FID.*
49
50What causes INP to fail:
51| Cause | Fix |
52|---|---|
53| Long event handlers (> 50ms) | Break into smaller tasks with `scheduler.yield()` / `setTimeout(0)` |
54| Heavy JS on main thread | Move to Web Worker or defer until after interaction |
55| Forced synchronous layout | Batch DOM reads/writes; avoid read-then-write patterns |
56| Third-party scripts blocking | Load third-party scripts with `async`/`defer`, or lazy-load |
57
58### CLS — Cumulative Layout Shift (target: < 0.1)
59
60What causes CLS to fail:
61| Cause | Fix |
62|---|---|
63| Images without dimensions | Always set `width` + `height` on `<img>`, or use `aspect-ratio` in CSS |
64| Web fonts causing FOUT | `font-display: optional` or reserve space with `size-adjust` |
65| Dynamic content inserted above existing | Reserve space with `min-height` before content loads |
66| Ads / embeds without dimensions | Fixed-size containers for ad slots |
67
68---
69
70## Bundle Optimization
71
72### Code splitting
73```js
74// Route-level splitting (React)
75const Dashboard = lazy(() => import('./Dashboard'));
76const Settings = lazy(() => import('./Settings'));
77
78// Wrap in Suspense with skeleton fallback
79<Suspense fallback={<DashboardSkeleton />}>
80 <Dashboard />
81</Suspense>
82```
83
84### Webpack / Vite vendor chunk split
85```js
86// vite.config.js
87build: {
88 rollupOptions: {
89 output: {
90 manualChunks: {
91 vendor: ['react', 'react-dom'],
92 charts: ['recharts'],
93 }
94 }
95 }
96}
97```
98
99### Tree shaking — what breaks it
100- `import * as X from 'lib'` — prevents tree shaking; use named imports
101- CommonJS `require()` — not tree-shakeable; prefer ESM
102- Side-effectful imports — mark packages as `"sideEffects": false` in package.json
103
104### Bundle analysis
105```bash
106# Vite
107npx vite-bundle-analyzer
108# Webpack
109npx webpack-bundle-analyzer stats.json
110```
111
112---
113
114## Image Optimization
115
116```html
117<!-- Modern format with fallback -->
118<picture>
119 <source srcset="hero.avif" type="image/avif">
120 <source srcset="hero.webp" type="image/webp">
121 <img src="hero.jpg"
122 width="1200" height="630"
123 alt="..."
124 fetchpriority="high" <!-- LCP image only -->
125 decoding="async">
126</picture>
127
128<!-- Below-fold images: lazy load -->
129<img src="card.webp" loading="lazy" decoding="async" width="400" height="300" alt="...">
130```
131
132Rules:
133- **LCP image**: never `loading="lazy"`, always `fetchpriority="high"`
134- **Every `<img>`**: always set `width` + `height` to prevent CLS
135- Prefer AVIF > WebP > JPEG for photos; SVG for icons/illustrations
136- Responsive `srcset`: provide 1×, 1.5×, 2× versions
137
138---
139
140## Caching Strategies
141
142### HTTP cache headers
143```
144# Immutable assets (hashed filenames) — cache forever
145Cache-Control: public, max-age=31536000, immutable
146
147# HTML — always revalidate
148Cache-Control: no-cache
149
150# API responses — short cache
151Cache-Control: public, max-age=60, stale-while-revalidate=300
152```
153
154### Service Worker (cache-first for assets, network-first for API)
155```js
156// Cache-first for static assets
157self.addEventListener('fetch', (e) => {
158 if (e.request.destination === 'image' || e.request.url.includes('/assets/')) {
159 e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
160 }
161});
162```
163
164---
165
166## Resource Hints
167
168```html
169<!-- DNS + TLS early for external origins -->
170<link rel="preconnect" href="https://fonts.googleapis.com">
171<link rel="preconnect" href="https://cdn.example.com" crossorigin>
172
173<!-- Preload critical resources (LCP image, key font) -->
174<link rel="preload" href="/fonts/inter-400.woff2" as="font" type="font/woff2" crossorigin>
175<link rel="preload" href="/hero.avif" as="image">
176
177<!-- Prefetch next-page resources (low priority, idle time) -->
178<link rel="prefetch" href="/dashboard-chunk.js">
179```
180
181---
182
183## Optimization Priority
184
185| Effort | Actions |
186|---|---|
187| Quick (1–2 days) | Enable gzip/Brotli, defer non-critical JS, add image `width`/`height`, `font-display: swap` |
188| Medium (1–2 weeks) | Route-level code splitting, service worker, WebP/AVIF conversion, preconnect hints |
189| Long-term (1–3 months) | CDN, edge rendering, architecture refactor, performance budget enforcement |
190
191---
192
193## Measurement
194
195```bash
196# Lighthouse CLI
197npx lighthouse https://yoursite.com --output=html --view
198
199# Field data (web-vitals library in production)
200import {onLCP, onINP, onCLS} from 'web-vitals';
201onLCP(console.log);
202onINP(console.log);
203onCLS(console.log);
204```
205
206**Always test on throttled mobile** (Lighthouse preset: Moto G4, slow 4G).
207Desktop scores mislead — real users are on slower devices.
208
209---
210
211## Anti-Fake-Pass Rules
212
213Before claiming performance is optimized, you MUST show:
214- [ ] LCP, INP, CLS — all three measured (Lighthouse score or field data)
215- [ ] Every `<img>` has `width` + `height` set (CLS prevention)
216- [ ] LCP image: no `loading="lazy"`, has `fetchpriority="high"`
217- [ ] Bundle: at least route-level code splitting present
218- [ ] Caching headers defined for static assets and HTML separately
219- [ ] Tested on throttled mobile, not just desktop
220
221Reference: `gates/anti-fake-pass-gate.md`