.js文件是否被缓存? [英] Are the .js files being cached?

查看:488
本文介绍了.js文件是否被缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一个网站,并对.js文件进行了更改,但是当我从FTP服务器上删除.js文件并上传新文件时,新文件不会显示在网站上。我检查了网站上.js文件背后的源代码,它不正确,它显示了旧文件的来源,而不是新文件的来源,即使旧文件不在。那是因为我的浏览器缓存了.js文件吗?



注意:我有这个源代码
$ b $

 < meta http-equiv =cache-controlcontent =no-cache/> 

可以阻止浏览器缓存我的页面,而且我知道它适用于HTML,但在那里有源代码,资源文件是否仍然被缓存?



我的其他页面上没有这行代码,只是我的主页,但是.js文件仍然在其他页面被引用,所以也许这就是它如何获得缓存?



此外,有没有办法检查您的浏览器缓存?编辑:我刚刚清除了我的浏览器缓存并重新加载了网站,并且该文件正如现在应该那样工作,所以这意味着该文件没有被缓存。所以现在我的问题变成了如何防止资源文件被缓存?

解决方案

code> meta 标签是要走的路。它否定了全部缓存,并且与类型相违背,可以快速访问网页。

每个页面都有c $ c>标签,所以如果你有一个没有它的页面引用你的JS,它将被缓存。



防止JS文件被广泛接受的方法(并且CSS再次被缓存)是为了区分它们的请求:



说,你有:

 < script type =text / JavaScriptsrc =somescript.js>< / script> 

这个会缓存它(除非上面的元标记存在。)



你想要的是,在每个页面加载上面的行看起来不同(URL的),如下所示:

 < script type =text / JavaScriptsrc =somescript.js?some-randomly-generated-string>< / script> 



同样适用于CSS。



如果您使用的是某种JS网络,照顾你,因为你给了它某种无缓存配置选项。



当然,你也可以在纯JS中做到这一点。



现在,您可以使用日期通常情况下,你不希望否定所有的缓存,所以这样做的方法是在你的URL中添加一个版本参数:

 < script type =text / JavaScriptsrc =somescript.js?version = 1.0.0>< / script> 

a nd从那里管理你的脚本。



编辑

任何额外的延伸。除非我大错特错,否则所有Chrome版本都会内置Chrome开发人员工具(当然,在测试版和开发版中),并可通过按 Ctrl-Shift-I 访问。在网络选项卡中,您可以看到所有请求,内容和标题。


I recently made a website and I made a change to a .js file, but when I delete the .js file from the FTP server and upload the new one, the new file doesn't show up on the website. I checked the source code behind the .js file on the website and it's not right, it's showing the source for the old file, not the new one even though the old one is gone. Is that because my browser cached the .js file?

Note: I have this source

<meta http-equiv="cache-control" content="no-cache" />

on my page to stop the browser from caching my page, and I know that works on the HTML, but with that source there, do the resource files still get cached?

I don't have that line of code on my other pages, just my home page, but the .js file is still referenced on the other pages so perhaps that is how it is getting cached?

Furthermore, is there a way to check your browsers cache? I use chrome.

Edit: I just cleared my browsers cache and reloaded the website and the file worked as it should now, so that means the file did get cached. So now my question becomes how to I prevent a resource file from being cached?

解决方案

I am not positive that no-cache meta tag is the way to go. It negates all caching and kind defeats the purpose of quickly accessible pages.

Also, AFAIK, meta tag works per page, so if you have a page without it that references your JS - it will be cached.

The widely acceptable way of preventing JS files (and, again, CSS) from being cached is to differentiate the requests for them:

Say, you have:

<script type="text/JavaScript" src="somescript.js″></script>

this one will cache it (unless the above meta-tag is present.

What you want to have is that on each page load the above line looks different (URL-wise) like so:

<script type="text/JavaScript" src="somescript.js?some-randomly-generated-string″></script>

Same goes for CSS.

If you were using some sort of JS network - it would take care of that for you had you given it some sort of "no-cache" configuration option.

You, of course, can do it in pure JS too. Some sort of Date related string is an option.


Now, normally, you would not want to negate all the caching, so the way to do so is to add a version parameter to your URL:

<script type="text/JavaScript" src="somescript.js?version=1.0.0″></script>

and manage your scripts from there.

EDIT

There is no need for any additional extension. Unless I am sorely mistaken, "Chrome Developer Tools" is built in in all Chrome versions (in beta and dev, for sure) and is accessible by pressing Ctrl-Shift-I. There, in "Network" tab, you can see all your requests, there content and headers.

这篇关于.js文件是否被缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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