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')) to

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: ""

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 (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

1 Like