From 5cb9fef9a714c117e5e4b81843f5632ef7c58013 Mon Sep 17 00:00:00 2001 From: Joe Ma Date: Thu, 13 Oct 2022 01:31:43 +0800 Subject: [PATCH] Add cache-control directive to frequently requested content Remove redundant header from upstream response Signed-off-by: Joe Ma --- src/index.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 87d83f7..209b704 100644 --- a/src/index.ts +++ b/src/index.ts @@ -116,7 +116,11 @@ export default { } if (path === '/api' && method == 'GET') { - return new Response(API_SPEC_TEXT); + return new Response(API_SPEC_TEXT, { + headers: { + 'cache-control': 'public, max-age=172800', + }, + }); } if (path === '/') { @@ -128,12 +132,13 @@ export default { cacheEverything: true, }, }).then(value => { - let res = new Response(value.body, value); - // Add the correct content-type to response header - res.headers.set('content-type', 'text/html; charset=UTF-8;'); - // Remove the default CSP header - res.headers.delete('content-security-policy'); - return res; + return new Response(value.body, { + // Add the correct content-type to response header + headers: { + 'content-type': 'text/html; charset=UTF-8;', + 'cache-control': 'public, max-age=172800', + }, + }); }); } @@ -389,7 +394,7 @@ export default { method: 'GET', }); - res = new Response(origin.body, origin); + res = new Response(origin.body); if (res.status == 404) { // UUID exists in index but not found in remote object storage service, probably expired @@ -407,13 +412,6 @@ export default { }); } - // Remove x-amz-* headers - for (let [key, value] of res.headers.entries()) { - if (key.startsWith('x-amz')) { - res.headers.delete(key); - } - } - res.headers.set('cache-control', 'public, max-age=18000'); res.headers.set('content-disposition', `inline; filename="${encodeURIComponent(descriptor.title ?? uuid)}"`);