Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict cache-status hit to cases where the cache was actually hit #30

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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