Why do we fetch?


I discovered Cloudflare workers, looking for a way to redirect my users depending on their country, if they don’t have a pre-existing cookie. I previously did this directly from my PHP script.

This means that a french user going to mywebsite.com, would:

  1. Load the page.
  2. Get redirected to fr.mywebsite.com.
  3. Load again fr.mywebsite.com.

I thought workers would prevent the first load, and redirect instantly to fr.mywebsite.com without going to my server.

What I’m having a hard time understanding from this example is why do we have to fetch the page anyway? Is fetch actually loading the page, or just looking if it returns a 200 status code?

On a side note, I think there’s an error on Random Content Cookies example: it should probably return modifiedResponse.

You don’t need to do a fetch to do a redirect, this would work just fine:

addEventListener('fetch', event => {

async function Redirect(request) {

    return new Response('', {
        status: 302,
        headers: {
          'Location': 'https://mynewdomain.com'
1 Like