Is it better to minify javascript into a single bundle for the whole site, or a specific-to-each-page bundle?

后端 未结 4 1542
萌比男神i
萌比男神i 2021-01-03 22:18

When minifying JavaScripts together in web-development, is it better from the user-loading-time point of view to:

  1. make one single big bundle of JavaScript cont
4条回答
  •  星月不相逢
    2021-01-03 23:10

    It depends entirely on what's in your scripts. If you've got loads of small functions which are used by a wide selection of pages then yes, a single file will be best. If you've got a large script that's only used by one page, you wouldn't typically want to slow down the initial front-page load time by including it in the shared script.

    So what you will typically end up with is a compromise, with base functions shared across all pages in one script, and the more complex and specific functions in per-page or per-page-group scripts. It'll very rarely be beneficial to go the whole option-2 hog and have a completely separate script for each page.

    Having shared functions in one file and separate page-specific complex scripts is also typically more maintainable.

提交回复
热议问题