service worker缓存是否支持cache-controlheaders?
Does the service worker cache support cache-control headers?
Service Worker 缓存是否支持 cache-control headers?例如,如果缓存中的条目具有 headers cache-control: no-store
或 cache-control: max-age=60
,这些是否受到 match()
的尊重?
尽管 header cache-control: no-store
出现在响应中,但以下代码输出 CACHE HIT
。 (我认为同样的问题适用于 max-age
。)
function warm(c) {
var req = new Request("/foo.txt");
var res = new Response("hello", {
status: 200,
statusText: "OK",
headers: new Headers({
"cache-control": "no-store",
"content-type": "text/plain"
})
});
return c.put(req, res).then(function () { return c; });
}
function lookup(c) {
return c.match(new Request("/foo.txt")).then(function (r) {
return r ? "CACHE HIT" : "CACHE MISS";
});
}
function deleteAllCaches() {
return caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
return caches.delete(cacheName);
})
);
});
}
self.addEventListener('install', function (event) {
event.waitUntil(
deleteAllCaches()
.then(caches.open.bind(caches, 'MYCACHE'))
.then(warm)
.then(lookup)
.then(console.log.bind(console))
.then(function () { return true; })
);
});
Service Worker 缓存的行为与规范中的标准 RFC-compliant HTTP cache. In particular, it ignores all headers that relate to "freshness" (such as cache-control
). Note, however, that it does behave as expected with respect to the vary
header. (See the cache resolution algorithm 不同。)
如果您想要符合 HTTP 的缓存行为,您需要将其置于现有缓存功能之上。
Service Worker 缓存是否支持 cache-control headers?例如,如果缓存中的条目具有 headers cache-control: no-store
或 cache-control: max-age=60
,这些是否受到 match()
的尊重?
尽管 header cache-control: no-store
出现在响应中,但以下代码输出 CACHE HIT
。 (我认为同样的问题适用于 max-age
。)
function warm(c) {
var req = new Request("/foo.txt");
var res = new Response("hello", {
status: 200,
statusText: "OK",
headers: new Headers({
"cache-control": "no-store",
"content-type": "text/plain"
})
});
return c.put(req, res).then(function () { return c; });
}
function lookup(c) {
return c.match(new Request("/foo.txt")).then(function (r) {
return r ? "CACHE HIT" : "CACHE MISS";
});
}
function deleteAllCaches() {
return caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
return caches.delete(cacheName);
})
);
});
}
self.addEventListener('install', function (event) {
event.waitUntil(
deleteAllCaches()
.then(caches.open.bind(caches, 'MYCACHE'))
.then(warm)
.then(lookup)
.then(console.log.bind(console))
.then(function () { return true; })
);
});
Service Worker 缓存的行为与规范中的标准 RFC-compliant HTTP cache. In particular, it ignores all headers that relate to "freshness" (such as cache-control
). Note, however, that it does behave as expected with respect to the vary
header. (See the cache resolution algorithm 不同。)
如果您想要符合 HTTP 的缓存行为,您需要将其置于现有缓存功能之上。