https://github.com/alula/cf-workers-media-proxy
A fast, simple image media proxy written in Rust that runs on Cloudflare Workers.
https://github.com/alula/cf-workers-media-proxy
Last synced: about 1 month ago
JSON representation
A fast, simple image media proxy written in Rust that runs on Cloudflare Workers.
- Host: GitHub
- URL: https://github.com/alula/cf-workers-media-proxy
- Owner: alula
- License: zlib
- Created: 2024-12-10T03:28:50.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-12-10T03:29:52.000Z (6 months ago)
- Last Synced: 2025-04-26T19:57:00.071Z (about 2 months ago)
- Language: Rust
- Size: 15.6 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cloudflare Workers Next.js Media Proxy
A fast, simple image media proxy written in Rust that runs on Cloudflare Workers.
## Features
- Supports passthrough of SVG images
- Supports PNG, JPEG, and WebP images
- Supports lossy and lossless WebP images
- Passes through ICC color profiles embedded inside images## Usage
To use the media proxy, make a request to the following endpoint:
```
/{base64url-encoded-url}?{query-parameters}
```### Supported Query Parameters
- `q`: Quality (0-100, default: 85)
- `w`: Max width (default: `DEFAULT_MAX_WIDTH`)
- `h`: Max height (default: `DEFAULT_MAX_HEIGHT`)
- `f`: Format (png, jpeg, webp)### Example
```sh
curl "https://your-cloudflare-worker-url/$(echo -n 'https://example.com/image.jpg' | basenc --base64url)?w=800&h=600&q=90&f=webp"
```## Integration with Next.js
To integrate the media proxy with Next.js, you must add a custom image loader that generates the correct URL for the media proxy.
Example implementation:
```javascript
'use client'
export default function cfWorkerImageLoader({ src, width, quality }) {
const base64Url = btoa(src).replace('+','-').replace('/','_').replace('=','');
return `https://your-cloudflare-worker-url/${base64Url}?w=${width}&f=webp${quality ? '&q=' + quality : ''}`;
}
```## Configuration
1. Copy `wrangler.toml.example` to `wrangler.toml`:
```sh
cp wrangler.toml.example wrangler.toml
```2. Set the allowed domains in `wrangler.toml`:
```toml
[vars]
DOMAIN_WHITELIST = "example.com,anotherdomain.com"
```3. Deploy the worker:
```sh
npx wrangler deploy
```## License
This project is licensed under the MIT License.