Use stale-while-revalidate for /api/validPetPoses

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!
This commit is contained in:
Emi Matchu 2021-05-13 01:05:34 -07:00
parent c8feb9a7e0
commit e56c5d9a9c

View file

@ -94,9 +94,9 @@ async function getDistinctPetStates(db) {
async function handle(req, res) { async function handle(req, res) {
const buffer = await getValidPetPoses(); const buffer = await getValidPetPoses();
// Cache for 1 hour. This will also cache at Vercel's CDN, so the function // Cache for 1 hour, and allow the CDN cache to serve copies up to an
// shouldn't even get run very often at all! // additional week older while re-fetching in the background.
res.setHeader("Cache-Control", "max-age=3600"); res.setHeader("Cache-Control", "max-age=3600, stale-while-revalidate=604800");
res.status(200).send(buffer); res.status(200).send(buffer);
} }