Html 如何防止html5页面缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15228697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:09:37  来源:igfitidea点击:

How to prevent html5 page from caching?

htmlcaching

提问by gps03

I converted a plain vanilla HTML page to HMTL5/CSS3 with a responsive layout, and for security reasons (dictated by the security people) the page must never cache.

我将一个普通的 HTML 页面转换为具有响应式布局的 HMTL5/CSS3,并且出于安全原因(由安全人员决定),页面绝不能缓存。

The page previously used <meta http-equiv="Pragma" content="no-cache">and <meta http-equiv="Expires" content="-1">to prevent the page from being cached.

以前使用的页面<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Expires" content="-1">防止页面被缓存。

What replaces this in HTML5? How do you prevent an html page from caching in the client?

什么在 HTML5 中取代了它?如何防止 html 页面在客户端缓存?

I've spent a week reading about manifest files, but they seem to do exactly opposite of what I want as attaching a manifest file explicitly causes the page it is attached to to cache.

我花了一周时间阅读有关清单文件的内容,但它们似乎与我想要的完全相反,因为附加清单文件显式导致它附加到缓存的页面。

And please don't refer me back to the w3c definition of which meta elements are now allowed — I understand that HTML5 does not include the cache-controlor Pragmain metaelements.

并且请不要让我回到 w3c 中关于现在允许哪些元元素的定义——我知道 HTML5 不包含cache-controlPragmameta元素中。

I need to know what it does include that will prevent a page from being cached.

我需要知道它包含什么会阻止页面被缓存。

回答by n1ckolas

In the beginning of code you need to use this:

在代码的开头,你需要使用这个:

<!DOCTYPE html>
<html manifest="manifest.appcache">
...

Then create manifest.appcachewith such content:

然后manifest.appcache用这样的内容创建:

CACHE MANIFEST

# Cache manifest version 1.0

# no cache

NETWORK:
*

回答by Dan Doyon

I dislike appcache a tremendous amount. It almost works well but can be a real unforgiving pain. While doing some code refactoring, I realized that after logout, i could browse back to the the last page. Of course, refreshing browser it would force user back to login but this is not desired.

我非常不喜欢 appcache。它几乎运作良好,但可能是一个真正无情的痛苦。在进行一些代码重构时,我意识到注销后,我可以浏览回最后一页。当然,刷新浏览器会强制用户重新登录,但这不是我们想要的。

After searching around and seeing the options, I started to get a bit frustrated. I do not want to use appcache. I then realized that my code was redirecting to login page after destroying the session and got an idea, what if, I redirect to the home page instead? And voila, page was loaded, session checked (and of course not there), user redirected to login. Problem solved.

在四处搜索并看到选项后,我开始有点沮丧。我不想使用 appcache。然后我意识到我的代码在销毁会话后重定向到登录页面并有了一个想法,如果我重定向到主页呢?瞧,页面已加载,会话已检查(当然不在那里),用户重定向到登录。问题解决了。

回答by Gladys

I have been struggling with the same issue for quite some time. What works for me - at least so far - in Chrome, FF and IE is doing the following:

1) reference the manifest file <html lang="nl" manifest="filename.appcache">From what I understand, this will cache everything that follows in this HTML document, hence a manifest file is needed to prevent this from happening:

2) use a manifest file filename.appcache with the following content which basically says: for all files, do not read from cache but from network server:

很长一段时间以来,我一直在为同样的问题苦苦挣扎。对我有用的 - 至少到目前为止 - 在 Chrome、FF 和 IE 中执行以下操作:

1)引用清单文件据<html lang="nl" manifest="filename.appcache">我所知,这将缓存此 HTML 文档中的所有内容,因此需要一个清单文件防止这种情况发生:

2)使用具有以下内容的清单文件 filename.appcache 基本上说:对于所有文件,不要从缓存中读取,而是从网络服务器中读取:

CACHE MANIFEST
# 2015-09-25 time 20:33 UTC v 1.01 
NETWORK:
*


3) a third step is required: each time you upload a (partial) update of your website, also change the manifest fileby changing the date and time stamp in the comment(#) line. Why? Because if you do not change the manifest file, it will not be read and it will default to step 1 and thus cache and read from cache. The fact that the manifest file is changed, however, enforces the manifest file to be read again, and thus enforces that the "do not read from cache but read from network server" instruction therein, is applied again.


3)需要第三步:每次上传网站的(部分)更新时,还要通过更改注释(#)行中的日期和时间戳来更改清单文件。为什么?因为如果您不更改清单文件,它将不会被读取,它将默认为第 1 步,从而缓存并从缓存中读取。然而,清单文件被改变的事实强制再次读取清单文件,从而强制再次应用其中的“不从缓存读取而是从网络服务器读取”指令。

回答by Gladys

The previous answermay not consistently work to prevent caching or to clear existing cache in chrome but there is a work around.

以前的答案可能并不一致地工作以防止缓存或清除现有缓存在Chrome,但有一个变通。

1) To clear existing cache in chrome, it may be necessary to update allfiles of the website(eg by linking to a new css file on every page) along with an update of the cache-manifestbefore existing cache in chrome gets cleared upon the second visit of a page (because of the "flow" of the way in which a page is rendered: the first time a page is visited, the browser reads and caches the manifest , while proceeding with loading from existing cache. Only upon the second visit will the newly stored updated manifest be read and applied).

1) 要清除 chrome 中的现有缓存,可能需要更新网站的所有文件(例如,通过链接到每个页面上的新 css 文件)以及缓存清单的更新,然后才能清除 chrome 中的现有缓存页面的第二次访问(因为页面呈现方式的“流程”:第一次访问页面时,浏览器读取并缓存清单,同时继续从现有缓存加载。仅在第二次访问将读取和应用新存储的更新清单)。

2) and if none of that helps, it is possible to include a script in the manifest file itselfto check for a new manifest and if found, reload it and use the new manifest. This did the trick and resolved all remaining cases I tested for where files had remained persistently cached in chrome. I found this script on this pageby Jason Stimpel.

2)如果这些都没有帮助,可以在清单文件本身中包含一个脚本来检查新清单,如果找到,重新加载它并使用新清单。这做到了这一点,并解决了我测试的所有剩余案例,其中文件一直缓存在 chrome 中。我在Jason Stimpel 的这个页面上找到了这个脚本。

<script type="text/javascript">
window.addEventListener('load', function (e) {
window.applicationCache.addEventListener('updateready', function (e) {
        window.location.reload();
    }, false);
}, false);
</script>