Post-Cache Edge Functions
Post-cache edge functions run after the response is served from cache, allowing you to modify cached responses without invalidating the cache. This is useful for adding dynamic headers, analytics, or other post-processing to cached content.
To create a post-cache edge function, set cache = "manual" in the edge function configuration:
import type { Config, Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
const url = new URL(request.url);
// Get the response from the origin/cache
const response = await context.next();
// Modify the response
const modifiedResponse = new Response(response.body, response);
modifiedResponse.headers.set(
"X-Post-Cache-Processed",
new Date().toISOString()
);
return modifiedResponse;
};
export const config: Config = {
path: "/*",
cache: "manual",
};
Key Points
- Post-cache functions run after non-cached edge functions
- They can modify responses that have been served from cache
- The
cache = "manual"setting enables this behavior - Useful for adding analytics, dynamic headers, or response transformation
See this in action
Use the links below to see the post-cache edge function in action. Check the response headers in your browser's developer tools to see the X-Post-Cache-Processed header.
- View /hello without post-cache processing
- View /hello with post-cache edge function (check response headers)
- The Edge Function code: post-cache.ts
What are Edge Functions?
Using JavaScript and TypeScript, Netlify Edge Functions give you the power to modify network requests to localize content, serve relevant ads, authenticate visitors, A/B test content, and much more!
This all happens at the Edge — directly from the worldwide location closest to each user.
To use Edge Functions on Netlify, add JavaScript or TypeScript files to a
/netlify/edge-functionsdirectory in your project.
Deploy this site to Netlify
Try out Edge Functions on Netlify today! Click the button below to deploy this site with all of its demos to your Netlify account.