Skip to content

Commit

Permalink
feat: add Next 13 request header mutation to middleware (#1866)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ericapisani committed Jan 9, 2023
1 parent 0caeab8 commit 5d60191
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 19 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test-deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Deno tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Git Checkout Deno Module
uses: actions/checkout@v2
- name: Use Deno Version ${{ matrix.deno-version }}
uses: denolib/setup-deno@v2
with:
deno-version: vx.x.x
- name: Test Deno
run: deno test packages/runtime/src/templates/edge-shared/
12 changes: 0 additions & 12 deletions cypress/integration/middleware/enhanced.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
describe('Enhanced middleware', () => {
it('adds request headers', () => {
cy.request('/api/hello').then((response) => {
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
})
})

it('adds request headers to a rewrite', () => {
cy.request('/headers').then((response) => {
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
})
})

it('rewrites the response body', () => {
cy.visit('/static')
cy.get('#message').contains('This was static but has been transformed in')
Expand Down
12 changes: 12 additions & 0 deletions cypress/integration/middleware/standard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
describe('Standard middleware', () => {
it('adds request headers', () => {
cy.request('/api/hello').then((response) => {
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
})
})

it('adds request headers to a rewrite', () => {
cy.request('/headers').then((response) => {
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
})
})

it('rewrites to internal page', () => {
// preview mode is off by default
cy.visit('/shows/rewriteme')
Expand Down
18 changes: 12 additions & 6 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export async function middleware(req: NextRequest) {
let response
const { pathname } = req.nextUrl

if (pathname.startsWith('/api/hello')) {
// Next 13 request header mutation functionality
const headers = new Headers(req.headers)

headers.set('x-hello', 'world')
return NextResponse.next({
request: {
headers
}
})
}

const request = new MiddlewareRequest(req)
if (pathname.startsWith('/static')) {
// Unlike NextResponse.next(), this actually sends the request to the origin
Expand All @@ -24,12 +36,6 @@ export async function middleware(req: NextRequest) {
return res
}

if (pathname.startsWith('/api/hello')) {
// Add a header to the request
req.headers.set('x-hello', 'world')
return request.next()
}

if (pathname.startsWith('/api/geo')) {
req.headers.set('x-geo-country', req.geo.country)
req.headers.set('x-geo-region', req.geo.region)
Expand Down
273 changes: 273 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"postinstall": "run-s build install-husky",
"install-husky": "if-env CI=1 || husky install node_modules/@netlify/eslint-config-node/.husky",
"test": "run-s build:demo test:jest",
"test:deno": "deno test packages/runtime/src/templates/edge-shared/",
"test:next": "jest -c test/e2e/jest.config.js",
"test:next:disabled": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.disabled.js",
"test:next:all": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.all.js",
Expand Down Expand Up @@ -101,7 +102,8 @@
"**/test/**/*.spec.ts",
"!**/test/e2e/**",
"!**/test/fixtures/**",
"!**/test/sample/**"
"!**/test/sample/**",
"!**/test/templates/edge-shared/**"
],
"transform": {
"\\.[jt]sx?$": "babel-jest"
Expand Down
Loading

0 comments on commit 5d60191

Please sign in to comment.