这是一篇持续更新的Lighthouse前端性能优化的文章, 详细的最佳实战见下文:
Lighthouse
Lighthouse 提供针对性能、无障碍功能、渐进式 Web 应用、SEO 等方面的审核。我们可以对任何公开网页或需要身份验证的网页运行 Lighthouse。
我们可以在 Chrome 开发者工具中、从命令行运行 Lighthouse,也可以将其作为节点模块运行。您向 Lighthouse 提供了一个进行审核的网址,它会对相应网页运行一系列审核,然后生成一份有关网页性能的报告。然后,参考未通过的审核,确定如何改进网页。
每项审核都有一个参考文档,其中说明了此次审核的重要性及相应的修正方法。
性能(Performance)
衡量性能并寻找提升网页加载速度的机会。
- 最大内容渲染
Largest Contentful Paint element
This is the largest contentful element painted within the viewport. Learn more about the Largest Contentful Paint element
LCP
- Reduce unused JavaScript
Reduce unused JavaScript and defer loading scripts until they are required to decrease bytes consumed by network activity. Learn how to reduce unused JavaScript.
FCP
LCP
- Eliminate render-blocking resources
Resources are blocking the first paint of your page. Consider delivering critical JS/CSS inline and deferring all non-critical JS/styles. Learn how to eliminate render-blocking resources.
FCP
LCP
- Preconnect to required origins
Consider adding
preconnect
ordns-prefetch
resource hints to establish early connections to important third-party origins. Learn how to preconnect to required origins.FCP
LCP
- Reduce unused CSS
Reduce unused rules from stylesheets and defer CSS not used for above-the-fold content to decrease bytes consumed by network activity. Learn how to reduce unused CSS.
FCP
LCP
- Avoid serving legacy JavaScript to modern browsers
Polyfills and transforms enable legacy browsers to use new JavaScript features. However, many aren't necessary for modern browsers. For your bundled JavaScript, adopt a modern script deployment strategy using module/nomodule feature detection to reduce the amount of code shipped to modern browsers, while retaining support for legacy browsers. Learn how to use modern JavaScript
FCP
LCP
- Image elements do not have explicit width and height
Set an explicit width and height on image elements to reduce layout shifts and improve CLS. Learn how to set image dimensions
CLS
- Enable text compression
Text-based resources should be served with compression (gzip, deflate or brotli) to minimize total network bytes. Learn more about text compression.
FCP
LCP
- Use HTTP/2
HTTP/2 offers many benefits over HTTP/1.1, including binary headers and multiplexing. Learn more about HTTP/2.
FCP
LCP
- Serve static assets with an efficient cache policy
A long cache lifetime can speed up repeat visits to your page. Learn more about efficient cache policies.
- Ensure text remains visible during webfont load
Leverage the
font-display
CSS feature to ensure text is user-visible while webfonts are loading. Learn more aboutfont-display
.
- Does not use passive listeners to improve scrolling performance
Consider marking your touch and wheel event listeners as
passive
to improve your page's scroll performance. Learn more about adopting passive event listeners.
- Avoids an excessive DOM size
A large DOM will increase memory usage, cause longer style calculations, and produce costly layout reflows. Learn how to avoid an excessive DOM size.
TBT
- JavaScript execution time
Consider reducing the time spent parsing, compiling, and executing JS. You may find delivering smaller JS payloads helps with this. Learn how to reduce Javascript execution time.
TBT
- Minimizes main-thread work
Consider reducing the time spent parsing, compiling and executing JS. You may find delivering smaller JS payloads helps with this. Learn how to minimize main-thread work
TBT
- Minimize third-party usage
Third-party code can significantly impact load performance. Limit the number of redundant third-party providers and try to load third-party code after your page has primarily finished loading. Learn how to minimize third-party impact.
TBT
- Avoid long main-thread tasks
Lists the longest tasks on the main thread, useful for identifying worst contributors to input delay. Learn how to avoid long main-thread tasks
TBT
- Avoid non-composited animations
Animations which are not composited can be janky and increase CLS. Learn how to avoid non-composited animations
CLS
- Initial server response time was short
Keep the server response time for the main document short because all other requests depend on it. Learn more about the Time to First Byte metric.
FCP
LCP
- Avoids enormous network payloads
Large network payloads cost users real money and are highly correlated with long load times. Learn how to reduce payload sizes.
- Avoid chaining critical requests
The Critical Request Chains below show you what resources are loaded with a high priority. Consider reducing the length of chains, reducing the download size of resources, or deferring the download of unnecessary resources to improve page load. Learn how to avoid chaining critical requests.
- Properly size images
Serve images that are appropriately-sized to save cellular data and improve load time. Learn how to size images.
FCP
LCP
- Defer offscreen images
Consider lazy-loading offscreen and hidden images after all critical resources have finished loading to lower time to interactive. Learn how to defer offscreen images.
FCP
LCP
- Minify CSS
Minifying CSS files can reduce network payload sizes. Learn how to minify CSS.
FCP
LCP
- Minify JavaScript
Minifying JavaScript files can reduce payload sizes and script parse time. Learn how to minify JavaScript.
FCP
LCP
- Efficiently encode images
Optimized images load faster and consume less cellular data. Learn how to efficiently encode images.
FCP
LCP
- Serve images in next-gen formats
Image formats like WebP and AVIF often provide better compression than PNG or JPEG, which means faster downloads and less data consumption. Learn more about modern image formats.
FCP
LCP
- Avoid multiple page redirects
Redirects introduce additional delays before the page can be loaded. Learn how to avoid page redirects.
FCP
LCP
- Use video formats for animated content
Large GIFs are inefficient for delivering animated content. Consider using MPEG4/WebM videos for animations and PNG/WebP for static images instead of GIF to save network bytes. Learn more about efficient video formats
FCP
LCP
- Remove duplicate modules in JavaScript bundles
Remove large, duplicate JavaScript modules from bundles to reduce unnecessary bytes consumed by network activity.
FCP
LCP
- Preload Largest Contentful Paint image
If the LCP element is dynamically added to the page, you should preload the image in order to improve LCP. Learn more about preloading LCP elements.
LCP
- User Timing marks and measures
Consider instrumenting your app with the User Timing API to measure your app's real-world performance during key user experiences. Learn more about User Timing marks.
- Lazy load third-party resources with facades
Some third-party embeds can be lazy loaded. Consider replacing them with a facade until they are required. Learn how to defer third-parties with a facade.
TBT
- Largest Contentful Paint image was not lazily loaded
Above-the-fold images that are lazily loaded render later in the page lifecycle, which can delay the largest contentful paint. Learn more about optimal lazy loading.
LCP
- Avoid large layout shifts
These are the largest layout shifts observed on the page. Each table item represents a single layout shift, and shows the element that shifted the most. Below each item are possible root causes that led to the layout shift. Some of these layout shifts may not be included in the CLS metric value due to windowing. Learn how to improve CLS
CLS
- Avoids document.write()
For users on slow connections, external scripts dynamically injected via
document.write()
can delay page load by tens of seconds. Learn how to avoid document.write().
- Has a tag with width or initial-scale
A
<meta name="viewport">
not only optimizes your app for mobile screen sizes, but also prevents a 300 millisecond delay to user input. Learn more about using the viewport meta tag.
- Page didn't prevent back/forward cache restoration
Many navigations are performed by going back to a previous page, or forwards again. The back/forward cache (bfcache) can speed up these return navigations. Learn more about the bfcache
无障碍 (Accessibility)
确定是否所有用户都能有效访问网站内容并浏览您的网站。
渐进式 Web 应用,最佳实践 (Best Practices)
遵循以下最佳实践可改善网页的代码健康状况。
搜索引擎优化SEO (Search Engine Optimization)
确保您的网页针对搜索引擎结果排名进行了优化。