服务工作者缓存是否支持缓存控制头?

mjs*_*mjs 2 javascript cache-control service-worker

服务工作者缓存是否支持缓存控制头?例如,如果缓存中的条目具有标题cache-control: no-store或者cache-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; })
  );
});
Run Code Online (Sandbox Code Playgroud)

mjs*_*mjs 5

服务工作者缓存的行为与标准的RFC兼容HTTP缓存不同.特别是,它忽略了与"新鲜度"相关的所有标题(例如cache-control).但请注意,它的表现与vary标题相同.(请参阅规范中的缓存分辨率算法.)

如果您需要符合HTTP的缓存行为,则需要在现有缓存​​的功能之上对其进行分层.