Html html5 元标记缓存控制不再有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6664542/
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
html5 meta tag cache-control no longer valid?
提问by powtac
How do I define
我如何定义
<meta http-equiv="cache-control" content="no-cache" />
in HTML5? It is no longer valid according to the W3C Validator and the documentation.
在 HTML5 中?根据 W3C 验证器和文档,它不再有效。
回答by wandarkaf
Putting caching instructions into meta tags is not a good idea, because although browsers may read them, proxies won't. For that reason, they are invalid and you should send caching instructions as real HTTP headers.
将缓存指令放入元标记并不是一个好主意,因为尽管浏览器可能会读取它们,但代理不会。因此,它们是无效的,您应该将缓存指令作为真正的 HTTP 标头发送。
回答by mahen3d
In the beginning of code you need to use this:
在代码的开头,你需要使用这个:
<!DOCTYPE html>
<html manifest="cache.manifest">
...
...
Then create cache.manifest file with content of what you want to cache i.e
然后用你想要缓存的内容创建 cache.manifest 文件,即
CACHE MANIFEST
# 2010-06-18:v2
# Explicitly cached 'master entries'.
CACHE:
/favicon.ico
index.html
stylesheet.css
images/logo.png
scripts/main.js
# Resources that require the user to be online.
NETWORK:
*
# static.html will be served if main.py is inaccessible
# offline.jpg will be served in place of all images in images/large/
# offline.html will be served in place of all other .html files
FALLBACK:
/main.py /static.html
images/large/ images/offline.jpg
A manifest can have three distinct sections: CACHE, NETWORK, and FALLBACK.
清单可以包含三个不同的部分:缓存、网络和回退。
CACHE:This is the default section for entries. Files listed under this header (or immediately after the CACHE MANIFEST) will be explicitly cached after they're downloaded for the first time.
CACHE:这是条目的默认部分。在此标题下(或紧接在 CACHE MANIFEST 之后)列出的文件在第一次下载后将被显式缓存。
NETWORK:Files listed in this section may come from the network if they aren't in the cache, otherwise the network isn't used, even if the user is online. You can white-list specific URLs here, or simply "", which allows all URLs. Most sites need "".
网络:本节中列出的文件如果不在缓存中,则可能来自网络,否则即使用户在线,也不会使用网络。您可以在此处将特定 URL 列入白名单,或者只是“ ”,它允许所有 URL。大多数网站都需要“”。
FALLBACK:An optional section specifying fallback pages if a resource is inaccessible. The first URI is the resource, the second is the fallback used if the network request fails or errors. Both URIs must from the same origin as the manifest file. You can capture specific URLs but also URL prefixes. "images/large/" will capture failures from URLs such as "images/large/whatever/img.jpg".
FALLBACK:如果资源不可访问,则指定回退页面的可选部分。第一个 URI 是资源,第二个 URI 是网络请求失败或错误时使用的回退。两个 URI 必须与清单文件来自同一来源。您可以捕获特定的 URL,也可以捕获 URL 前缀。“images/large/”将从诸如“images/large/whatever/img.jpg”之类的 URL 中捕获失败。
回答by jonasjacek
There is no HTML solution. Mozilla's application cache (cache.manifest) is deprecated. The application cache site says:
没有 HTML 解决方案。Mozilla 的应用程序缓存 (cache.manifest) 已弃用。应用程序缓存站点说:
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible. ... Use Service Workersinstead.
此功能已从 Web 标准中删除。虽然一些浏览器可能仍然支持它,但它正在被删除。如果可能,请避免使用它并更新现有代码。...使用服务人员代替。
Apart from that, I suggest you use HTTP Cache-Controlto solve cache issues.
除此之外,我建议您使用HTTP Cache-Control来解决缓存问题。
回答by Stu Cox
There isn't an HTML solution, because it's not a markup problem. Caching is an action on the resource, not part of the resource definition itself.
没有 HTML 解决方案,因为它不是标记问题。缓存是对资源的操作,而不是资源定义本身的一部分。
As others have said, HTTP headers are the best way to control caches, because these are observed by allcaches - <meta>
tags are only observed by browser caches. These should be set by your server / web framework.
正如其他人所说,HTTP 标头是控制缓存的最佳方式,因为所有缓存都会观察到这些标头-<meta>
标记仅由浏览器缓存观察到。这些应该由您的服务器/网络框架设置。
That said, I wouldn't be surprised if browsers still observe <meta http-equiv="cache-control" content="no-cache">
for pages with the HTML5 doctype.
也就是说,如果浏览器仍然观察<meta http-equiv="cache-control" content="no-cache">
带有 HTML5 文档类型的页面,我不会感到惊讶。