简体   繁体   English

网站性能(通过Lighthouse):更少的HTTP请求或更少的渲染阻止?

[英]Site Performance (via Lighthouse): Less HTTP Requests or Less Render Blocking?

So I've been checking out Chrome's new Audits panel with Lighthouse, and slowly been improving my site's metrics. 因此,我一直在使用Lighthouse查看Chrome的新“审核”面板,并逐渐改善了我网站的指标。 It currently sits at 95 (performance), 97 (accessibility) and 75 (best practices). 它目前位于95(性能),97(可访问性)和75(最佳实践)上。

I started going through the recommendations, and clicked the "learn more" links on several sections. 我开始仔细阅读建议,然后单击几个部分的“了解更多”链接。 As a result, I'm now reading through the Google Developers website. 因此,我现在正在阅读Google Developers网站。 However the two articles that are specifically of importance for my question, are Render-Blocking Resources and Render-Blocking CSS . 但是,对我的问题特别重要的两篇文章是Render-Blocking ResourcesRender-Blocking CSS

From the Render-Blocking Resources article, here is the most important excerpt (the CSS article as a whole essentially carries on with this in more detail): 从“ 渲染阻止资源”一文中,这是最重要的摘录(整个CSS文章实质上对此进行了更详细的介绍):

  • For critical scripts, consider inlining them in your HTML. 对于关键脚本,请考虑将其内联到HTML中。 For non-critical scripts, consider marking them with the async or defer attributes. 对于非关键脚本,请考虑使用asyncdefer属性对其进行标记。 See Adding Interactivity with JavaScript to learn more. 请参阅添加与JavaScript的交互以了解更多信息。
  • For stylesheets, consider splitting up your styles into different files, organized by media query, and then adding a media attribute to each stylesheet link. 对于样式表,请考虑按照媒体查询将样式拆分为不同的文件,然后将media属性添加到每个样式表链接。 When loading a page, the browser only blocks the first paint to retrieve the stylesheets that match the user's device. 加载页面时,浏览器仅阻止第一个绘画来检索与用户设备匹配的样式表。 See Render-Blocking CSS to learn more. 请参阅渲染阻止CSS以了解更多信息。
  • For non-critical HTML imports, mark them with the async attribute. 对于非关键的HTML导入,请使用async属性标记它们。 As a general rule, async should be used with HTML imports as much as possible. 通常, async应该与HTML导入一起使用。

Now, in order for my site to achieve its current scores, here is what is currently in place. 现在,为了使我的网站达到当前评分,这是当前的位置。 (I should note that I do plan on taking advantage of them all, if possible. This is simply as the site currently stands.) (我应该注意,如果可能的话,我确实计划充分利用它们。这只是该站点当前的状态。)

Current Implementations 当前实施

  • The site is 100% designed to be mobile-first, and is compatible with device resolutions ranging from 240 pixels up to 3840 pixels in width. 该网站100%设计为移动优先,并且与设备分辨率兼容,范围从240像素到3840像素。 I am using adaptive design, with a few breakpoints. 我正在使用自适应设计,但有一些断点。
  • I'm using HTTPS. 我正在使用HTTPS。
  • I'm using icon sprites. 我正在使用图标精灵。
  • I'm using non-minified CSS and JavaScript, split into several files, because the site theme is still under development. 由于站点主题仍在开发中,因此我使用的是非最小化的CSS和JavaScript,它们分为多个文件。 The CSS files contain media queries. CSS文件包含媒体查询。
  • <a> elements are using target="_blank" and rel="noopener" is absent. <a>元素正在使用target="_blank"并且不存在rel="noopener"
  • <script> elements are not using async or defer attributes. <script>元素未使用asyncdefer属性。
  • I'm not using HTTP/2, or service workers. 我没有使用HTTP / 2或服务工作者。
  • I have enabled GZIP compression via cPanel ("Optimize Website"), targeting all content. 我已经通过cPanel(“优化网站”)启用了GZIP压缩,定位所有内容。 (I am prepared to limit this to just HTML, PHP, CSS, JavaScript, plain text and XML, if the average load times are faster [I plan on running a benchmark to test both scenarios by loading the site 10 times for each — I haven't had a chance to test this yet].) (如果平均加载时间更快,我准备将其限制为仅HTML,PHP,CSS,JavaScript,纯文本和XML [我计划通过运行基准测试来对这两种情况分别加载10次,以测试这两种情况。还没有机会进行测试]。)

The Problems 问题所在

The main CSS file is fairly large (currently 75 kiB). 主CSS文件相当大(当前为75 kiB)。 On-top of that, there is the jQuery file that is already minified (95 kiB), plus several smaller CSS files which are specific to certain areas of the site. 最重要的是,已经缩小了jQuery文件(95 kiB),还有一些较小的CSS文件,这些文件特定于网站的某些区域。 Users are also expected to view multiple pages within a single visit due to the nature of my site (a microprocessor information repository). 由于我的网站(微处理器信息存储库)的性质,还希望用户一次访问就可以浏览多个页面。

In order to comply with the outlined guidelines above, I'm contemplating splitting up the CSS files to not only be dependent on the section of the site as they currently are, but also by media query, linking to them with <link> elements using the media attribute, versus queries within the CSS files themselves. 为了遵守上述概述的准则,我正在考虑将CSS文件拆分为不仅依赖于网站当前所在的部分,还依赖于媒体查询,并通过<link>元素使用media属性,而不是CSS文件本身中的查询。

For JavaScript, I'm thinking about grouping all async scripts together in one file, and all defer scripts together in another. 对于JavaScript,我正在考虑将所有async脚本归为一个文件,而将所有defer脚本归为另一个文件。 I'm assuming there are no problems with grouping the jQuery API code with other self-written functions? 我假设将jQuery API代码与其他自写函数组合在一起没有问题吗?

Okay. 好的。 There are my plans, but in taking a step back for a moment and thinking about how this would be implemented, a couple of questions were brought to my attention, and I was hoping you guys could help me decide. 我有计划,但是在退后一会儿并思考如何实现时,引起了我几个问题,我希望你们能帮助我做出决定。 This kind of thought process is very new to me (also partly because I've never had a site of this size before), so any input would be great. 这种思考过程对我来说是很新的(部分原因是我以前从未有过这么大的站点),因此任何投入都会很棒。


The Questions 问题

  1. Since all CSS files are downloaded, irrespective of whether they are used, I'm not sure whether one large main CSS file is the best way to go, or whether to have more HTTP requests and split it up by media type/query and section of the site. 由于所有CSS文件均已下载,无论是否使用它们,我都不确定一个大型主CSS文件是否是最佳选择,还是不确定是否有更多HTTP请求并按媒体类型/查询和部分将其拆分该网站。 Regardless of the method, CSS will be minified. 无论采用哪种方法,CSS都将被最小化。

     <link rel="stylesheet" href="reset-min.css"> <!-- ~ 1.5 kiB; all media types --> <link rel="stylesheet" href="layout-min.css"> <!-- ~ 50 kiB; internal @media --> <link rel="stylesheet" href="color-min.css"> <!-- ~ 25 kiB; internal @media --> <!-- 3 HTTP requests; 76.5 kiB Main CSS file split into "layout" and "color" for easy editing in the future. --> vs. <link rel="stylesheet" href="reset-min.css"> <!-- ~ 1.5 kiB; all media types --> <link rel="stylesheet" href="global-screen.css" media="screen"> <!-- ~ 7 kiB; site-wide elements --> <link rel="stylesheet" href="color-screen.css" media="screen"> <!-- ~ 10 kiB --> <link rel="stylesheet" href="database-screen.css" media="screen"> <!-- 20 kiB; not on all pages --> <link rel="stylesheet" href="global-print.css" media="print"> <!-- ~ 6 kiB; site-wide elements --> <link rel="stylesheet" href="color-print.css" media="print"> <!-- ~ 7 kiB --> <link rel="stylesheet" href="database-print.css" media="print"> <!-- ~ 7 kiB; not on all pages --> <!-- 7 HTTP requests; 58.5 kiB CSS files split into "global/database" and "color" for easy editing in the future. --> 
  2. The first question also applies to JavaScript files. 第一个问题也适用于JavaScript文件。 I have the main JS file for my functions, plus the jQuery API file. 我具有用于功能的主要JS文件以及jQuery API文件。 However, since the functions will be split up by async or defer attributes, this will result in more HTTP requests, but of smaller size. 但是,由于将通过asyncdefer属性来拆分功能,因此这将导致更多的HTTP请求,但大小会更小。 JS will also be minified. JS也将被缩小。

     <script src="jquery-min.css"> <!-- ~ 95 kiB; local --> <script src="scripts-min.css"> <!-- ~ 21.3 kiB --> <!-- 2 HTTP requests; 116.3 kiB --> vs. <script src="scripts-async-min.css" async> <!-- ~ 108.1 kiB; includes jQuery API --> <script src="scripts-defer-min.css" defer> <!-- ~ 4.3 kiB --> <!-- 2 HTTP requests; 112.4 kiB --> 
  3. My last question is really concerning the main culprits of reduced webpage loading — the jQuery API file, and web fonts. 我的最后一个问题确实涉及减少网页加载的主要根源-jQuery API文件和Web字体。 Since they take up HTTP requests anyway, would you recommend outsourcing directly from the jQuery CDN and Google, versus hosting the files locally? 由于它们仍然处理HTTP请求,您是否建议直接从jQuery CDN和Google外包,而不是在本地托管文件? I bring this up as I'm assuming the CDNs would be faster to serve. 我提出这一点是因为我认为CDN的投放速度会更快。 However, I would be taking advantage of service workers where possible to reduce the need to download files. 但是,我将尽可能利用服务人员来减少下载文件的需求。


Thank You! 谢谢!

Thank you for reading through this long post. 感谢您阅读这篇长文章。 I understand that I have probably gone into too much detail, but I didn't want to leave anything out that may be important. 我知道我可能已经讲了太多细节,但是我不想遗漏任何可能很重要的东西。

Here are my views on the questions you raised: 这是我对您提出的问题的看法:

1) Browser can open multiple parallel connections to a single server while loading the files. 1)浏览器可以在加载文件时打开到单个服务器的多个并行连接。 However, the catch lies in the limits on the number of these connections. 但是,问题在于这些连接数量的限制。 Loading a single file of 76.5 KiB will usually be slower than loading 3 files of 25 KiBs. 加载76.5 KiB的单个文件通常比加载25 KiB的3个文件要慢。 This won't hold true always since the Server handshake time 'might' be greater than the time taken to actually fetch the resource, especially in your case were the file size is 70Kibs and average Internet Speeds are ever-increasing. 由于服务器握手时间“可能”大于实际获取资源所花费的时间,所以这种情况永远不会成立,尤其是在文件大小为70KB和平均Internet速度不断提高的情况下。 I guess the best solution will be highly dependent on the geographical areas you are targeting. 我想最好的解决方案将高度取决于您所针对的地理区域。 Heres a discussion on parallel connections limit : Max parallel http connections in a browser? 这里是关于并行连接限制的讨论: 浏览器中的最大并行http连接数?

2) Same predicate applies to the js files as well. 2)同样的谓词也适用于js文件。 However, jQuery is almost global these days. 但是,jQuery如今几乎是全球性的。 There's a very very high chance that user has visited a website that required before coming to your website. 用户访问您的网站之前需要访问的网站的可能性非常高。 Hence, using a popular CDN would mean that THE jQuery FILE WILL NOT BE DOWNLOADED AT ALL AND LOADED FROM THE BROWSER CACHE. 因此,使用流行的CDN意味着将完全不会下载jQuery文件,也不会从浏览器缓存中加载jQuery文件。 There goes one of your biggest culprit. 您最大的罪魁祸首之一。 Even if the file does not exists in the browser cache, using a CDN should be the preferred practice, as CDNs by definition, are Geographically Distributed Systems and a good chance exists that the CDN can provide a lower latency if the visitor is not geographically in the same region as your server. 即使文件不存在于浏览器缓存中,使用CDN仍应是首选做法,因为根据定义,CDN是地理上分散的系统,并且如果访问者不在地理上,则很有可能CDN可以提供较低的延迟。与您的服务器相同的区域。

The javascript code that is not immediately required to be executed should be loaded async or deffered. 并非立即需要执行的javascript代码应异步加载或延迟。 What you do of these two further depends upon the browsers you are targeting. 您对这两者的操作还取决于您要定位的浏览器。 Older browser won't see any advantage with deferred loading. 较旧的浏览器不会看到延迟加载的任何优势。 Refer: Script Tag - async & defer 参考: 脚本标签-异步和延迟

3) The same rational applies to fonts as well.If you are using a popular font, chances are it already exists in the cache. 3)同样的理由也适用于字体。如果您使用的是流行字体,则很可能它已经存在于缓存中。 Using service workers to manage jquery and fonts seems like an overkill to me. 对我来说,使用服务人员来管理jquery和字体似乎是过大了。 You might want to use them for your other assets though. 不过,您可能希望将它们用于其他资产。

Also, I would like to add that you should try make the webpages load progressively. 另外,我想补充一点,您应该尝试逐步加载网页。 Put only the required code inside the head section. 仅将所需的代码放入头部。 JS code which is executed upon user interaction should be loaded after the DOM content, ie, just above the closing body tag. 在用户交互时执行的JS代码应在DOM内容之后(即,在封闭主体标签上方)加载。

I would also suggest to defer loading the css which is being used to style the content rather than fixing the layout. 我还建议推迟加载用于样式化内容的CSS,而不是固定布局。 This includes all the colours, fonts and other fancy stuff. 这包括所有颜色,字体和其他奇特的东西。

If your purpose is to get a good score on the Chrome Audit, things I mentioned might not stand correct. 如果您的目的是在Chrome审核中获得良好的成绩,那么我提到的内容可能并不正确。 But for serving actual users, this is what I would personally do. 但是,为了服务于实际用户,这是我个人要做的。

Question 1: You can keep separate css files with you for easy editing. 问题1:您可以随身携带单独的CSS文件,以方便编辑。 Use vswebessentials to bundle and minify for the production version. 使用vswebessentials捆绑和缩小生产版本。 When you are hosting it on server, put up a far future expires date or use cache manifest. 当您将其托管在服务器上时,请设置一个较远的过期日期或使用缓存清单。 Then, you would need to rename the bundled file when you make the changes. 然后,您需要在进行更改时重命名捆绑的文件。 Site will work lot faster. 网站将工作得更快。

A 75kb css is not large in today's date when downloaded over single connection, if you give a far future expiry date and your client makes no other request for that for any other date for the domain. 通过单连接下载时,如果今天的日期为75kb,则css并不大,如果您提供了一个远期的到期日期,并且您的客户端对该域的任何其他日期都没有其他要求。 Hopefully, you are not going to change css daily. 希望您不会每天更改CSS。 You can break it down too as per media query specification. 您也可以按照媒体查询规范将其细分。

Question 2 Loading jquery from cdn is a good idea but you need to make sure that it is available before your js is going to execute. 问题2从cdn加载jquery是一个好主意,但是您需要确保在js执行之前,它可以使用。 Do not use service workers for simpler stuff. 不要将服务人员用于简单的事情。 If you want to check, audit www.google.co.in, your audit numbers may be better than google. 如果您想检查www.google.co.in进行审核,则您的审核编号可能会比Google更好。 So, do not complicate development with audit numbers. 因此,不要使审核编号使开发复杂化。 Concentrate on UX. 专注于UX。 That's what google is doing. 这就是谷歌在做什么。

Question 3 You can and should use CDN for jquery and fonts. 问题3您可以并且应该将CDN用于jquery和字体。 Use cache manifest and far future expiry, if you can instead of following the audit tab for numbers and service workers. 如果可以,请使用缓存清单和远期到期,而不要遵循号码和服务工作者的审核选项卡。

I wanted a high rating for my website for my progressive web app (Angular, AWS serverless) for free. 我希望我的网站免费获得我的渐进式Web应用程序(Angular,AWS serverless)的高评价。 the ratings I got are in the image below. 我得到的评分如下图所示。 This is what I would advise against your questions... 对于您的问题,我会建议您这样做...

1) Put the CSS file required for above the fold content in the HTML page itself, for the rest of the css files, let them load asynchronously. 1)将折叠内容上方所需的CSS文件放在HTML页面本身中,对于其余的CSS文件,让它们异步加载。 be careful about unused css, I spent time cleaning and removing unused classes. 小心未使用的CSS,我花时间清理和删除未使用的类。

2) Do you really use a lot of jquery? 2)您真的使用很多jquery吗? for best results, you might want to write vanilla javascript instead of using jquery in the above-the-fold content especially. 为了获得最佳结果,您可能想编写原始的javascript而不是在上面的内容中特别使用jquery。 in my website https://beta.akberiqbal.com i wrote plain javascript for the above-the-fold items (navigation etc). 在我的网站https://beta.akberiqbal.com中,我为上述项目(导航等)编写了简单的javascript。 and below the fold, I had my three Angular5 JS files. 在折叠之下,我得到了三个Angular5 JS文件。 the bundle.css file was small, so I made it part of the HTML also. bundle.css文件很小,因此我也将其作为HTML的一部分。

3) if you have a service worker app, a local fetch should be faster than a CDN. 3)如果您有Service Worker应用程序,则本地获取应该比CDN更快。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM