Skip to content
Discussion options

You must be logged in to vote

Based on the research, here are the current solutions for setting response headers in App Router:


✅ Working Solutions

1. Use Route Handlers (API Routes)

// app/api/product/[id]/route.ts
import { NextResponse } from 'next/server';

export async function GET(request: Request, { params }) {
  const product = await getProduct(params.id);
  
  return NextResponse.json(product, {
    headers: {
      'Surrogate-Key': product.id,
      'Cache-Control': `s-maxage=${product.cacheTime}`,
    },
  });
}

2. Use Middleware (when headers don't depend on fetched data)

// middleware.ts
import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by diego-toress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants