Proxies readonly requests to Cloudflare R2 via Cloudflare Workers.
Note: This worker might not conform to standards very well, but it should work in most cases... if you see a bug or something missing, please please please open an issue!
- Handles
HEAD
,GET
, andOPTIONS
requests - Forwards caching headers (
etag
,cache-control
,expires
,last-modified
) - Forwards content headers (
content-type
,content-encoding
,content-language
,content-disposition
) - Caches served files using the Cache API
- Ranged requests (
range
,if-range
, returnscontent-range
) - Handles precondition headers (
if-modified-since
,if-unmodified-since
,if-match
,if-none-match
) - Can serve an appended path if the requested url ends with / - Defaults to
index.html
in 0.5.0
npm i -g wrangler
wrangler login
Create your R2 bucket(s) if you haven't already (replace bucket_name
and preview_bucket_name
appropriately):
wrangler r2 bucket create bucket_name # required
wrangler r2 bucket create preview_bucket_name # optional
You can also do this from the Cloudflare dashboard.
Edit wrangler.toml
to have the correct bucket_name
and optionally, preview_bucket_name
(you can set it to bucket_name
) if you're going to run this locally.
You can do this from a fork, if using the GitHub Actions method.
You may edit CACHE_CONTROL
to the default cache-control
header or remove it entirely to fall back to nothing. If you set CACHE_CONTROL
to "no-store"
then Cloudflare caching will not be used.
Note: Due to how custom domains for workers work, you MUST use a route to take advantage of caching. Cloudflare may fix this soon. Also note that *.workers.dev domains do not cache responses. You MUST use a route to your own (sub)domain.
npm install
wrangler publish # or `npm run deploy`
- Fork this repository
- Set the secrets
CF_API_TOKEN
(withWorker Scripts: Edit
permissions) andCF_ACCOUNT_ID
in the repo settings - Enable workflows in the Actions tab
- Update
wrangler.toml
as needed (this will trigger the workflow) - (Optionally) set the worker route in the Cloudflare dashboard to use the Cache API
You may use this worker's functionality as a package by installing and importing render2
:
npm install render2
Usage:
import render from "render2";
render.fetch(req, env, ctx);
Install deps:
npm install
To launch the development server:
npm run dev