Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@vercel/edge] Support overriding request headers (#8724)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> This PR adds a feature in middleware to add, modify, or delete request headers. This feature is quite useful to pass data from middleware to Serverless/Edge API routes. ### New APIs Adds a new option `request.headers` to the `MiddlewareResponseInit` parameter in `NextResponse.next()` and `NextResponse.rewrite()`. It's a [`Header`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object holding *all* request headers. Specifically: ```ts interface ExtraResponseInit extends ResponseInit { request?: { headers?: Headers } } ``` ### Example ```ts // api/hello.ts export default (req, res) => { const valueFromMiddleware = req.headers['x-hello-from-middleware'] return res.send(valueFromMiddleware) } // middleware.ts import { next } from '@vercel/edge' export default function middleware(request: NextRequest) { // Clone request headers const headers = new Headers(request.headers); // Add a new request header headers.set('x-hello-from-middleware', 'foo'); // Delete a request header from the client headers.delete('x-from-client'); return next({ request: { headers } }); } ``` ### New middleware headers - `x-middleware-override-headers`: A comma separated list of *all* request header names. Headers not listed will be deleted. - `x-middleware-request-<name>`: A new value for the header `<name>`. ### Related Issues - Next.js' implementation: vercel/next.js#41380 ### 📋 Checklist <!-- Please keep your PR as a Draft until the checklist is complete --> #### Tests - [ ] The code changed/added as part of this PR has been covered with tests - [ ] All tests pass locally with `yarn test-unit` #### Code Review - [ ] This PR has a concise title and thorough description useful to a reviewer - [ ] Issue from task tracker has a link to this PR Co-authored-by: Gal Schlezinger <gal@spitfire.co.il>
- Loading branch information
Showing
5 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Interface: ModifiedRequest | ||
|
||
## Table of contents | ||
|
||
### Properties | ||
|
||
- [headers](ModifiedRequest.md#headers) | ||
|
||
## Properties | ||
|
||
### headers | ||
|
||
• `Optional` **headers**: `Headers` | ||
|
||
If set, overwrites the incoming headers to the origin request. | ||
|
||
This is useful when you want to pass data between a Middleware and a | ||
Serverless or Edge Function. | ||
|
||
**`Example`** | ||
|
||
<caption>Add a `x-user-id` header and remove the `Authorization` header</caption> | ||
|
||
```ts | ||
import { rewrite } from '@vercel/edge'; | ||
export default async function middleware(request: Request): Promise<Response> { | ||
const newHeaders = new Headers(request.headers); | ||
newHeaders.set('x-user-id', 'user_123'); | ||
newHeaders.delete('authorization'); | ||
return rewrite(request.url, { | ||
request: { headers: newHeaders }, | ||
}); | ||
} | ||
``` | ||
|
||
#### Defined in | ||
|
||
[src/middleware-helpers.ts:23](https://github.com/vercel/vercel/blob/main/packages/edge/src/middleware-helpers.ts#L23) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters