Allow edge function to finish before starting the next one in the chain

Hello,

I have an edge function authenticate.js that modifies the page and serves up different content if the user is authenticated versus when they’re not. This edge function runs on every route of my multi-page application (just static HTML files).

I then have another edge function, say, some-page.js which only runs on a specific page.

Because authenticate.js is alphabetically before some-page.js it runs first, and its code does this:

  // authenticate.js
  const response = await context.next();
  let page = await response.text();
  page = page.replace(/Authenticated/, "insert cool logged-in content here");

  // … more code …

  // … and finally …
  return new Response(page, response);

When await context.next() runs it immediately calls some-page.js edge function even though authenticate.js hasn’t finished running nor modified the page’s content nor returned yet.

How can I modify the page without immediately invoking the next edge function in the chain? I want all my middlewares to run one after the other, rather than interweaving in and out of one another.

And is there a way to pass the modifications of page from one edge function to the next so it can carry on the modifications from one edge function to the next?

Thanks!
Chris

You can declare the Edge Functions in netlify.toml to control the order of execution: Edge Functions declarations | Netlify Docs