Alter headers example in ES module mode in worker document does not work

Hello,

I was trying to run the ‘alter headers’ example in ES module mode that I found in worker document. However, it does not work and gives me the following error message:

’ Error

Subrequest depth limit exceeded. This request recursed through Workers too many times. This can happen e.g. if you have a Worker or Durable Object that calls other Workers or objects recursively. Workers and objects can recurse up to 16 times. If you’re trying to fetch from an origin server, make sure you’ve set the upstream option.’

The problem seems to be that exported async fetch function recursively calls itself, as shown in the following lines:

export default {
async fetch(request) {
const response = await fetch(request);

}

I run the example by the latest Wrangler, and debugged it on my local machine.

Would you please give me your opinion and solution for this problem.

Best regards,

David

A Worker that fetches itself will go to the origin for that DNS record - it won’t recursively call the Worker.

The issue is likely just a bad redirect from the origin - i.e if you’re using Flexible SSL mode.

Hi,

Thanks for your reply. I didn’t use flexible SSL or anything to redirect the call. What I did are creating a worker project in local environment with the latest Wrangler, copying the ‘alter headers’ example to the index.js, and debugging it on local machine 127.0.0.1. Other examples are fine with this approach. Do you have any idea to fix the problem?

Thanks