]> git.ardis.lu - ardislu.dev/commitdiff
Avoid blocking for auth token
authorArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 04:30:05 +0000 (21:30 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 13 Jun 2024 04:30:05 +0000 (21:30 -0700)
The auth token is not required if responding with a cached response, so don't wait for it.

functions/api.js

index dfbbff43baa8202290255d170b0ea5c7d9ff7611..370ac99be8897ae068d1f9b3dae7dd3ea1fa86ff 100644 (file)
@@ -9,8 +9,8 @@ export async function onRequestGet({ request, env, waitUntil }) {
     return new Response('Invalid query URL. Query URL hostname must be "sheets.googleapis.com" or "docs.googleapis.com".', { status: 400 });
   }
 
-  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 = 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
   const cachedResponse = await caches.default.match(queryUrl);
This page took 0.039541 seconds and 4 git commands to generate.