Image resize with Cloudflare Worker

Hello, I’ve problems with image resizing. I set up a simple worker which has a direct test-route to one of my images like example.com/image1.jpg

The domain is on a Business plan.

When I open the URL in workers playground I get the response:

ERROR 9504: Could not fetch the image - request timed out

Open in normal browser also times out.

My code is:

async function handleRequest(request) {
  const url = new URL(request.url)
  const the_img = await fetch(url.href, {
    cf: {
      image: {
        width: 150,
        quality: 80,
        format: 'jpeg'
      }
    }});

  return new Response(the_img.body, {
    headers: {
      'content-type': 'image/jpeg',
    },
  });
}

addEventListener('fetch', event => {
  return event.respondWith(handleRequest(event.request));
});

Someone can give me a hint what’s wrong with my code?
Thanks!

Was a request loop problem. This worked for me:

...

    if (/image-resizing/.test(event.request.headers.get("via"))) {
      return fetch(event.request)
    }
    else{
      return event.respondWith(handleRequest(event));
    }

I think this might be caused by not excluding the path for Worker on the image itself…

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.