Caching in Netlify edge functions using Hono

Alright, I was able to figure out the solution to my problem. It consisted of two steps:

  • I had to set the cache to "manual" for the Netlify edge function:
export const config: Config = {
	path: ["/*"],
	cache: "manual",
};
  • I had to strip the response headers from the previous response and add the cache headers. Somehow Netlify wasn’t able to cache the response with the original headers:
const headers = new Headers({
	"Content-Type": "application/json",
	"Netlify-CDN-Cache-Control": "public, max-age=1800, must-revalidate",
	"CDN-Cache-Control": "public, max-age=1800, must-revalidate",
	"Cache-Control": "public, max-age=1800, must-revalidate",
});

return new Response(response.body, { ...response, headers });