From e30fec3990e9cc8d9f02bc3114efd037cd95aabf Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Thu, 13 Jun 2024 00:32:43 -0700 Subject: [PATCH] Set edge cache TTL to 30 days Default TTL is 2 hours. Confirmed that stale-while-revalidate logic is working. --- functions/api.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/functions/api.js b/functions/api.js index 370ac99..b6e7630 100644 --- a/functions/api.js +++ b/functions/api.js @@ -12,7 +12,12 @@ export async function onRequestGet({ request, env, waitUntil }) { const responsePromise = getGoogleAuthToken(env.EMAIL, env.PRIVATE_KEY, 'https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/documents.readonly') .then(t => fetch(queryUrl, { headers: { Authorization: `Bearer ${t}` } })) .then(r => r.blob()) - .then(b => new Response(b, { headers: { 'Content-Type': 'application/json' } })); // Remove extra response headers which may interfere with cache + .then(b => new Response(b, { // Remove extra response headers which may interfere with cache + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'max-age=2592000' // = 60*60*24*30 (i.e., 30 days in seconds) + } + })); const cachedResponse = await caches.default.match(queryUrl); if (cachedResponse) { waitUntil(responsePromise.then(response => caches.default.put(queryUrl, response))); -- 2.47.3