# songwhip-cache-proxy

A Cloudflare Worker proxy service that implements [Stale-While-Revalidate](https://vercel.com/docs/concepts/edge-network/caching#stale-while-revalidate).

## Motivation

This is the kind of functionality you'd expect from Cloudflare or Vercel but practice Cloudflare doesn't implement SWR and Vercel seems to drop cached content really quickly. This version of SWR honours the TTLs defined in the `Cache-Control` header.

## Where does Songwhip use it?

`songwhip-api` uses it to fetch data from `songwhip-analytics` where queries can be slow. We serve 'stale' content from `songwhip-cache-proxy` layer and indicate staleness with `x-edge-cache-status: REVALIDATING` header, meaning the client can attempt to fetch again after a few seconds if it wants the 'revalidated' content.

## Deployment

- Uses `wrangler` to deploy to Cloudflare Workers
- Deploys using Github Actions when pull-requests are merged into `master`
- Currently don't have a `staging` environment, so you'll need to use `wrangler` locally to test changes before deploying to `production`.
