Web performance resources - Learn web development 编辑

Draft

This page is not complete.

There are many reasons why your website should perform as well as possible.
Below is a quick review of best practices, tools, APIs with links to provide more information about each topic.

Best practices

  • Start with learning the critical rendering path of the browser. Knowing this will help you understand how to improve the performance of the site.
  • Using resource hints such as rel=preconnect, rel=dns-prefetch, rel=prefetch, and rel=preload
  • Keep the size of Javascript to a minimum. Only use as much Javascript as needed for the current page.
  • CSS performance factors
  • Use HTTP/2 on your server (or CDN).
  • Use a CDN for resources which can reduce load times significantly.
  • Compress your resources using gzipBrotli, and Zopfli.
  • Image optimization (use CSS animation, or SVG if possible).
  • Lazy loading parts of your application outside the viewport. If you do, have a backup plan for SEO (e.g render full page for bot traffic); for example, by using the loading attribute on the <img> element
  • It is also crucial to realize what is really important to your users. It might not be absolute timing, but user perception.

Quick Wins

CSS

Web performance is all about user experience and perceived performance. As we learned in the critical rendering path document, linking CSS with a tradional link tag with rel="stylesheet" is synchronous and blocks rendering. Optimize the rendering of your page by removing blocking CSS.

To load CSS asynchronously one can simpy set the media type to print and then change to all once loaded. The following snippet includes an onload attribute, requiring Javascript, so it is important to include a noscript tag with a traditional fallback.

<link rel="stylesheet" href="/path/to/my.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="/path/to/my.css"></noscript>

The downside with this approach is the flash of unstyled text (FOUT.) The simplist way to address this is by inlining CSS that is required for any content that is rendered above the fold, or what you see in the browser viewport before scrolling. These styles will improve perceived performance as the CSS does not require a file request.

<style type="text/css">
// Insert your CSS here
</style>

Javascript

Avoid Javascript blocking by using the async or defer attributes, or link javascript assets after the page's DOM elements. Javascript only block rendering for elements that appear after the script tag in the DOM tree.

Web Fonts

EOT and TTF formats are not compressed by default. Apply compression such as GZIP or Brotli for these file types. Use WOFF and WOFF2. These formats have compression built in.

Within @font-face use font-display: swap. By using font display swap the browser will not block rendering and will use the backup system fonts that are defined. Optimiize font weight to match the web font as closely as possible.

Icon web fonts

If possible avoid icon web fonts and use compressed SVGs. To further optimize inline your SVG data within HTML markup to avoid HTTP requests.

Tools

APIs

Things not to do (bad practices)

  •   Download everything
  •   Use uncompressed media files

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:110 次

字数:6880

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文