Request: cache 属性
Baseline 广泛可用 *
注意:此功能在 Web Workers 中可用。
Request 接口的只读属性 cache 包含请求的缓存模式。它控制着请求如何与浏览器的 HTTP 缓存 进行交互。
值
一个 RequestCache 值。可用值为:
-
default— 浏览器在其 HTTP 缓存中查找匹配的请求。 -
no-store— 浏览器会从远程服务器获取资源,而不会先查找缓存,并且不会使用下载的资源更新缓存。 -
reload— 浏览器会从远程服务器获取资源,而不会先查找缓存,但之后会使用下载的资源更新缓存。 -
no-cache— 浏览器在其 HTTP 缓存中查找匹配的请求。- 如果找到匹配项,无论新鲜或过期,浏览器都会向远程服务器发送一个条件请求。如果服务器指示资源未更改,则会从缓存中返回。否则,将从服务器下载资源,并更新缓存。
- 如果没有找到匹配项,浏览器将发送一个常规请求,并使用下载的资源更新缓存。
-
force-cache— 浏览器在其 HTTP 缓存中查找匹配的请求。- 如果找到匹配项,无论新鲜或过期,则会从缓存中返回。
- 如果没有找到匹配项,浏览器将发送一个常规请求,并使用下载的资源更新缓存。
-
only-if-cached— 浏览器在其 HTTP 缓存中查找匹配的请求。 实验性- 如果找到匹配项,无论新鲜或过期,则会从缓存中返回。
- 如果没有找到匹配项,浏览器将以 504 网关超时 状态进行响应。
仅当请求的
mode为"same-origin"时,才能使用"only-if-cached"模式。如果请求的redirect属性为"follow"并且重定向不违反"same-origin"模式,则会遵循缓存的重定向。
示例
js
// Download a resource with cache busting, to bypass the cache
// completely.
fetch("some.json", { cache: "no-store" }).then((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((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((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((response) => {
/* consume the response */
});
// Naive stale-while-revalidate client-level implementation.
// Prefer a cached albeit stale response; but update if it's too old.
// AbortController and signal to allow better memory cleaning.
// In reality; this would be a function that takes a path and a
// reference to the controller since it would need to change the value
let controller = new AbortController();
fetch("some.json", {
cache: "only-if-cached",
mode: "same-origin",
signal: controller.signal,
})
.catch((e) =>
e instanceof TypeError && e.message === "Failed to fetch"
? { status: 504 } // Workaround for chrome; which fails with a TypeError
: Promise.reject(e),
)
.then((res) => {
if (res.status === 504) {
controller.abort();
controller = new AbortController();
return fetch("some.json", {
cache: "force-cache",
mode: "same-origin",
signal: controller.signal,
});
}
const date = res.headers.get("date"),
dt = date ? new Date(date).getTime() : 0;
if (dt < Date.now() - 86_400_000) {
// if older than 24 hours
controller.abort();
controller = new AbortController();
return fetch("some.json", {
cache: "reload",
mode: "same-origin",
signal: controller.signal,
});
}
// Other possible conditions
if (dt < Date.now() - 300_000)
// If it's older than 5 minutes
fetch("some.json", { cache: "no-cache", mode: "same-origin" }); // no cancellation or return value.
return res;
})
.then((response) => {
/* consume the (possibly stale) response */
})
.catch((error) => {
/* Can be an AbortError/DOMException or a TypeError */
});
规范
| 规范 |
|---|
| Fetch # ref-for-dom-request-cache② |
浏览器兼容性
加载中…