To serve images from a custom domain:
Log in to the Cloudflare dashboard ↗ .
Select your account > select Workers & Pages .
Select Create application > Workers > Create Worker and create your Worker.
In your Worker, select Quick edit and paste the following code.
// You can find this in the dashboard, it should look something like this: ZWd9g1K7eljCn_KDTu_MWA
const { pathname } = new URL ( request . url ) ;
// A request to something like cdn.example.com/83eb7b2-5392-4565-b69e-aff66acddd00/public
// will fetch "https://imagedelivery.net/<accountHash>/83eb7b2-5392-4565-b69e-aff66acddd00/public"
return fetch ( `https://imagedelivery.net/ ${ accountHash }${ pathname } ` ) ;
async fetch ( request ) : Promise < Response > {
// You can find this in the dashboard, it should look something like this: ZWd9g1K7eljCn_KDTu_MWA
const { pathname } = new URL ( request . url ) ;
// A request to something like cdn.example.com/83eb7b2-5392-4565-b69e-aff66acddd00/public
// will fetch "https://imagedelivery.net/<accountHash>/83eb7b2-5392-4565-b69e-aff66acddd00/public"
return fetch ( `https://imagedelivery.net/ ${ accountHash }${ pathname } ` ) ;
} satisfies ExportedHandler ;
from js import URL , fetch
async def on_fetch ( request ):
# You can find this in the dashboard, it should look something like this: ZWd9g1K7eljCn_KDTu_MWA
url = URL . new ( request . url )
# A request to something like cdn.example.com/83eb7b2-5392-4565-b69e-aff66acddd00/public
# will fetch "https://imagedelivery.net/<accountHash>/83eb7b2-5392-4565-b69e-aff66acddd00/public"
return fetch ( f 'https://imagedelivery.net/ { account_hash }{ url . pathname } ' )
Another way you can serve images from a custom domain is by using the cdn-cgi/imagedelivery
prefix path which is used as path to trigger cdn-cgi
image proxy.
Below is an example showing the hostname as a Cloudflare proxied domain under the same account as the Image, followed with the prefix path and the image <ACCOUNT_HASH>
, <IMAGE_ID>
and <VARIANT_NAME>
which can be found in the Images on the Cloudflare dashboard.
https : //example.com/cdn-cgi/imagedelivery/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>