Skip to content

feat: add support for Next 12.3 middleware matchers #1612

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ test
lib
demos
packages/runtime/src/templates/edge
packages/runtime/src/templates/edge-shared
packages/runtime/lib
packages/runtime/dist-types
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"deno.enablePaths": [
"packages/runtime/src/templates/edge",
"packages/runtime/src/templates/edge-shared",
"demos/middleware/.netlify/edge-functions",
"demos/server-components/.netlify/edge-functions",
],
Expand Down
23 changes: 23 additions & 0 deletions cypress/integration/middleware/standard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ describe('Standard middleware', () => {
})
})
})

describe('Middleware matchers', () => {
it('does not apply "has" matcher when headers are not sent', () => {
cy.request('/conditional').then((response) => {
expect(response.headers).not.to.have.property('x-is-deno', 'true')
expect(response.headers).not.to.have.property('x-modified-edge', 'true')
})
})

it('matches when headers are sent', () => {
cy.request({ url: '/conditional', headers: { 'x-my-header': 'my-value' } }).then((response) => {
expect(response.headers).to.have.property('x-is-deno', 'true')
expect(response.headers).to.have.property('x-modified-edge', 'true')
})
})

it('matches when headers are sent', () => {
cy.request('/_next/data/build-id/static.json').then((response) => {
expect(response.headers).to.have.property('x-is-deno', 'true')
expect(response.headers).to.have.property('x-modified-edge', 'true')
})
})
})
2 changes: 1 addition & 1 deletion demos/base-path/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"next": "^12.2.0"
"next": "^12.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
13 changes: 13 additions & 0 deletions demos/canary/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export async function middleware(req: NextRequest) {
const res = NextResponse.rewrite(new URL('/', req.url))
res.headers.set('x-response-header', 'set in middleware')
res.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
return res
}

export const config = {
matcher: ['/foo'],
}
Loading