服务辅助角色缓存是否支持缓存控制标头



服务工作进程缓存是否支持缓存控制标头?例如,如果缓存中的条目具有标头cache-control: no-storecache-control: max-age=60这些标头是否受到match()的尊重?

以下代码输出CACHE HIT尽管标头cache-control: no-store出现在响应中。(我认为同样的问题也适用于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; })
  );
});

服务工作进程缓存的行为与符合 RFC 的标准 HTTP 缓存不同。特别是,它会忽略与"新鲜度"相关的所有标头(例如cache-control)。但请注意,它确实与vary标头相关的预期行为。(请参阅规范中的缓存解析算法。

如果你想要符合HTTP的缓存行为,你需要在现有缓存的功能之上分层。

最新更新