Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • Code Replica
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • Adaptive Image Format Conversion via Edge Functions
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

Image Adaptive WebP

This example checks the Accept header in the request. If it contains "image/webp", the edge function automatically converts the image format to WebP and caches it on the EdgeOne edge node. If your web application displays a large number of PNG and JPEG images and you want to automatically optimize images at the edge to reduce bandwidth costs, you can use edge functions to achieve a smooth upgrade, automatically converting PNG and JPEG images to WebP format with zero changes to your business logic. For more information on image format conversion, please refer to ImageProperties.
Note:
The conversion feature in this example can be executed normally only when the MIME type specified by the Content-Type response header of the source file is an image (image/*).
This example does not currently support converting SVG images.
It is recommended that image file extensions such as .png, .jpeg, and .jpg be added to the edge function trigger rule configuration.
async function handleEvent(event) {
const { request } = event;
// Obtain the image types supported by the client
const accept = request.headers.get('Accept');
const option = { eo: { image: {} } };
// Check whether the client supports WebP images. If not, respond with the original image.
if (accept && accept.includes('image/webp')) {
option.eo.image.format = 'webp';
}
const response = await fetch(request, option);
return response;
}

addEventListener('fetch', event => {
// When the function code throws an unhandled exception, the edge function forwards this request back to the origin server.
event.passThroughOnException();
event.respondWith(handleEvent(event));
});

Sample Preview

Enter a URL that matches the edge function trigger rule in the address bar of a PC or mobile browser (for example, https://example.com/images-format/ef-1.jpeg), and the image is automatically converted to WebP format.


Related References