简体   繁体   English

如何检查我的JavaScript是否正确缓存

[英]How to check whether my JavaScript is cached properly

I have a script in my JSP page.The script code was very large and I came to know it's not a best practice to have such huge amount of script code in JSP code. 我的JSP页面中有一个脚本,该脚本代码非常大,我知道在JSP代码中包含如此大量的脚本代码不是最佳实践。

I have separated the script to a js file and included that js file in JSP page. 我已将脚本分离为一个js文件,并将该js文件包含在JSP页面中。 I have the following questions: 我有以下问题:

  1. How can I verify whether my js file is cached? 如何验证我的js文件是否已缓存? Is there any browser tool available to check whether my js file is getting cached or not? 有没有可用的浏览器工具来检查我的js文件是否已被缓存?
  2. Should I write any piece of code to cache the js file? 我应该编写任何代码来缓存js文件吗?

First, I would look at my server logs, and if the .js file is not being loaded every time the main page is loaded, then your script is probably cached. 首先,我将查看服务器日志,如果每次加载主页时都未加载.js文件,则可能已缓存了脚本。 But to be more direct to your question - 但更直接地回答您的问题-

I would put some dynamic code inside my .JS file that creates a function to do a callback to the server and send the date/time stamp of when it was loaded last. 我会在.JS文件中放入一些动态代码,以创建一个函数以对服务器进行回调并发送上次加载时间的日期/时间戳。 Since I dont know which language you are writing in, I will use some conceptual notation where <<timeloaded>> is replaced with a url friendly date/time value: 由于我不知道您使用的是哪种语言,因此我将使用一些概念性的符号将<<timeloaded>>替换为url友好的日期/时间值:

in yourfile.js include something like this... 在yourfile.js中包括这样的内容...

function (){
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", "lastload.htm?<<timeloaded>>", false );
}()

so when the browser requests yourfile.js the first time, the time is inserted into the js file dynamically by your server side app code. 因此,当浏览器第一次请求yourfile.js时,服务器端应用程序代码会将时间动态插入js文件中。 Then each time the browser "loads" the file, it will call your server to look for a file called lastload.htm and pass the time that .js file was loaded. 然后,每次浏览器“加载”该文件时,它将调用您的服务器以查找名为lastload.htm的文件,并传递.js文件的加载时间。

Inserting that time value into your file can be done many different ways which is beyond the scope of your question. 可以将时间值插入文件中的方法很多,这超出了您的问题范围。

If you have Chrome, open the Inspector (right-click, "Inspect Element"), then click the Network tab. 如果您使用的是Chrome,请打开检查器(右键单击“检查元素”),然后单击“网络”选项卡。 Now reload the page. 现在重新加载页面。 You should see a list of all of the elements that were loaded for the page. 您应该看到为该页面加载的所有元素的列表。

To determine if the Javascript (or any element) was cached, what you want to look for is the "Size" column. 要确定是否已缓存Javascript(或任何元素),您要查找的是“大小”列。 Data that is browser-cached will show "(from cache)" as the Size value. 浏览器缓存的数据将显示“(来自缓存)”作为“大小”值。 If you see anything else for Size, that means it was loaded over the network and wasn't loaded from the browser cache. 如果您看到Size的其他内容,则表示它是通过网络加载的,不是从浏览器缓存加载的。

To answer the second part of your question, yes it's definitely a good idea to make sure you're sending headers to make things browser cache anything on your website that is static. 要回答问题的第二部分,是的,确保发送标头绝对是个不错的主意,以使浏览器在您的网站上缓存任何静态内容。 You can't use Javascript to set the cache headers, that would need to be done by the web server. 您不能使用Javascript设置缓存标头,而这需要由Web服务器来完成。 How you do that entirely depends on your environment, but it can be done by either editing your web server configuration, or by a script if you're using a server-side language such as PHP. 具体操作方式完全取决于您的环境,但是可以通过编辑Web服务器配置或脚本(如果使用的是服务器端语言,例如PHP)来完成。

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

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