Trouble issuing a redirect behind worker function

Hi!

We use a cloudflare worker to distribute traffic between a marketing site hosted using a CMS and an app server which runs a ticketing system, so both are under the same domain.

I have just implemented a contact form, which is hosted on the app server, and when it posts to the server the request is processed just fine, but the last thing the app server does is issue a redirect. The form posts to /site//contact and the server after processing the request redirects to /site//contact/received and this for some reason fails in the worker, and i can’t get an error message. I have added a tryIcatch as described in here https://developers.cloudflare.com/workers/archive/writing-workers/debugging-tips/

But it doesn’t seemt like it event gets hit. In the network tab of Chrome Dev Tools it appears as though it is just the post request to the server that ends in a 500 error, but there aren’t any errors in the app server logs which processes the request and i do receive the message in the form.

The worker we use is this one. Hope anyone can see if i’m mishandling the redirect or something.

Thanks to anyone for taking a look at this!

const appPrefixes = [
‘/api’,
‘/backstage’,
‘/site’,
‘/ticket’,
‘/login’,
‘/static’,
‘/oval-office’,
‘/.well-known’
];

function createPath(pathname) {
return pathname;
}

function createForwardUrl(url, isAppRequest) {
const host = isAppRequest ? ‘’ : ‘’;
return ${host}${createPath(url.pathname)}${url.search || ''};
}

async function createForwardRequestOptions(request) {
const headers = {
‘User-Agent’: ‘ticketcase.dk’,
‘Accept’: ‘/
}

let body = request.body;
return {
  method: request.method,
  headers: Object.assign(request.headers, headers),
  body: body
}

}

async function forward(request)
{
const url = new URL(request.url);

const isAppRequest = appPrefixes.findIndex(prefix => url.pathname.startsWith(prefix)) > -1;
const forwardUrl = createForwardUrl(url, isAppRequest);
const options = await createForwardRequestOptions(request);

return await fetch(forwardUrl, options);
}

/**

  • Respond to the request
  • @param {Request} request
    */
    async function handleRequest(event) {
    try {
    return forward(event.request);
    } catch (e) {
    return new Response(e.stack || e);
    }
    }

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event));
});

If you’re able to wrap your code as ‘preformatted text’ it will make it much easier to read. Do you think you could do that?

One other thing that could help is running this with the new workers dev feature to see errors locally as you test it, or using the new workers tail feature to get log messages as you hit it in production.