2016-02-02 80 views
2

服务工作者缓存是否支持缓存控制头?例如,如果高速缓存中的条目具有标头cache-control: no-storecache-control: max-age=60,则match()是否遵守这些标头?服务工作者缓存是否支持缓存控制头文件?

尽管报头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; }) 
); 
}); 

回答

5

服务工作者高速缓冲存储器并不像一个标准RFC-compliant HTTP cache。特别是,它会忽略与“新鲜度”相关的所有标题(例如cache-control)。但请注意,它的行为与vary标题相同。 (请参阅规格中的cache resolution algorithm

如果您需要符合HTTP的缓存行为,则需要在现有缓存​​功能的顶部对此进行分层。