Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap CDN Rendering Delay

Have been working on a site and have used Boostrap via MAX CDN by putting

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 

in the head section.

Google Pagespeed is showing:

Your page has 1 blocking CSS resources. This causes a delay in rendering your page. None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML. Optimize CSS Delivery of the following:

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css

Is there anyway of fixing this?

Thanks

like image 679
Walruss Avatar asked Feb 13 '17 16:02

Walruss


1 Answers

The other answer is too incorrect, so I am writing this if anyone still needs help on this.

CDNs speed up your production websites too, not only your local development environment. Hosting a static file locally on your own host does not solve the Render Blocking issue that pagespeed tool suggests here. A static file hosted locally is still render blocking.

The solution here is inlining the render blocking CSS files completely. However, I would recommend to not inline the resources for several reasons:

  1. Resources delivered over CDN are delivered faster. If you inline them, the entire payload is expected to deliver at slow pace.
  2. Resources delivered separately are cached by browser, while if you inline CSS resources browser will not be able to reuse them across requests.
  3. Resources inlined are compressed on every request along with the other dynamic contents of the file. You cannot take advantage of pre-compression that comes with CDN.
  4. Bigger HTML document due to inlined CSS files is expected to slow when it comes to parsing.

What should one do to resolve this issue? You might think differently, but I would suggest you to do nothing.

  1. Its not that bad if the browser has to wait a little for resources to download. If you are using CDN, chances are visitors will not perceive the download as most of the CDNs now a days have less than 50ms average global latency.
  2. Resources are cached by browser. While pagespeed tool loads fresh resources on every request, please note that browser may be loading some of the resources from cache, completely eliminating the CDN request.
  3. Resources are shared across websites. If you are using Bootstrap from a public CDN, chances are that the same bootstrap file is already available in browser cache of the visitor, that is downloaded when the user visited another website that used bootstrap. This gives 0ms latency and 100% bandwidth saving for that particular resource for even your first time visitors that have no other resources of your site in their browser cache. Browser can now spend the saved bandwidth elsewhere to speed other things up.
  4. Manually inlining external libraries make it little more difficult to keep traces of all inlined copies of the library and makes the edits and updates hard.
  5. Dynamic inlining adds few more disk seeks per request. This just adds to the server load. These IOs can be saved if the files are served separately. Your OS may just cache the files if they are hot enough. Using CDN will completely eliminate this load.

Although not exactly a solution that will remove this pagespeed warning, its possible to reduce the impact of render blocking resources for real visitors (not performance measurement tools):

  1. Serve resources with aggressive compression to reduce the payload size.
  2. Serve resources with immutable cache-control header to tell the browser to confidently store this file for longer period as this is not going to change in the future. If you use bootstrap cdn by pagecdn, these two optimizations are enabled by default.
  3. If you know a file is going to be loaded immediately after a page load, you can use HTTP/2 Server Push to deliver the file before even the browser asks for it. However, if you do this, you will need to make sure that the same files are not aggressively been pushed on every request (that is not a good option as the files should load from browser cache on second request onwards).
like image 178
Hamid Sarfraz Avatar answered Oct 09 '22 22:10

Hamid Sarfraz