This Cloudflare worker is designed to run in front of your website, and it will optimize all of your site's images seamlessly on the fly, including:
- Scaling images down based on device type (desktop, tablet and mobile)
- Serving images in modern formats such as AVIF and WEBP when supported by the user's browser
- Enabling you generate alternate video formats for HTML5
- If your website already uses any WEBP or AVIF images, it will downgrade these to PNG when not supported by the browser
- All images are also optimized for filesize by adjusting quality automatically where possible
This project requires:
- A Cloudflare account (you can use their free plan!), and you'll need to have a website setup
- A Cloudinary account (again you can use their free plan!)
- Clone the GitHub repository
- Create a new worker at Cloudflare, choose
HTTP Handler
as the type - Replace values in
wrangler.toml
including: name, ORIGIN, WORKER_HOST, CLOUDINARY_CLOUD - Run
yarn
to install dependencies - Run
yarn deploy
to deploy the worker to Cloudflare, this uses thename
field inwrangler.toml
to find your worker, and Cloudflare will require you to login- The first time you do this Cloudflare will launch your browser to let you authenticate
- Run
yarn tail
to see live logs from the worker - You should now be able to test your worker at a url like https://your-worker.your-domain.workers.dev
To use this in production you'd need to add a Worker route
in your website at Cloudflare, and bind it to the worker. You'd then need to modify the values in worker.toml
This is a standard file for Cloudflare workers, where you specify configuration values and environment variables.
name
This is the name of the Cloudflare worker you created on your Cloudflare dashboardCLOUDINARY_CLOUD
This is the id of your cloud environment at Cloudinary, its likely a 10 character alphanumeric stringORIGIN
is only for use in testing (not production), it should be your host name likegoogle.com
WORKER_HOST
is only for use in testing (not production), it should be your worker's hostname, likely a value likeyour-worker.your-domain.workers.dev
- This project relies on your website's images having file extensions (eg gif, jpg, png) so it can identify the type of image. If your images don't have extensions, then they won't be transformed at all.