--- title: Request.cache slug: Web/API/Request/cache translation_of: Web/API/Request/cache ---
cache
作为{{domxref("Request")}} 接口只读属性包含着请求的缓存模式。它控制着请求以何种方式与浏览器的 HTTP 缓存进行交互。
var currentCacheMode = request.cache;
A RequestCache
value. The available values are:
default
— 浏览器从HTTP缓存中寻找匹配的请求。
no-store
— 浏览器直接从远程服务器获取资源,不查看缓存,并且不会使用下载的资源更新缓存。reload
— 浏览器直接从远程服务器获取资源,不查看缓存,然后使用下载的资源更新缓存。no-cache
— 浏览器在其HTTP缓存中寻找匹配的请求。
force-cache
— 浏览器在其HTTP缓存中寻找匹配的请求。
only-if-cached
— 浏览器在其HTTP缓存中寻找匹配的请求。
"only-if-cached"
mode can only be used if the request's mode
is "same-origin"
. Cached redirects will be followed if the request's redirect
property is "follow"
and the redirects do not violate the "same-origin"
mode.// Download a resource with cache busting, to bypass the cache // completely. fetch("some.json", {cache: "no-store"}) .then(function(response) { /* consume the response */ }); // Download a resource with cache busting, but update the HTTP // cache with the downloaded resource. fetch("some.json", {cache: "reload"}) .then(function(response) { /* consume the response */ }); // Download a resource with cache busting when dealing with a // properly configured server that will send the correct ETag // and Date headers and properly handle If-Modified-Since and // If-None-Match request headers, therefore we can rely on the // validation to guarantee a fresh response. fetch("some.json", {cache: "no-cache"}) .then(function(response) { /* consume the response */ }); // Download a resource with economics in mind! Prefer a cached // albeit stale response to conserve as much bandwidth as possible. fetch("some.json", {cache: "force-cache"}) .then(function(response) { /* consume the response */ });
Specification | Status | Comment |
---|---|---|
{{SpecName('Fetch','#dom-request-cache','cache')}} | {{Spec2('Fetch')}} | Initial definition |
{{Compat("api.Request.cache")}}