Reputation: 1493
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?
Upvotes: 9
Views: 2057
Reputation: 36487
Impossible to say in general, because it is very situational.
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.
Upvotes: 6
Reputation: 1
Yes, it will improve page load time but still this method is not often used due to these reasons:
And yeah, for faster page load, you can use a BUILD SYSTEM like GRUNT, GULP, BRUNCH etc. for better performance.
Upvotes: 0