Image Optimization
vinext uses Cloudflare Images for on-the-fly image resizing and format transcoding in production. Images are optimized at the edge with zero build-time processing.How It Works
When usingnext/image, vinext:
- Generates an optimization URL:
/_vinext/image?url=/photo.jpg&w=800&q=75 - Fetches the source image from the ASSETS binding
- Transforms via Cloudflare Images (resize, transcode, compress)
- Returns optimized image with
Cache-Control: public, max-age=31536000, immutable
Setup
Automatic Setup (vinext deploy)
vinext deploy configures everything automatically:
- ASSETS binding in
wrangler.jsonc - IMAGES binding in
wrangler.jsonc - Worker entry with image optimization handler
Manual Setup
1. Configure wrangler.jsonc
2. Create Worker Entry
API Reference
handleImageOptimization
Parameters
request (required)Type:
RequestIncoming image optimization request.
handlers (required)Type:
ImageHandlersCallbacks for fetching and transforming images.
handlers.fetchAssetFetch the source image. Typically delegates to
env.ASSETS.fetch().
handlers.transformImage (optional)Transform the image. Omit for passthrough (serves original).
Returns
Promise<Response> - Optimized image response with cache headers.
parseImageParams
Query Parameters
url (required)Type:
stringSource image path. Must be path-relative (starts with
/, not //).
w (required)Type:
numberTarget width in pixels.
0 = no resize.
q (optional)Type:
numberDefault:
75Quality (1-100).
Example
negotiateImageFormat
Accept header.
Returns
'image/avif'ifAcceptincludesimage/avif'image/webp'ifAcceptincludesimage/webp'image/jpeg'otherwise
Example
Constants
IMAGE_OPTIMIZATION_PATH
IMAGE_CACHE_CONTROL
Cache-Control header for optimized images. Optimized images are immutable because the URL encodes the transform params.
Usage in Components
Usenext/image normally:
Remote Images
Remote images work via @unpic/react which auto-detects 28 CDN providers:next.config.js:
Format Negotiation
The handler automatically serves the best format based on theAccept header:
- AVIF (smallest, newer browsers)
- WebP (smaller, wide support)
- JPEG (fallback)
Security
Path Validation
Only path-relative URLs are allowed. This prevents:- Open redirect:
?url=https://evil.com/phishing - SSRF:
?url=http://internal-service/admin - Protocol smuggling:
?url=data:image/svg+xml,...
?url=/photo.jpg?url=/images/profile.png
?url=https://example.com/photo.jpg?url=//example.com/photo.jpg?url=data:image/svg+xml,...
Width/Quality Limits
The handler validates:- Width: Must be
>= 0.0= no resize. - Quality: Must be
1-100.
400 Bad Request.
Fallback Behavior
IftransformImage is omitted or throws an error, the handler serves the original image with cache headers:
- Development server (no Images binding)
- Non-Cloudflare deployments
- Debugging
Development vs Production
Development
In dev mode, images are served as passthroughs (no optimization). ThetransformImage callback is omitted:
Production
In production on Workers, images are optimized via Cloudflare Images:Example: Custom Watermarking
Add custom image processing logic:Limitations
- No build-time optimization: Images are optimized at request time, not build time
- No static resizing:
next exportdoesn’t pre-generate responsive variants - No blur placeholders:
placeholder="blur"is not supported - No
sizesauto-detection: You must provide thesizesprop manually for responsive images
Related
- next/image - Image component reference
- Cloudflare Bindings - Access ASSETS, IMAGES, and other bindings
- Cloudflare Images - Official Cloudflare Images docs