Given a name, set a cookie with the given value on the response.
// Given incoming request /homelet response = NextResponse.next()// Set a cookie to hide the bannerresponse.cookies.set('show-banner', 'false')// Response will have a `Set-Cookie:show-banner=false;path=/home` headerreturn response
Given a cookie name, return the value of the cookie. If the cookie is not found, undefined is returned. If multiple cookies are found, the first one is returned.
Given a cookie name, delete the cookie from the response.
// Given incoming request /homelet response = NextResponse.next()// Returns true for deleted, false is nothing is deletedresponse.cookies.delete('experiments')
import { NextResponse } from 'next/server'return NextResponse.redirect(new URL('/new', request.url))
The URL can be created and modified before being used in the NextResponse.redirect() method. For example, you can use the request.nextUrl property to get the current URL, and then modify it to redirect to a different URL.
import { NextResponse } from 'next/server'// Given an incoming request...const loginUrl = new URL('/login', request.url)// Add ?from=/incoming-url to the /login URLloginUrl.searchParams.set('from', request.nextUrl.pathname)// And redirect to the new URLreturn NextResponse.redirect(loginUrl)
The next() method is useful for Middleware, as it allows you to return early and continue routing.
import { NextResponse } from 'next/server'return NextResponse.next()
You can also forward headers upstream when producing the response, using NextResponse.next({ request: { headers } }):
import { NextResponse } from 'next/server'// Given an incoming request...const newHeaders = new Headers(request.headers)// Add a new headernewHeaders.set('x-version', '123')// Forward the modified request headers upstreamreturn NextResponse.next({ request: { // New request headers headers: newHeaders, },})
This forwards newHeaders upstream to the target page, route, or server action, and does not expose them to the client. While this pattern is useful for passing data upstream, it should be used with caution because the headers containing this data may be forwarded to external services.
In contrast, NextResponse.next({ headers }) is a shorthand for sending headers from middleware to the client. This is NOT good practice and should be avoided. Among other reasons because setting response headers like Content-Type, can override framework expectations (for example, the Content-Type used by Server Actions), leading to failed submissions or broken streaming responses.
import { type NextRequest, NextResponse } from 'next/server'async function middleware(request: NextRequest) { const headers = await injectAuth(request.headers) // DO NOT forward headers like this return NextResponse.next({ headers })}
In general, avoid copying all incoming request headers because doing so can leak sensitive data to clients or upstream services.
Prefer a defensive approach by creating a subset of incoming request headers using an allow-list. For example, you might discard custom x-* headers and only forward known-safe headers:
import { type NextRequest, NextResponse } from 'next/server'function middleware(request: NextRequest) { const incoming = new Headers(request.headers) const forwarded = new Headers() for (const [name, value] of incoming) { const headerName = name.toLowerCase() // Keep only known-safe headers, discard custom x-* and other sensitive ones if ( !headerName.startsWith('x-') && headerName !== 'authorization' && headerName !== 'cookie' ) { // Preserve original header name casing forwarded.set(name, value) } } return NextResponse.next({ request: { headers: forwarded, }, })}