]> git.ardis.lu - ardislu.dev/commitdiff
Clean origin response to allow custom cache logic
authorArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 02:43:01 +0000 (19:43 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 02:43:01 +0000 (19:43 -0700)
functions/api.js

index a9c4e616c2ada44286cdbbe5a6c8978bab0abc7f..dfbbff43baa8202290255d170b0ea5c7d9ff7611 100644 (file)
@@ -10,7 +10,9 @@ export async function onRequestGet({ request, env, waitUntil }) {
   }
 
   const token = await getGoogleAuthToken(env.EMAIL, env.PRIVATE_KEY, 'https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/documents.readonly');
-  const responsePromise = fetch(queryUrl, { headers: { Authorization: `Bearer ${token}` } });
+  const responsePromise = fetch(queryUrl, { headers: { Authorization: `Bearer ${token}` } })
+    .then(r => r.blob())
+    .then(b => new Response(b, { headers: { 'Content-Type': 'application/json' } })); // Remove extra response headers which may interfere with cache
   const cachedResponse = await caches.default.match(queryUrl);
   if (cachedResponse) {
     waitUntil(responsePromise.then(response => caches.default.put(queryUrl, response)));
This page took 0.036146 seconds and 4 git commands to generate.