Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it faster to load if all webpage resources are compiled into a single HTML file?

What if I had a compilation step for my website that turned all external scripts and styles into a single HTML file with embedded <script> and <style> tags? Would this improve page load times due to not having to send extra GETs for the external files? If so, why isn't this done more often?

like image 333
oink Avatar asked Jun 22 '16 04:06

oink


People also ask

What is better to load one complete JavaScript file on all pages or separate files based on different pages?

It is best to keep separate files or include all files in one file Javascript? To avoid multiple server requests, it is better to group all your JavaScript files into only one.

Why CSS loads faster than HTML?

It's all quite simple and involves one goal: less characters and as a result, smaller file sizes and faster-loading web pages. So “minifying” JavaScript and CSS ultimately leads to faster page load speed.


1 Answers

Impossible to say in general, because it is very situational.

  • If you're pulling resources from many different servers, these requests can slow your page loading down (especially with some bad DNS on the visiting side).
  • Requesting many different files may also slow down page load even if they're from the same origin/server.
  • Keep in mind not everyone has gigabit internet (or even on megabit level). So putting everything directly into your HTML file (inlining or using data URIs) will definitely reduce network overhead at first (less requests, less headers, etc.).
  • In addition (and making the previous point even worse) this will also break many other features often used to reduce page loading times. For example, resources can't be cached - neither locally nor on some proxy - and are always transferred. This might be costly for both the visitor as well as the hosting party.

So often the best way to approach this is going the middle ground, if loading times are an issue to you:

  • If you're using third party scripts, e.g. jQuery, grab these from a public hosted CDN that's used by other pages as well. If you're lucky, your visitor's browser will have a cached copy and won't do the request.

  • Your own scripts should be condensed and potentially minified into a single script (tools such as browersify, webpack, etc.). This must not include often changing parts, as these would force you to transfer even more data more often.

  • If you've got any scripts or resources that are really only part of your current visitor's experience (like logged in status, colors picked in user preferences, etc.), it's okay to put these directly into the parent HTML file, if that file is customized anyway and delivering them as separate files wouldn't work or would cause more overhead. A perfect example for this would be CSRF tokens. Don't do this if you're able to deliver some static HTML file that's filled/updated by Javascript though.

like image 60
Mario Avatar answered Oct 17 '22 03:10

Mario