Skip to content
forked from kotx/render

Cloudflare Worker to proxy and cache requests to R2

License

Notifications You must be signed in to change notification settings

LestherSK/render

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Render

Proxies readonly requests to Cloudflare R2 via Cloudflare Workers.

Features

  • Handles HEAD, GET, and OPTIONS 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
  • Handles precondition headers (if-modified-since, if-unmodified-since, if-match, if-none-match)

Setup

Installing wrangler

npm i -g wrangler
wrangler login

Configuration

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.

Deploying

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.

Method 1 (Local)

wrangler publish # or `npm run deploy`

Method 2 (GitHub Actions)

  1. Fork this repository
  2. Create and set the R2 bucket names in wrangler.toml
  3. Set the secrets CF_API_TOKEN and CF_ACCOUNT_ID in settings
  4. Enable workflows in the Actions tab
  5. Profit

Development

Install deps:

npm install

To launch the development server:

npm run dev

About

Cloudflare Worker to proxy and cache requests to R2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%