Chaining of edge func gives 502

I try to implement chaining pattern when one function is calling another, and that another one in this way

await fetch(req.url.replace('day', 'week'))

https://ME.netlify.app/getday?day=2022-07-10 to https://ME.netlify.app/getweek?day=2022-07-10

it works fine on local “netlify dev” but after deploy its generate 502 error:

 Response {
  body: ReadableStream { locked: true },
  bodyUsed: true,
  headers: Headers {
  "content-length": "62",
  "content-type": "text/plain; charset=utf-8",
  date: "Wed, 13 Jul 2022 20:36:32 GMT",
  server: "Netlify",
  "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
  "x-nf-request-id": "01G7WME3PH1VXFKZEPFHGR6QXB"
},
  ok: false,
  redirected: false,
  status: 502,
  statusText: "Bad Gateway",
  url: "https://ME.netlify.app/getweek?day=2022-07-10"
}

how to fix this?

Hi Dan, and welcome to the Forums!

Using fetch for calling another edge function results in an endless loop, because the same edge function that’s making the fetch request will be invoked again as part of the downstream request.

If you want to chain edge functions together, I recommend you take a look at context.next (see Edge Functions API | Netlify Docs).
If your goal is to share code between functions, it might be easier to share them on a code level, and import shared logic in all edge functions where it’s used.

Let me know if that helped!

Best Regards
Simon

1 Like