网站html由于缓存而不会为用户更新 [英] Website html doesnt update for users because of cache

查看:291
本文介绍了网站html由于缓存而不会为用户更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站,但遇到了我的用户的网站缓存问题。我开发了我的网站,并设置了chrome开发人员工具来禁用网站缓存以进行开发。问题是,当我发布新的更改以使我的所有用户由于其浏览器缓存而无法获得更新时,该问题发生了。当我在朋友计算机上手动删除网站缓存时,它可以工作,但是我显然不能期望每个人都这样做以获取新的更新。无论如何,我可以通过版本控制解决问题吗?我环顾四周,但似乎找不到任何东西。

I am making a website and am running into an issue with website cache for my users. I develop my website and have set chrome developer tools to disable cache for my website for development. The issue is when i release a new change to prod all my users don't get the update because of their browser cache. When i delete the cache for my website manually on a friends computer it works but i obviously cant expect everyone to do this to get the new updates. Is there anyway for me to get around this with versioning or something? i have looked around but cant seem to find anything.

编辑:我知道我完全可以防止缓存,但是我不想完全防止看起来像a的缓存错误的设计

edit: i know i can prevent caching at all but i don't want to completely prevent caching that seems like a bad design

推荐答案

正在缓存哪些资源?我怀疑是js / css文件,一种解决此问题的好方法是在这些资源的路径中添加带有版本的查询参数,以在版本更改时强制浏览器加载新文件,如下所示:

What are the resources that are being cached? I suspect js/css files, a good way to handle this is to add a query param with a version to the path of those resources in order to force the browser to load the new file if the version changed, something like this:

<script type="text/javascript" src="your/js/path/file.js?v=1"></script>
<link href="/css/main.css?v=1" media="screen,print" rel="stylesheet" type="text/css">

当您发布网站的新更新时,请替换以下版本:

And when you release a new update of your website, replace the version as follows:

<script type="text/javascript" src="your/js/path/file.js?v=2"></script>
<link href="/css/main.css?v=2" media="screen,print" rel="stylesheet" type="text/css">

浏览器会发现该文件是一个新文件,它将更新缓存。希望对您有所帮助。

The browser will thing that the file is a new file and it will update the cache. Hope this helps.

要禁用html缓存,您可以按如下所示将metatag添加到文件中:

In order to disable html caching, you can add a metatag to your file as follows:

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

但这将完全禁用具有此元标记的html文件的缓存,我不认为有一种与js / css文件一样容易处理的方法,尽管如此,您可以设置元标记以在将来的日期刷新html。这是一篇介绍了如何在需要更多信息时使用该元标记的文章:

But this will entirely disable caching of html files that have this metatag, I don't think there is a way to handle this as easily as with js/css files, you can set the metatag to refresh the html in a future date though. Here is an article describing how to use that metatag if you need more info:

http://www.metatags.info/meta_http_equiv_cache_control

这篇关于网站html由于缓存而不会为用户更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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