Skip to content

Commit

Permalink
feat(cache): response GET cache-control by request
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei397 committed Apr 20, 2022
1 parent 516181c commit 308458f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ export async function checkEtag(context: Context, val: any) {
const etagOptions = { weak: true };
const actual = await calculate(str, etagOptions);
context.response.headers.set("etag", actual);
if (!context.response.headers.has("Cache-Control")) {
context.response.headers.set("Cache-Control", "no-cache");

// cache-control see https://cloud.tencent.com/developer/section/1189911
const requestCacheControl = context.request.headers.get("Cache-Control");
let responseCacheControl = context.response.headers.get("Cache-Control");
if (!responseCacheControl) {
if (requestCacheControl) {
const cacheArr = requestCacheControl.split(",");
const cacheResHeaders = [
"max-age",
"no-cache",
"no-store",
"no-transform",
];
responseCacheControl = cacheArr.filter((str) => {
const key = str.split("=")[0];
return cacheResHeaders.includes(key.trim().toLowerCase());
}).join(",");
} else {
responseCacheControl = "no-cache";
}
}

context.response.headers.set("Cache-Control", responseCacheControl);
if (
etag && !await ifNoneMatch(etag, str, etagOptions) // if etag is not match, then will return 200
) {
Expand Down

0 comments on commit 308458f

Please sign in to comment.