Performance And Web Vitals

dembrandt/dembrandt-skills/skills/performance-and-web-vitals

作者 dembrandt20de5f225ea7cffe2a721ac18c1077a92769a013无许可证68 个星标收录于 2026年10月9日更新于 2026年10月9日仓库今天更新

Lighthouse audits and Core Web Vitals — LCP, CLS, INP. Use when optimising page load, layout shift, input delay, images, fonts or render-blocking resources.

AI 生成的概览

指导使用 Lighthouse 审计并修复网页的 LCP、CLS、INP 等核心网页指标。

功能
提供通过命令行、DevTools 或 Lighthouse CI 运行 Lighthouse 审计的说明与代码片段,并给出目标分数表。它解释核心网页指标(LCP、CLS、INP)的含义、目标值、常见原因与修复方法,并涵盖图片、字体和 JavaScript 加载。最后提供性能、无障碍和 SEO 的检查清单。
适用场景
适用于优化页面加载速度、布局偏移、输入延迟、图片、字体或阻塞渲染的资源。也适合在部署流程中配置自动化 Lighthouse 审计。
运行要求
仅为说明文档,不附带脚本。执行所述审计需要 Node.js/npx 运行 Lighthouse 命令行,可选安装 @lhci/cli 用于 Lighthouse CI,并需要 Chrome/Chromium 浏览器进行无头审计以及访问被测页面的网络连接。

Performance and Web Vitals

Run a Lighthouse Audit

bash
# CLI audit — outputs JSON and HTML reportnpx lighthouse https://example.com --output html --output-path ./lighthouse-report.html
# Headless, useful in CInpx lighthouse https://example.com --chrome-flags="--headless" --output json --output-path ./report.json
# Audit specific categories onlynpx lighthouse https://example.com --only-categories=performance,accessibility,seo

Or open Chrome DevTools → Lighthouse tab → Analyse page load.

Target scores:

CategoryTarget
Performance≥ 90
Accessibility100
Best Practices≥ 95
SEO≥ 95

Core Web Vitals

LCP — Largest Contentful Paint

How fast does the main content appear?

Target: ≤ 2.5s

LCP measures when the largest visible element (hero image, heading, video poster) renders. It is the user's perception of "did the page load?"

Common causes and fixes:

CauseFix
Unoptimised hero imageUse WebP/AVIF, correct size, fetchpriority="high"
Image not preloaded<link rel="preload" as="image" href="hero.webp">
Render-blocking CSS/JSDefer non-critical JS, inline critical CSS
Slow server responseCDN, caching headers, edge delivery
Web font blocking renderfont-display: swap or optional
html
<!-- Preload LCP image --><link rel="preload" as="image" href="hero.webp" fetchpriority="high">
<!-- LCP image: no lazy loading --><img src="hero.webp" alt="..." fetchpriority="high" width="1200" height="600">

Never use loading="lazy" on the LCP image — it delays the most important render.


CLS — Cumulative Layout Shift

Does content jump around while loading?

Target: ≤ 0.1

CLS measures unexpected layout shifts — content moving after it has rendered. Caused by images without dimensions, late-loading ads, fonts swapping, or dynamic content injected above existing content.

Common causes and fixes:

CauseFix
Images without width/heightAlways set width and height on <img>
Web font swapUse font-display: optional or preload fonts
Dynamic content above foldReserve space with min-height on containers
Late-loading ads or embedsReserve fixed dimensions for ad slots
Animations that shift layoutAnimate transform only, never top/left/width/height
html
<!-- Always include dimensions --><img src="product.jpg" width="400" height="300" alt="...">
css
/* Reserve space for dynamic content */.ad-slot { min-height: 250px; }
/* Animate transform, not layout properties */.slide-in { transform: translateY(0); transition: transform 300ms; }

INP — Interaction to Next Paint

How quickly does the page respond to user input?

Target: ≤ 200ms

INP measures the delay between a user interaction (click, tap, keyboard) and the next visual update. High INP makes the UI feel sluggish or frozen.

Common causes and fixes:

CauseFix
Heavy JS on main threadBreak into smaller tasks, use requestIdleCallback
Large event handlersDebounce/throttle scroll and resize handlers
Synchronous DOM updatesBatch DOM writes with requestAnimationFrame
Third-party scripts blockingLoad third-party scripts with async or defer
React re-rendersMemoize with useMemo, useCallback, React.memo

Images

Images are the single biggest performance lever on most pages.

html
<!-- Modern formats with fallback --><picture>  <source srcset="image.avif" type="image/avif">  <source srcset="image.webp" type="image/webp">  <img src="image.jpg" width="800" height="600" alt="..." loading="lazy"></picture>
<!-- Responsive images --><img  srcset="image-400.webp 400w, image-800.webp 800w, image-1200.webp 1200w"  sizes="(max-width: 600px) 100vw, 50vw"  src="image-800.webp"  alt="..."  width="800"  height="600"  loading="lazy">

Rules:

  • Always set width and height — prevents CLS
  • Use loading="lazy" below the fold, never on LCP image
  • Serve WebP or AVIF — typically 30–50% smaller than JPEG
  • Size images to their display size — do not serve 2000px image for a 400px slot
  • Use a CDN with automatic format conversion where possible

Fonts

Web fonts block rendering if not handled correctly.

html
<!-- Preconnect to font origin --><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Preload critical font file --><link rel="preload" href="/fonts/brand.woff2" as="font" type="font/woff2" crossorigin>
css
@font-face {  font-family: 'Brand';  src: url('/fonts/brand.woff2') format('woff2');  font-display: swap;     /* show fallback immediately, swap when loaded */  /* font-display: optional; — never swap, use fallback if not cached */}
  • font-display: swap — good for headings, acceptable CLS
  • font-display: optional — zero CLS, font only used if cached (best for body text)
  • Subset fonts to the characters actually used — reduces file size by 60–80%

JavaScript

html
<!-- Defer non-critical scripts --><script src="analytics.js" defer></script><script src="chat-widget.js" async></script>
<!-- Module scripts are deferred by default --><script type="module" src="app.js"></script>
  • defer: executes after HTML parsed, in order — use for most scripts
  • async: executes as soon as downloaded, out of order — use for independent scripts (analytics)
  • Never block the main thread with synchronous <script> in <head>

Lighthouse CI (automated audits)

Run Lighthouse in CI to catch regressions before deployment.

bash
# Installnpm install -g @lhci/cli
# Runlhci autorun --upload.target=temporary-public-storage
yaml
# .lighthouserc.json{  "ci": {    "assert": {      "assertions": {        "categories:performance": ["warn", { "minScore": 0.9 }],        "categories:accessibility": ["error", { "minScore": 1.0 }],        "categories:seo": ["warn", { "minScore": 0.95 }]      }    }  }}

Review Checklist

  • Lighthouse performance score ≥ 90
  • Lighthouse accessibility score = 100
  • LCP ≤ 2.5s — LCP image preloaded, no loading="lazy" on it
  • CLS ≤ 0.1 — all images have width and height, no layout-shifting animations
  • INP ≤ 200ms — no heavy synchronous JS on main thread
  • Images served as WebP or AVIF with correct dimensions
  • loading="lazy" on all below-fold images
  • Web fonts use font-display: swap or optional
  • Non-critical JS loaded with defer or async
  • Lighthouse CI configured to catch regressions in deployment pipeline

来源与署名

来源:dembrandt/dembrandt-skills位于skills/performance-and-web-vitals提交20de5f2

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架