Serve ultra-fast, globally available sites or apps by render-caching and asset-proxying on the edge
If you want to serve ultra-fast, globally available sites or apps you’ve found the right place. This repo gives you the basic setup to do “render caching”—that is, fetching HTML from a requested endpoint, caching it, and returning the cached content to anyone requesting the same URL. The magic of Cloudflare is that their Workers and KV edge-side key-value store is extremely fast. I’m personally seeing server-side rendered sites go from 300-500 ms to around 30ms.
You should add more logic around validating request headers and content types and any other production-readiness stuff before going too far with this, I suppose.
The original context for this was (as alluded to above) to render cache SSR applications, so there’s also an asset proxy in this repo.
https://my-amazing-site.dev/something
ENDPOINT
environment variable)something
is cached; if it’s not it will go to the origin and cache it; if it’s cached, return the cached contenthttps://my-amazing-site.dev/something
https://my-amazing-site.dev/assets/image.jpg
, the asset proxy edge worker will trigger (the exact path is set in assetProxy/wrangler.toml
and the route
field)https://awesome-cloud-provider.net/my-bucket-123/assets/image.jpg
because you specified https://awesome-cloud-provider.net/my-bucket-123/
in assetProxy/wrangler.toml
and the STORAGE_BUCKET_URL
variable)Certainly, though this repo does not currently do so.
Since this uses Cloudflare Workers and KV, you need to have a Cloudflare account. Workers and KV are available even in their free account, so go get one if you don’t have one already.
I assume you have Wrangler installed.
You will need to provide both an endpoint URL (the site that you are going to cache), and an assets storage bucket path for the asset proxying.
Make sure to rename/set all the empty/dummy fields in wrangler.toml
files to your own values.
Run wrangler dev
in the respective subfolders. Go to http://127.0.0.1:8787
and whatever sub-route you need to start testing your work!
Standard way: Run wrangler publish
in the respective subfolders.
To deploy both, I’ve provided a tiny helper script called deploy.sh
to deploy them for you.
You can also just copy-paste copy into the online editor.