Netlify Edge Handlers

Some very cool news from Netlify: Edge Handlers are in Early Access (request it here). I think these couple of lines of code do a great job in explaining what an Edge Handler is:

export function onRequest(event) {
  console.log(`Incoming request for ${event.request.url}`);
  event.replaceResponse(() => fetch("https://www.netlify.com/"));
}

So that’s a little bitty bit of JavaScript that runs at “the edge” (at the CDN level) for every request through your site. In the case above, I’m totally replacing the response with an Ajax request for another URL. Weird! But cool. This has incredible power. I can replace the response with a manipulated response (it could be just a small change). Say, change the headers. Or check who the logged-in user is, make a request for data on their behalf, and inject that data into the response. 🤯.

So you might think of Jamstack as either pre-render or get data client-side. This is opening up a new door: build your response dynamically at the edge.

What’s nice about the Netlify approach is that the code that runs these sits right alongside the rest of your code in the repo itself, just like functions.