Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@vercel/edge] Support overriding request headers #8724

Merged
merged 11 commits into from Oct 21, 2022

Conversation

nuta
Copy link
Contributor

@nuta nuta commented Oct 13, 2022

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 object holding all request headers. Specifically:

interface ExtraResponseInit extends ResponseInit {
  request?: {
    headers?: Headers
  }
}

Example

// 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

馃搵 Checklist

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

@nuta nuta force-pushed the edge-support-request-headers-manipulation branch from ddca94d to 889b0c7 Compare October 13, 2022 06:35
@nuta nuta marked this pull request as ready for review October 14, 2022 09:58
@nuta nuta requested a review from a team October 14, 2022 09:58
Co-authored-by: Gal Schlezinger <gal@spitfire.co.il>
Copy link
Contributor

@Schniz Schniz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:chefkiss:

@nuta nuta merged commit df9accf into main Oct 21, 2022
@nuta nuta deleted the edge-support-request-headers-manipulation branch October 21, 2022 02:27
kodiakhq bot pushed a commit that referenced this pull request Oct 27, 2022
Implements request headers override in middlewares.

#### 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

- #8724: Add helper functions for non-Next.js middlewares
- vercel/next.js#41380: Next.js' implementation

### 馃搵 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: edge semver: minor PR contains new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants