performance by addyosmani/web-quality-skills
npx skills add https://github.com/addyosmani/web-quality-skills --skill performance基于 Lighthouse 性能审计的深度性能优化。专注于加载速度、运行时效率和资源优化。
| 资源 | 预算 | 依据 |
|---|---|---|
| 页面总大小 | < 1.5 MB | 3G 网络约 4 秒加载完成 |
| JavaScript (压缩后) | < 300 KB | 解析 + 执行时间 |
| CSS (压缩后) | < 100 KB | 渲染阻塞 |
| 首屏图片 | < 500 KB | 对 LCP 的影响 |
| 字体 | < 100 KB | 防止 FOIT/FOUT |
| 第三方资源 | < 200 KB | 不可控的延迟 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
预连接到必需的源:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.example.com" crossorigin>
预加载关键资源:
<!-- LCP 图片 -->
<link rel="preload" href="/hero.webp" as="image" fetchpriority="high">
<!-- 关键字体 -->
<link rel="preload" href="/font.woff2" as="font" type="font/woff2" crossorigin>
延迟加载非关键 CSS:
<!-- 内联关键 CSS -->
<style>/* 首屏样式 */</style>
<!-- 非关键 CSS -->
<link rel="preload" href="/styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles.css"></noscript>
延迟加载非必要脚本:
<!-- 解析器阻塞(避免使用) -->
<script src="/critical.js"></script>
<!-- 延迟加载(推荐) -->
<script defer src="/app.js"></script>
<!-- 异步加载(用于独立脚本) -->
<script async src="/analytics.js"></script>
<!-- 模块(默认延迟) -->
<script type="module" src="/app.mjs"></script>
代码分割模式:
// 基于路由分割
const Dashboard = lazy(() => import('./Dashboard'));
// 基于组件分割
const HeavyChart = lazy(() => import('./HeavyChart'));
// 基于功能分割
if (user.isPremium) {
const PremiumFeatures = await import('./PremiumFeatures');
}
Tree shaking 最佳实践:
// ❌ 导入整个库
import _ from 'lodash';
_.debounce(fn, 300);
// ✅ 仅导入所需部分
import debounce from 'lodash/debounce';
debounce(fn, 300);
| 格式 | 使用场景 | 浏览器支持 |
|---|---|---|
| AVIF | 照片,最佳压缩率 | 92%+ |
| WebP | 照片,良好的备选方案 | 97%+ |
| PNG | 带透明度的图形 | 通用 |
| SVG | 图标、徽标、插图 | 通用 |
<picture>
<!-- 现代浏览器使用 AVIF -->
<source
type="image/avif"
srcset="hero-400.avif 400w,
hero-800.avif 800w,
hero-1200.avif 1200w"
sizes="(max-width: 600px) 100vw, 50vw">
<!-- WebP 备选 -->
<source
type="image/webp"
srcset="hero-400.webp 400w,
hero-800.webp 800w,
hero-1200.webp 1200w"
sizes="(max-width: 600px) 100vw, 50vw">
<!-- JPEG 备选 -->
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w,
hero-800.jpg 800w,
hero-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
width="1200"
height="600"
alt="Hero image"
loading="lazy"
decoding="async">
</picture>
<!-- 首屏 LCP 图片:急切加载,高优先级 -->
<img
src="hero.webp"
fetchpriority="high"
loading="eager"
decoding="sync"
alt="Hero">
<!-- 首屏以下图片:延迟加载 -->
<img
src="product.webp"
loading="lazy"
decoding="async"
alt="Product">
/* 系统字体栈作为备选 */
body {
font-family: 'Custom Font', -apple-system, BlinkMacSystemFont,
'Segoe UI', Roboto, sans-serif;
}
/* 防止文本不可见 */
@font-face {
font-family: 'Custom Font';
src: url('/fonts/custom.woff2') format('woff2');
font-display: swap; /* 对于非关键字体可使用 optional */
font-weight: 400;
font-style: normal;
unicode-range: U+0000-00FF; /* 子集化为拉丁字符 */
}
<link rel="preload" href="/fonts/heading.woff2" as="font" type="font/woff2" crossorigin>
/* 一个文件替代多个字重 */
@font-face {
font-family: 'Inter';
src: url('/fonts/Inter-Variable.woff2') format('woff2-variations');
font-weight: 100 900;
font-display: swap;
}
# HTML(短时间缓存或不缓存)
Cache-Control: no-cache, must-revalidate
# 带哈希的静态资源(不可变)
Cache-Control: public, max-age=31536000, immutable
# 不带哈希的静态资源
Cache-Control: public, max-age=86400, stale-while-revalidate=604800
# API 响应
Cache-Control: private, max-age=0, must-revalidate
// 静态资源优先使用缓存
self.addEventListener('fetch', (event) => {
if (event.request.destination === 'image' ||
event.request.destination === 'style' ||
event.request.destination === 'script') {
event.respondWith(
caches.match(event.request).then((cached) => {
return cached || fetch(event.request).then((response) => {
const clone = response.clone();
caches.open('static-v1').then((cache) => cache.put(event.request, clone));
return response;
});
})
);
}
});
// ❌ 强制多次重排
elements.forEach(el => {
const height = el.offsetHeight; // 读取
el.style.height = height + 10 + 'px'; // 写入
});
// ✅ 批量读取,然后批量写入
const heights = elements.map(el => el.offsetHeight); // 全部读取
elements.forEach((el, i) => {
el.style.height = heights[i] + 10 + 'px'; // 全部写入
});
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
// 对滚动/调整大小事件处理器进行防抖
window.addEventListener('scroll', debounce(handleScroll, 100));
// ❌ 可能导致卡顿
setInterval(animate, 16);
// ✅ 与显示刷新同步
function animate() {
// 动画逻辑
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
// 对于超过 100 项的列表,仅渲染可见项
// 使用如 react-window、vue-virtual-scroller 等库,或原生 CSS:
.virtual-list {
content-visibility: auto;
contain-intrinsic-size: 0 50px; /* 预估的项目高度 */
}
// ❌ 阻塞主线程
<script src="https://analytics.example.com/script.js"></script>
// ✅ 异步加载
<script async src="https://analytics.example.com/script.js"></script>
// ✅ 延迟到交互时加载
<script>
document.addEventListener('DOMContentLoaded', () => {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
const script = document.createElement('script');
script.src = 'https://widget.example.com/embed.js';
document.body.appendChild(script);
observer.disconnect();
}
});
observer.observe(document.querySelector('#widget-container'));
});
</script>
<!-- 在交互前显示静态占位符 -->
<div class="youtube-facade"
data-video-id="abc123"
onclick="loadYouTube(this)">
<img src="/thumbnails/abc123.jpg" alt="Video title">
<button aria-label="Play video">▶</button>
</div>
| 指标 | 目标 | 工具 |
|---|---|---|
| LCP | < 2.5s | Lighthouse, CrUX |
| FCP | < 1.8s | Lighthouse |
| 速度指数 | < 3.4s | Lighthouse |
| TBT | < 200ms | Lighthouse |
| TTI | < 3.8s | Lighthouse |
# Lighthouse CLI
npx lighthouse https://example.com --output html --output-path report.html
# Web Vitals 库
import {onLCP, onINP, onCLS} from 'web-vitals';
onLCP(console.log);
onINP(console.log);
onCLS(console.log);
关于核心 Web 指标的具体优化,请参阅 Core Web Vitals。
每周安装量
3.9K
代码仓库
GitHub 星标数
1.3K
首次出现
Jan 20, 2026
安全审计
安装于
opencode3.4K
gemini-cli3.3K
codex3.3K
github-copilot3.2K
cursor3.0K
kimi-cli2.7K
Deep performance optimization based on Lighthouse performance audits. Focuses on loading speed, runtime efficiency, and resource optimization.
| Resource | Budget | Rationale |
|---|---|---|
| Total page weight | < 1.5 MB | 3G loads in ~4s |
| JavaScript (compressed) | < 300 KB | Parsing + execution time |
| CSS (compressed) | < 100 KB | Render blocking |
| Images (above-fold) | < 500 KB | LCP impact |
| Fonts | < 100 KB | FOIT/FOUT prevention |
| Third-party | < 200 KB | Uncontrolled latency |
Preconnect to required origins:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.example.com" crossorigin>
Preload critical resources:
<!-- LCP image -->
<link rel="preload" href="/hero.webp" as="image" fetchpriority="high">
<!-- Critical font -->
<link rel="preload" href="/font.woff2" as="font" type="font/woff2" crossorigin>
Defer non-critical CSS:
<!-- Critical CSS inlined -->
<style>/* Above-fold styles */</style>
<!-- Non-critical CSS -->
<link rel="preload" href="/styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles.css"></noscript>
Defer non-essential scripts:
<!-- Parser-blocking (avoid) -->
<script src="/critical.js"></script>
<!-- Deferred (preferred) -->
<script defer src="/app.js"></script>
<!-- Async (for independent scripts) -->
<script async src="/analytics.js"></script>
<!-- Module (deferred by default) -->
<script type="module" src="/app.mjs"></script>
Code splitting patterns:
// Route-based splitting
const Dashboard = lazy(() => import('./Dashboard'));
// Component-based splitting
const HeavyChart = lazy(() => import('./HeavyChart'));
// Feature-based splitting
if (user.isPremium) {
const PremiumFeatures = await import('./PremiumFeatures');
}
Tree shaking best practices:
// ❌ Imports entire library
import _ from 'lodash';
_.debounce(fn, 300);
// ✅ Imports only what's needed
import debounce from 'lodash/debounce';
debounce(fn, 300);
| Format | Use case | Browser support |
|---|---|---|
| AVIF | Photos, best compression | 92%+ |
| WebP | Photos, good fallback | 97%+ |
| PNG | Graphics with transparency | Universal |
| SVG | Icons, logos, illustrations | Universal |
<picture>
<!-- AVIF for modern browsers -->
<source
type="image/avif"
srcset="hero-400.avif 400w,
hero-800.avif 800w,
hero-1200.avif 1200w"
sizes="(max-width: 600px) 100vw, 50vw">
<!-- WebP fallback -->
<source
type="image/webp"
srcset="hero-400.webp 400w,
hero-800.webp 800w,
hero-1200.webp 1200w"
sizes="(max-width: 600px) 100vw, 50vw">
<!-- JPEG fallback -->
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w,
hero-800.jpg 800w,
hero-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
width="1200"
height="600"
alt="Hero image"
loading="lazy"
decoding="async">
</picture>
<!-- Above-fold LCP image: eager loading, high priority -->
<img
src="hero.webp"
fetchpriority="high"
loading="eager"
decoding="sync"
alt="Hero">
<!-- Below-fold images: lazy loading -->
<img
src="product.webp"
loading="lazy"
decoding="async"
alt="Product">
/* System font stack as fallback */
body {
font-family: 'Custom Font', -apple-system, BlinkMacSystemFont,
'Segoe UI', Roboto, sans-serif;
}
/* Prevent invisible text */
@font-face {
font-family: 'Custom Font';
src: url('/fonts/custom.woff2') format('woff2');
font-display: swap; /* or optional for non-critical */
font-weight: 400;
font-style: normal;
unicode-range: U+0000-00FF; /* Subset to Latin */
}
<link rel="preload" href="/fonts/heading.woff2" as="font" type="font/woff2" crossorigin>
/* One file instead of multiple weights */
@font-face {
font-family: 'Inter';
src: url('/fonts/Inter-Variable.woff2') format('woff2-variations');
font-weight: 100 900;
font-display: swap;
}
# HTML (short or no cache)
Cache-Control: no-cache, must-revalidate
# Static assets with hash (immutable)
Cache-Control: public, max-age=31536000, immutable
# Static assets without hash
Cache-Control: public, max-age=86400, stale-while-revalidate=604800
# API responses
Cache-Control: private, max-age=0, must-revalidate
// Cache-first for static assets
self.addEventListener('fetch', (event) => {
if (event.request.destination === 'image' ||
event.request.destination === 'style' ||
event.request.destination === 'script') {
event.respondWith(
caches.match(event.request).then((cached) => {
return cached || fetch(event.request).then((response) => {
const clone = response.clone();
caches.open('static-v1').then((cache) => cache.put(event.request, clone));
return response;
});
})
);
}
});
// ❌ Forces multiple reflows
elements.forEach(el => {
const height = el.offsetHeight; // Read
el.style.height = height + 10 + 'px'; // Write
});
// ✅ Batch reads, then batch writes
const heights = elements.map(el => el.offsetHeight); // All reads
elements.forEach((el, i) => {
el.style.height = heights[i] + 10 + 'px'; // All writes
});
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
// Debounce scroll/resize handlers
window.addEventListener('scroll', debounce(handleScroll, 100));
// ❌ May cause jank
setInterval(animate, 16);
// ✅ Synced with display refresh
function animate() {
// Animation logic
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
// For lists > 100 items, render only visible items
// Use libraries like react-window, vue-virtual-scroller, or native CSS:
.virtual-list {
content-visibility: auto;
contain-intrinsic-size: 0 50px; /* Estimated item height */
}
// ❌ Blocks main thread
<script src="https://analytics.example.com/script.js"></script>
// ✅ Async loading
<script async src="https://analytics.example.com/script.js"></script>
// ✅ Delay until interaction
<script>
document.addEventListener('DOMContentLoaded', () => {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
const script = document.createElement('script');
script.src = 'https://widget.example.com/embed.js';
document.body.appendChild(script);
observer.disconnect();
}
});
observer.observe(document.querySelector('#widget-container'));
});
</script>
<!-- Show static placeholder until interaction -->
<div class="youtube-facade"
data-video-id="abc123"
onclick="loadYouTube(this)">
<img src="/thumbnails/abc123.jpg" alt="Video title">
<button aria-label="Play video">▶</button>
</div>
| Metric | Target | Tool |
|---|---|---|
| LCP | < 2.5s | Lighthouse, CrUX |
| FCP | < 1.8s | Lighthouse |
| Speed Index | < 3.4s | Lighthouse |
| TBT | < 200ms | Lighthouse |
| TTI | < 3.8s | Lighthouse |
# Lighthouse CLI
npx lighthouse https://example.com --output html --output-path report.html
# Web Vitals library
import {onLCP, onINP, onCLS} from 'web-vitals';
onLCP(console.log);
onINP(console.log);
onCLS(console.log);
For Core Web Vitals specific optimizations, see Core Web Vitals.
Weekly Installs
3.9K
Repository
GitHub Stars
1.3K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode3.4K
gemini-cli3.3K
codex3.3K
github-copilot3.2K
cursor3.0K
kimi-cli2.7K
97,600 周安装