From e56c5d9a9c23088b1b4177ac94b68f125e1d40b7 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 13 May 2021 01:05:34 -0700 Subject: [PATCH] Use stale-while-revalidate for /api/validPetPoses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Idk I was surprised to notice that /api/validPetPoses wasn't cached by the Vercel CDN… but I guess that makes sense, because it's only allowed to be an hour old, and client caches hold onto it, so I suppose it makes sense for the CDN cache to not bother. But in practice, that means users loading the site are pretty _likely_ to see /api/validPetPoses taking its full load time, which can be up to 500ms. So, I'm offering this additional cache hint, to be willing to serve /api/validPetPose responses up to a week old while reloading the latest data. My hope is that the cache algorithm is much more excited about holding onto that, and that it makes the 500ms delay very _rare_ in practice! --- api/validPetPoses.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/validPetPoses.js b/api/validPetPoses.js index 04ea0fa..df1087c 100644 --- a/api/validPetPoses.js +++ b/api/validPetPoses.js @@ -94,9 +94,9 @@ async function getDistinctPetStates(db) { async function handle(req, res) { const buffer = await getValidPetPoses(); - // Cache for 1 hour. This will also cache at Vercel's CDN, so the function - // shouldn't even get run very often at all! - res.setHeader("Cache-Control", "max-age=3600"); + // Cache for 1 hour, and allow the CDN cache to serve copies up to an + // additional week older while re-fetching in the background. + res.setHeader("Cache-Control", "max-age=3600, stale-while-revalidate=604800"); res.status(200).send(buffer); }