【问题标题】:Does the service worker cache support cache-control headers?Service Worker 缓存是否支持缓存控制标头?
【发布时间】:2016-02-02 19:55:32
【问题描述】:

Service Worker 缓存是否支持缓存控制标头?例如,如果缓存中的条目具有标头cache-control: no-storecache-control: max-age=60match() 是否尊重这些标头?

以下代码输出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; })
  );
});

【问题讨论】:

    标签: javascript cache-control service-worker


    【解决方案1】:

    Service Worker 缓存的行为与标准 RFC-compliant HTTP cache 不同。特别是,它会忽略所有与“新鲜度”相关的标头(例如cache-control)。但是请注意,对于 vary 标头,它的行为确实符合预期。 (请参阅规范中的cache resolution algorithm。)

    如果您想要符合 HTTP 的缓存行为,您需要将其置于现有缓存功能之上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2018-05-14
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多