Skip to content

Commit

Permalink
Restrict cache-status hit to cases where the cache was actually hit (#30
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dontcallmedom authored Oct 23, 2023
1 parent 3fae3d2 commit 9691724
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions fetch-filecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ async function cacheFetch(url, options) {
}
}

async function readFromCache() {
async function readFromCache(hit = false) {
let data = await fs.promises.readFile(cacheHeadersFilename, 'utf8');
let headers = JSON.parse(data);
let status = headers.status || 200;
Expand All @@ -409,9 +409,11 @@ async function cacheFetch(url, options) {
return new Response(null, { url, status: 304, headers });
}
let readable = fs.createReadStream(cacheFilename);
// Indicate this is coming from the cache via
// https://www.rfc-editor.org/rfc/rfc9211.html
headers["cache-status"] = "fetch-filecache-for-crawling; hit";
if (hit) {
// Indicate this is coming from the cache via
// https://www.rfc-editor.org/rfc/rfc9211.html
headers["cache-status"] = "fetch-filecache-for-crawling; hit";
}
return new Response(readable, { url, status, headers });
}

Expand Down Expand Up @@ -532,7 +534,7 @@ async function cacheFetch(url, options) {
}
else {
resolvePendingFetch();
return readFromCache();
return readFromCache(true);
}
}
catch (err) {
Expand Down

0 comments on commit 9691724

Please sign in to comment.