简体   繁体   English

如何要求浏览器缓存文件以备将来使用

[英]How to ask browser to cache files for future use

I have an app that is using some large libraries. 我有一个使用一些大型库的应用程序。 There are almost 200 js and css files being loaded. 几乎有200个js和css文件正在加载。 And this is how it goes. 事情就是这样。 First it loads 30 of them listed on first html. 首先,它加载第一个html中列出的其中30个。 Than after running some. 比跑了以后。 they trigger loading their own required files. 他们触发加载自己所需的文件。 After that library starts, I run a callback that starts other library that loads their own files. 该库启动后,我运行一个回调,该回调启动其他加载其自己文件的库。

So in first load network activity almost stops several times before it starts downloading 50 other files. 因此,在首次加载时,网络活动几乎停止了几次,然后才开始下载50个其他文件。 And website opening time is almost 20 seconds. 而且网站的开放时间几乎是20秒。 But it is mainly because there are big gaps in between library loads. 但这主要是因为库加载之间存在很大的差距。

Since I have the list of all 200 files is there any way that I can ask browser to start downloading all these files and only run in when it is required. 由于我拥有所有200个文件的列表,因此我可以要求浏览器开始下载所有这些文件,并且仅在需要时才运行。 So that it will work on downloading all these libraries in the very beginning. 这样一来,它就可以开始下载所有这些库。

My first approach was I thought maybe I can add file list to HTTP header of first html file. 我的第一种方法是我想我可以将文件列表添加到第一个html文件的HTTP标头中。 I looked for https://en.wikipedia.org/wiki/List_of_HTTP_header_fields and couldn't see one that I can send list of files that will be used in this html file. 我寻找了https://zh.wikipedia.org/wiki/List_of_HTTP_header_fields ,但看不到我可以发送将用于此html文件的文件列表。 And google did not offered me any other solution too at least I couldnt find it. 谷歌也没有为我提供任何其他解决方案,至少我找不到它。

UPDATE: 更新:

This files are all at CDN, I cannot change the headers for assets, I can only change my initial html file headers. 该文件全部位于CDN,我无法更改资产的标头,只能更改我的初始html文件标头。

First bit, how to get it to cache. 首先,如何获取缓存。 This comes from the server. 这来自服务器。 Basically, set an Expires , ETag , Cache-Control or some combination to configure how long it should stick around. 基本上,设置ExpiresETagCache-Control或某种组合来配置应该保留的时间。 Regardless of how it is loaded, if it has one of these headers, it'll stick around. 无论它如何加载,如果它具有这些头文件之一,它就会留下来。 Do note that it isn't guaranteed to stick around (especially on mobile, where disk space is more of a premium), but most will. 请注意,它不能保证持久存在(尤其是在磁盘空间更为重要的移动设备上),但大多数情况下都会如此。

As for pre-loading them, it may be unnecessary. 至于预加载它们,则可能是不必要的。 If they all load and get cached, then subsequent runs may not have to wait and you might be good. 如果它们都加载并被缓存,则随后的运行可能不必等待,您可能会很好。 This way, they'll load in the right order and only run when you want. 这样,它们将以正确的顺序加载,并且仅在您需要时运行。

If you still want them to all pull down right away and you have a list of them, you can just add them each as <script> tags. 如果您仍然希望它们全部立即下拉,并且有它们的列表,则可以将它们分别添加为<script>标记。 You can either render this list as script tags server-side, or pull the list and use some JavaScript to dynamically add script tags. 您可以将此列表呈现为服务器端的脚本标签,也可以拉列表并使用一些JavaScript动态添加脚本标签。

If these script tags have load-time dependencies (ie, B must be started before A or B will fail, etc), you'll need to make sure you have them in the right order. 如果这些脚本标签具有加载时间相关性(即B必须在A或B失败之前启动,等等),则需要确保按正确的顺序排列它们。

If they have onload functionality, then you'll have to somehow wrap that up so it'll only trigger when you want. 如果他们具有onload功能,则您必须以某种方式将其包装起来,以便仅在需要时才触发。

If you are using apache you would need to use a plugin called mod_expires in .htaccess . 如果您使用的是apache ,则需要在.htaccess使用一个名为mod_expires的插件。

In a nutshell, the browser will ask the server to get the file and then the browser responds with the file and expiration time. 简而言之,浏览器将要求服务器获取文件,然后浏览器以文件和到期时间响应。 If the browser will be getting the same file again, it will decide whether the file is old enough to actually make a request to get a new one, otherwise it will used the cached version. 如果浏览器将再次获取同一文件,它将决定该文件是否足够旧以实际请求获取新文件,否则将使用缓存的版本。

For this to work your .htaccess would look like this: 为此,您的.htaccess将如下所示:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 week”
ExpiresByType image/jpeg "access 1 week"
ExpiresByType image/gif "access 1 week"
ExpiresByType image/png "access 1 week"
ExpiresByType image/svg "access 1 week"
ExpiresByType image/svg+xml "access 1 week"
ExpiresByType text/css "access 1 hour"
ExpiresByType text/x-javascript "access 1 hour"
ExpiresByType text/javascript "access 1 hour"
ExpiresByType application/javascript "access 1 hour”
ExpiresByType application/x-javascript "access 1 hour”
ExpiresByType application/pdf "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 week"
ExpiresByType image/x-icon "access 1 week"
</IfModule>

The rules above, add expiration for Content types like png, jpg, jpeg, javascript etc. for an hour or a week depending on the content type. 上面的规则为png,jpg,jpeg,javascript等内容类型添加了一个小时或一周的有效期,具体取决于内容类型。

The solution I found is preloading , it allows to load files in advance. 我发现的解决方案是preloading ,它允许提前加载文件。

I had to add tags to my html file: 我必须在我的html文件中添加标签:

<link rel="preload" href="https://example.com/style.css" as="style">
<link rel="preload" href="https://example.com/example.js" as="script">
<link rel="preload" href="https://example.com/example.woff2" as="font" type="font/woff2" crossorigin>

More Info at : https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content 有关更多信息, 访问: https : //developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content

And here: https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/ 在这里: https : //www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/

Preloading Browser support : https://caniuse.com/#feat=link-rel-preload 预加载浏览器支持https : //caniuse.com/#feat=link-rel-preload

Preconnect might also be helpful if you want to start SSL connection beforehand. 如果你想提前启动SSL连接PRECONNECT也可能会有所帮助。

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

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