Howto:Javascript 文件始终是最新的

发布于 2024-09-24 06:45:07 字数 1021 浏览 4 评论 0原文

我有一个使用大量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度℉ 2024-10-01 06:45:07

Safari 拒绝缓存带有查询参数的 URL。因此,您可以使用版本化路径之类的东西来代替查询参数,并使用 mod_rewrite 来删除它。

类似于:

<script type='text/javascript' src='/scripts/1377815076/GamesCharts.js'></script>

在 Apache 配置文件中(其他服务器的配置留作家庭作业):

RewriteEngine On
RewriteRule  ^/scripts/[0-9]+/(.+)$    /scripts/$1

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:

<script type='text/javascript' src='/scripts/1377815076/GamesCharts.js'></script>

And in Apache config file (config for other servers left as homework):

RewriteEngine On
RewriteRule  ^/scripts/[0-9]+/(.+)$    /scripts/$1
感性不性感 2024-10-01 06:45:07

我们在环境中所做的是手动增加调用脚本中的计数器:

<script type='text/javascript' src='/scripts/GamesCharts.js?v=1'></script>

每当 js 文件有任何更新时,我们只需增加计数器:

<script type='text/javascript' src='/scripts/GamesCharts.js?v=2'></script>

您的方法会自动工作,但每次调用脚本时都需要检索 LastWriteTime 。如果是高流量站点,则会增加 CPU 处理量。

What we do in our environment is we manually increase the counter in the calling script :

<script type='text/javascript' src='/scripts/GamesCharts.js?v=1'></script>

Whenever there is any update to the js file , we just increment the counter :

<script type='text/javascript' src='/scripts/GamesCharts.js?v=2'></script>

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.

烟花易冷人易散 2024-10-01 06:45:07

只是一个想法:您可能可以将 JS 渲染为 ASPX;我之前曾使用 JSP 强制重新加载 CSS。我并不为这个解决方案感到自豪,但它以前就有效过。

<script type='text/javascript' src='/mycode/GamesCharts.js.aspx'></script>

然后只需在 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.

<script type='text/javascript' src='/mycode/GamesCharts.js.aspx'></script>

Then just render the JS statically in the ASPX file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文