将javascript缩小为整个网站的单个捆绑包或特定的每个页面捆绑包更好吗? [英] Is it better to minify javascript into a single bundle for the whole site, or a specific-to-each-page bundle?

查看:70
本文介绍了将javascript缩小为整个网站的单个捆绑包或特定的每个页面捆绑包更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web开发中缩小JavaScripts时,从用户加载时间的角度来看,它是否更好:

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


  1. make一个包含所有脚本的大型JavaScript包,并在每个页面上包含这个 - 所以每个页面可能不需要全部,但是一旦用户将其缓存,他们就不需要再获取任何脚本(直到它当然,从他们的缓存到期) - 优化请求数量

  2. 每页制作一个JavaScript包,这样每个页面只加载它需要的脚本而不是其他任何东西 - 因此,首次加载时每个页面肯定需要一个JS请求(但后来仍然有缓存。优化请求大小。

我对一些数据感兴趣,以决定采用哪种策略。我可以像其他人一样轻松地根据轶事得出结论: - )

I'm interested in some data upon which to base the decision for which strategy to go with. I can arrive at conclusions based on anecdote as easily as everyone else :-)

推荐答案

重新ally取决于脚本的大小和功能。对于您的所有页面都有一个 master.js 是很常见的,其中包含站点每个页面所需的所有功能,同时还有其他js文件用于某些页面上可能只需要的功能。

It really depends on the sizes and functions of the script. It's common to have a single master.js for all your pages, which contains all the functionality required by every page of your site, whilst having other js files for functionality that might only be needed on certain pages.

以Stack Overflow为例。他们在网站的每个页面上都包含 master.js 文件,但是当您访问问题页面或询问问题页面时,您会注意到 wmd.js 。此脚本包含编辑器的所有功能,这些功能在较少的页面上是必需的。

Take Stack Overflow, for instance. They have a master.js file included on every page of the site, but when you visit a question page or the "ask a question" page you'll notice wmd.js. This script includes all the functionality for the editor which is needed on fewer pages.

这篇关于将javascript缩小为整个网站的单个捆绑包或特定的每个页面捆绑包更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆