简体   繁体   English

内联与包含的js和CSS?

[英]Inline vs included js and css?

In an environment with at least 500ms latency over 2G mobile connections (~0.1mbps), what's the fastest and most efficient way of sending a about 10kb of css and js, in around 5-10 files on the server, to the client? 在2G移动连接上至少有500ms延迟(约0.1mbps)的环境中,最快和最有效的方法是在服务器上以大约5-10个文件的形式将大约10kb的CSS和js发送到客户端吗?

I can think of three options: 我可以想到三种选择:

  1. combining all js to one file and all css to one file 将所有js合并到一个文件,将所有CSS合并到一个文件
  2. linking all css and js files one by one 一对一链接所有css和js文件
  3. inline everything 内联一切

I know google uses inline, but that's probably just to save server sockets. 我知道google使用内联,但这可能只是为了节省服务器套接字。 They are even saving ram by running in stateless mode - they trust the clients to remember the sessions for them. 他们甚至通过在无状态模式下运行来节省内存-他们信任客户端为他们记住会话。 Server power isn't an issue at all. 服务器电源根本不是问题。

On the other hand, facebook seem to autogenerate their css (their names are base64 encoded), but into over 10 different files sent to the user, and they don't even seem to optimize it that heavily; 另一方面,facebook似乎可以自动生成其css(它们的名称是base64编码的),但是可以发送到发送给用户的10多个不同的文件中,而且它们似乎并没有对其进行大量优化。 only some whitespace removal. 仅删除一些空格。

I'm already passing all the files through a function that compresses everything, so any one of these are feasible. 我已经通过压缩所有内容的函数传递了所有文件,因此其中任何一个都是可行的。 I don't want to choose the first alternative because it's easier. 我不想选择第一个替代方法,因为它更容易。

The first two takes advantage of caching (the second one a bit less than the first one) but the second only requires three requests to the server, and the third only requires one get request from the server (ignoring the few images we might have on some of the pages). 前两个利用了缓存的优势(第二个比第一个少一点),但是第二个只需要对服务器的三个请求,而第三个只需要一个来自服务器的get请求(忽略了我们可能拥有的少量映像)一些页面)。

Does Android / iOS cache js and css across restarts of the browser? Android / iOS是否在浏览器重新启动时缓存js和CSS? If not, then inline sounds better. 如果没有,那么内联听起来更好。

The only goal is to minimize the average load time of the user. 唯一的目标是最大程度地减少用户的平均加载时间。 Each user will be spending about 100 page loads on the site per day, seeing about 40 css and js files per day. 每个用户每天将在网站上花费大约100个页面加载,每天会看到大约40个CSS和JS文件。 The css and js is basically static content. CSS和JS基本上是静态内容。 It's set to cache 30 days, and we change the url if the file changes using /path/to/file.ext?md5-hash-of-file . 它设置为缓存30天,如果文件使用/path/to/file.ext?md5-hash-of-file更改,我们将更改url。 Also, everything is gzipped wherever possible. 此外,一切都将在所有可能的地方压缩。

EDIT: 编辑:

I think i should clarify the two options I found for number two. 我认为我应该澄清为第二个发现的两个选项。 Is it a good idea to use a single file for css and js across the whole site? 在整个网站上为CSS和JS使用单个文件是个好主意吗? It would only use two requests and remove any double (or septuple) caching because a single function is in two or more different combined js files, but a download of up to 1MB doesn't sound that good. 它只会使用两个请求并删除任何双重(或七元组)缓存,因为单个功能位于两个或多个不同的组合js文件中,但是下载多达1MB的声音听起来并不好。

Today it's basically one combined css per view, so every time you view the same page again the content is cached. 今天,它基本上是每个视图结合在一起的CSS,因此,每次您再次查看同一页面时,都会缓存内容。 However, some js and css is used on more than one page. 但是,在多个页面上使用了一些js和CSS。

内联css和javascript将使您的页面变得沉重。将所有样式表和所有javascript文件合并为一个并将其包含到页面中是一种很好的做法。与内联样式相比,这会使您的页面非常快速。

It really depends on the usage. 这实际上取决于用法。 For a page with only one time visitors , I would recommend inlining everything. 对于只有一次访问者的页面,我建议内联所有内容。 This makes for a faster initial load (a single request vs multiple requests) and easier to work with. 这样可以提高初始加载速度(单个请求与多个请求),并且更易于使用。 This is the case for landing pages, help pages, wizards and similar one-use pages. 登陆页面,帮助页面,向导和类似的一次性页面就是这种情况。

However, if you are expecting recurring visitors , I'd recommend using an external file. 但是,如果您希望经常性访问者 ,我建议您使用外部文件。 While the first load will be slower, you make it up with near-zero load time afterwards for these assets. 虽然第一次加载的速度较慢,但​​是您随后为这些资产添加了接近零的加载时间。 This is the case for most websites. 大多数网站都是这种情况。

The problem with #2, linking to each file, is that the biggest factor in load time for small elements is round trip time, not file size. 链接到每个文件的#2问题在于,小元素加载时间的最大因素是往返时间,而不是文件大小。 It takes several round trips to set up the connection to get each file. 建立连接以获取每个文件需要几次往返。 This also means that you should combine your css and js files. 这也意味着您应该结合使用css和js文件。 In your high-latency environment, round trips will be especially painful. 在您的高延迟环境中,往返旅行会特别痛苦。 Here's google's advice on round trips 这是谷歌关于往返的建议

As others have pointed out, #3, inlining, means that files cannot be cached. 正如其他人指出的那样,内联#3表示无法缓存文件。 It can slow down load times because of the increased size of the html. 由于html的大小增加,它可能会减慢加载时间。 However you avoid the roundtrip penalty. 但是,您可以避免往返罚款。

In your environment, I would also recommend looking at the HTML5 application cache to optimize caching for css and js files. 在您的环境中,我还建议您查看HTML5应用程序缓存,以优化css和js文件的缓存。 You would need to convert your app to use AJAX calls instead of loading html pages, but doing that also reduces the needed data transfer. 您可能需要将应用程序转换为使用AJAX调用,而不是加载html页面,但是这样做还可以减少所需的数据传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM