Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Learn to add response headers at the edge.
Adding Response Headers in Edge Middleware is a minimal Next.js example from Vercel's official vercel/examples repository that demonstrates how to set custom response headers on every request using Edge Middleware. The template supplies a single middleware.ts file, a one-click Vercel deployment URL, and a live demo.
This template is a focused learning resource that shows the shortest possible middleware implementation: it creates a response with NextResponse.next(), sets a custom header x-modified-edge to true, and returns the response. The logic lives in one TypeScript file with no UI components, so the only way to confirm the middleware ran is to inspect response headers in the browser. The example is published in the vercel/examples GitHub repository under the edge-middleware folder.
pnpm create next-app --example https://github.com/vercel/examples/tree/main/edge-middleware/add-header add-header to scaffold a local copy, then run pnpm dev.The middleware.ts file exports a default function that Next.js runs before any route is handled. Inside, NextResponse.next() creates an empty response, response.headers.set('x-modified-edge', 'true') writes the custom header, and returning that response passes it to the application. Because the middleware has no matcher config, it runs for every request and adds the header globally.
Deploy or run the example, open the page, and look at the network tab in your browser devtools. Any request's response headers will include x-modified-edge: true.
Yes — the same Headers.set method can set or update any response header. You can also call delete on the headers object to remove an existing header.
No, the example has no visual components; its entire output is the middleware behavior, which you verify via network inspection.
Copy the middleware.ts file into the root of any Next.js project, then deploy to Vercel or any platform running the Edge Runtime. The middleware will apply to all routes.
