Perf Web Optimization

作者 tech-leads-club120b67676388无许可证收录于 2026年10月8日更新于 2026年10月8日

Optimize web performance: bundle size, images, caching, lazy loading, and overall page speed. Use when site is slow, reducing bundle size, fixing layout shifts, improving Time to Interactive, or optimizing for Lighthouse scores. Triggers on: web performance, bundle size, page speed, slow site, lazy loading. Do NOT use for Core Web Vitals-specific fixes (use core-web-vitals), running Lighthouse audits (use perf-lighthouse), or Astro-specific optimization (use perf-astro).

AI 生成的概览

指导系统化地优化网页性能,涵盖打包体积、图片、字体、缓存与懒加载。

功能
提供“测量—识别—排序—实施—验证”的流程来提升页面速度,并给出 LCP、INP、CLS 和 TTFB 的目标指标阈值。它包含图片、字体、第三方脚本和关键 CSS 的快速优化模式,以及打包分析命令、代码分割示例和缓存响应头建议。还提供检查清单,并指向 Core Web Vitals、打包优化和图片优化的参考文件。
适用场景
适用于网站变慢,或需要减小打包体积、修复布局偏移、改善可交互时间、优化 Lighthouse 得分时。不适用于专门修复 Core Web Vitals、运行 Lighthouse 审计或 Astro 专属优化。
运行要求
无需脚本,仅提供说明和参考文档。打包分析示例假定使用 Node.js 工具,如 webpack-bundle-analyzer、vite-bundle-visualizer 或 bundlephobia,但这些均为可选。

Web Performance Optimization

Systematic approach: Measure → Identify → Prioritize → Implement → Verify.

Target Metrics

MetricGoodNeeds WorkPoor
LCP< 2.5s2.5-4s> 4s
INP< 200ms200-500ms> 500ms
CLS< 0.10.1-0.25> 0.25
TTFB< 800ms800ms-1.8s> 1.8s

Quick Wins

1. Images (usually biggest impact on LCP)

html
<!-- Hero/LCP image: eager + high priority --><img src="/hero.webp" alt="Hero" width="1200" height="600" loading="eager" fetchpriority="high" decoding="async" />
<!-- Below fold: lazy load --><img src="/product.webp" alt="Product" width="400" height="300" loading="lazy" decoding="async" />

Always set width and height to prevent CLS.

2. Fonts (common LCP/CLS culprit)

html
<!-- Preconnect to font origin --><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<!-- Non-blocking font load --><link  rel="stylesheet"  href="https://fonts.googleapis.com/css2?family=Inter&display=swap"  media="print"  onload="this.media='all'"/>

3. Third-party Scripts (common INP killer)

html
<!-- Defer to user interaction --><script>  function loadThirdParty() {    // Load analytics, chat widgets, etc.  }  ;['scroll', 'click', 'touchstart'].forEach((e) => addEventListener(e, loadThirdParty, { once: true, passive: true }))  setTimeout(loadThirdParty, 5000)</script>

4. Critical CSS

Inline critical CSS in <head>, defer the rest:

html
<style>  /* critical styles */</style><link rel="preload" href="/styles.css" as="style" onload="this.rel='stylesheet'" />

Bundle Analysis

bash
# Webpacknpx webpack-bundle-analyzer dist/stats.json
# Vitenpx vite-bundle-visualizer
# Check package size before installingnpx bundlephobia <package-name>

Common heavy packages to replace:

  • moment (67KB) → date-fns (12KB) or dayjs (2KB)
  • lodash (72KB) → cherry-pick imports or native methods

Code Splitting Patterns

javascript
// React lazyconst Chart = lazy(() => import('./Chart'))
// Next.js dynamicconst Admin = dynamic(() => import('./Admin'), { ssr: false })
// Vite/Rollup manual chunksbuild: {  rollupOptions: {    output: {      manualChunks: {        vendor: ['react', 'react-dom']      }    }  }}

Caching Headers

# Static assets (immutable hash in filename)Cache-Control: public, max-age=31536000, immutable
# HTML (revalidate)Cache-Control: no-cache
# API responsesCache-Control: private, max-age=0, must-revalidate

Measurement

For running audits, reading reports, and setting budgets, use the perf-lighthouse skill.

Checklist

Images

  • Modern formats (WebP/AVIF)
  • Responsive srcset
  • width/height attributes
  • loading="lazy" below fold
  • fetchpriority="high" on LCP image

JavaScript

  • Bundle < 200KB gzipped
  • Code splitting by route
  • Third-party scripts deferred
  • No unused dependencies

CSS

  • Critical CSS inlined
  • Non-critical CSS deferred
  • No unused CSS

Fonts

  • font-display: swap
  • Preconnect to font origin
  • Subset if possible

Detailed Examples

For in-depth optimization patterns, see:

  • references/core-web-vitals.md [blocked] - Fixing LCP, CLS, INP issues
  • references/bundle-optimization.md [blocked] - Reducing JS bundle size
  • references/image-optimization.md [blocked] - Image formats, responsive images, sharp scripts

来源与署名

来源:tech-leads-club/agent-skills位于packages/skills-catalog/skills/(performance)/perf-web-optimization提交120b676

许可证: 无许可证

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

举报或申请下架