Howto:Javascript 文件始终是最新的
我有一个使用大量 JavaScript 的 .NET Web 应用程序。 .aspx 和 .js 文件密切相关。 问题:.aspx 文件在客户端上始终是最新的(未缓存),但 .js 文件很可能已缓存。如果文件仅缓存一个会话,这甚至会成为一个问题,因为用户在我的网站上花费了很多时间,并且每次我更新 .aspx/.js 对时,用户都会遇到问题。
现在,我找到了一个解决方案,但我不确定是否有更好的解决方案或者我的解决方案是否存在性能缺陷。
这是我的解决方案:
.aspx 中的 .js-References:
<script type='text/javascript' src='../scripts/<%# GetScriptLastModified("MyScript.js") %>'></script>
因此,“GetScriptLastModified” 将附加一个 ?v= 参数,如下所示:
protected string GetScriptLastModified(string FileName)
{
string File4Info = System.Threading.Thread.GetDomain().BaseDirectory + @"scripts\" + FileName;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(File4Info);
return FileName + "?v=" + fileInfo.LastWriteTime.GetHashCode().ToString();
}
因此,呈现的 .js-Link 对客户端来说将如下所示:
<script type='text/javascript' src='/scripts/GamesCharts.js?v=1377815076'></script>
链接将更改有时,当我上传新版本时,我可以确保用户在更改后立即获得新的脚本或图像。
I have a .NET web applications which uses a lot of javascript. The .aspx and the .js files go hand-in-hand together.
Problem: The .aspx files are always up-to-date on the client (not cached) but the .js files might well be cached. This is even a problem if the files are only cached for one session since users are spending many hours on my site and everytime I update a .aspx/.js pair users are running into a problem.
Now, I found a solution but I am not sure if there is perhaps a better solution or if my solution has a performance drawback.
This is my solution:
.js-References in .aspx:
<script type='text/javascript' src='../scripts/<%# GetScriptLastModified("MyScript.js") %>'></script>
So, the "GetScriptLastModified" will append a ?v= parameter like this:
protected string GetScriptLastModified(string FileName)
{
string File4Info = System.Threading.Thread.GetDomain().BaseDirectory + @"scripts\" + FileName;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(File4Info);
return FileName + "?v=" + fileInfo.LastWriteTime.GetHashCode().ToString();
}
So, the rendered .js-Link would look like this to the client:
<script type='text/javascript' src='/scripts/GamesCharts.js?v=1377815076'></script>
The link will change every time, when I upload a new version and I can be sure that the user immediately gets a new script or image when I change it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Safari 拒绝缓存带有查询参数的 URL。因此,您可以使用版本化路径之类的东西来代替查询参数,并使用 mod_rewrite 来删除它。
类似于:
在 Apache 配置文件中(其他服务器的配置留作家庭作业):
Safari refuses to cache URLs with query parameters. So instead of a query parameter you can use something like a versioned path and use mod_rewrite to remove it.
Something like:
And in Apache config file (config for other servers left as homework):
我们在环境中所做的是手动增加调用脚本中的计数器:
每当 js 文件有任何更新时,我们只需增加计数器:
您的方法会自动工作,但每次调用脚本时都需要检索 LastWriteTime 。如果是高流量站点,则会增加 CPU 处理量。
What we do in our environment is we manually increase the counter in the calling script :
Whenever there is any update to the js file , we just increment the counter :
Your method works automatically but it needs to retrieve the LastWriteTime every time the script being called. If its a high traffic site, it will increase the CPU processing.
只是一个想法:您可能可以将 JS 渲染为 ASPX;我之前曾使用 JSP 强制重新加载 CSS。我并不为这个解决方案感到自豪,但它以前就有效过。
然后只需在 ASPX 文件中静态渲染 JS 即可。
Just a thought: You could probably just render the JS as an ASPX; I've done this for forceful reload of CSS before with a JSP. Not proud of that solution, but it's worked before.
Then just render the JS statically in the ASPX file.