]> git.ardis.lu - ardislu.dev/commitdiff
Set edge cache TTL to 30 days
authorArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 07:32:43 +0000 (00:32 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 07:32:43 +0000 (00:32 -0700)
Default TTL is 2 hours. Confirmed that stale-while-revalidate logic is working.

functions/api.js

index 370ac99be8897ae068d1f9b3dae7dd3ea1fa86ff..b6e7630676c7a7be062bd0bc76fbb28b5261f464 100644 (file)
@@ -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)));
This page took 0.039961 seconds and 4 git commands to generate.