I try to fetch image from worker by cache.match() method, it hits the cache but always return 200, which makes the response time longer (about 200ms+).
I put request with headers, make sure If-Not-Match included in the headers, but it still return 200 rather then 304 not modified.
Now I have to compare the etag, return 304 if it is the same with the cache, it works. But it still take time for the cache.match() method, 200ms+ shown by browser.
Here is the code
const cache = caches.default;
var headers = new Headers(event.request.headers);
var newHeaders = new Headers();
newHeaders.append('if-none-match', headers.get('if-none-match'));
var request = new Request(url, { headers : newHeaders, method: 'GET' });
// from here response return 200, rather then 304, even the etag is the same with the browser
let response = await cache.match(request);
// here is how I put into cache
if(!response){
response = await fetch(url, {cf: {polish: "lossless"}});
let newRspHeaders= new Headers(response.headers);
const etag = newRspHeaders.get('x-bz-content-sha1') || newRspHeaders.get('x-bz-info-src_last_modified_millis') || newRspHeaders.get('x-bz-file-id');
newRspHeaders.set('ETag', etag);
// replace response headers
response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newRspHeaders
});
event.waitUntil(cache.put(url, response.clone()));
}
Any one can help? Thanks.