Skip to content

Commit 5d60191

Browse files
authored
feat: add Next 13 request header mutation to middleware (#1866)
* test: add Next 13 request header mutation to middleware * feat: support Next 13 request header mutation * refactor: remove duplicate route conditional * refactor: remove debugger * test: update test to use Deno style of testing include github workflow * style: lint * test: update test command * refactor: remove debugger statement that snuck in * fix: add the modified middleware headers to the request, not response update the related tests * style: lint * fix: remove debugger * test: move tests for request headers into standard
1 parent 0caeab8 commit 5d60191

File tree

8 files changed

+413
-19
lines changed

8 files changed

+413
-19
lines changed

.github/workflows/test-deno.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deno tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Git Checkout Deno Module
15+
uses: actions/checkout@v2
16+
- name: Use Deno Version ${{ matrix.deno-version }}
17+
uses: denolib/setup-deno@v2
18+
with:
19+
deno-version: vx.x.x
20+
- name: Test Deno
21+
run: deno test packages/runtime/src/templates/edge-shared/

cypress/integration/middleware/enhanced.spec.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
describe('Enhanced middleware', () => {
2-
it('adds request headers', () => {
3-
cy.request('/api/hello').then((response) => {
4-
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
5-
})
6-
})
7-
8-
it('adds request headers to a rewrite', () => {
9-
cy.request('/headers').then((response) => {
10-
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
11-
})
12-
})
13-
142
it('rewrites the response body', () => {
153
cy.visit('/static')
164
cy.get('#message').contains('This was static but has been transformed in')

cypress/integration/middleware/standard.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
describe('Standard middleware', () => {
2+
it('adds request headers', () => {
3+
cy.request('/api/hello').then((response) => {
4+
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
5+
})
6+
})
7+
8+
it('adds request headers to a rewrite', () => {
9+
cy.request('/headers').then((response) => {
10+
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
11+
})
12+
})
13+
214
it('rewrites to internal page', () => {
315
// preview mode is off by default
416
cy.visit('/shows/rewriteme')

demos/middleware/middleware.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ export async function middleware(req: NextRequest) {
88
let response
99
const { pathname } = req.nextUrl
1010

11+
if (pathname.startsWith('/api/hello')) {
12+
// Next 13 request header mutation functionality
13+
const headers = new Headers(req.headers)
14+
15+
headers.set('x-hello', 'world')
16+
return NextResponse.next({
17+
request: {
18+
headers
19+
}
20+
})
21+
}
22+
1123
const request = new MiddlewareRequest(req)
1224
if (pathname.startsWith('/static')) {
1325
// Unlike NextResponse.next(), this actually sends the request to the origin
@@ -24,12 +36,6 @@ export async function middleware(req: NextRequest) {
2436
return res
2537
}
2638

27-
if (pathname.startsWith('/api/hello')) {
28-
// Add a header to the request
29-
req.headers.set('x-hello', 'world')
30-
return request.next()
31-
}
32-
3339
if (pathname.startsWith('/api/geo')) {
3440
req.headers.set('x-geo-country', req.geo.country)
3541
req.headers.set('x-geo-region', req.geo.region)

package-lock.json

+273
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"postinstall": "run-s build install-husky",
1919
"install-husky": "if-env CI=1 || husky install node_modules/@netlify/eslint-config-node/.husky",
2020
"test": "run-s build:demo test:jest",
21+
"test:deno": "deno test packages/runtime/src/templates/edge-shared/",
2122
"test:next": "jest -c test/e2e/jest.config.js",
2223
"test:next:disabled": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.disabled.js",
2324
"test:next:all": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.all.js",
@@ -101,7 +102,8 @@
101102
"**/test/**/*.spec.ts",
102103
"!**/test/e2e/**",
103104
"!**/test/fixtures/**",
104-
"!**/test/sample/**"
105+
"!**/test/sample/**",
106+
"!**/test/templates/edge-shared/**"
105107
],
106108
"transform": {
107109
"\\.[jt]sx?$": "babel-jest"

0 commit comments

Comments
 (0)