I am currently tackling a large website that has accumulated quite a bit of technical debt that needs to be addressed. The site contains a significant amount of JavaScript and CSS files being loaded. Currently, these files are aggregated and minified in layers, with one layer being used on each page, while other layers are only loaded on specific pages that require them.
For instance:
page 1:
- default.css
- page1.css
- some-feature.css
- default.js
- page1.js
- some-feature.js
page 2:
- default.css
- page2.css
- default.js
- page2.js
page 3:
- default.css
- page3.css
- some-feature.css
- some-other-feature.css
- default.js
- page3.js
- some-feature.js
- some-other-feature.js
In addition to these resources, there are numerous external resources being loaded for tracking, advertising, social integration, and more.
I have a hunch that these resources could load faster (both initially and in subsequent requests) if they were combined and minified into one single JS and CSS file per page. For example, page1.css + page1.js
, and on another page it would be page2.css + page2.js
. However, this approach may result in certain content being loaded redundantly (such as the original default.css
).
What is the recommended method for loading these resources efficiently? And do you have any test results regarding this approach?