Skip to content

Commit

Permalink
fix: added support for "missing" matcher (#1905)
Browse files Browse the repository at this point in the history
* fix: added support for missing matcher

* fix: prettier fix

* fix: removed skip from missing matcher tests

* fix: updatiing demo to showcase missing matcher

* fix: added new cookie testing page for missing matcher

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
taty2010 and kodiakhq[bot] committed Jan 30, 2023
1 parent 2e5a728 commit f30e178
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 8 deletions.
22 changes: 22 additions & 0 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ export async function middleware(req: NextRequest) {
return response
}

if(pathname.startsWith('/matcher-cookie')) {
response = NextResponse.next()
response.cookies.set('missingCookie', 'true')
return response
}

if (pathname.startsWith('/conditional')) {
response = NextResponse.next()
response.headers.set('x-modified-edge', 'true')
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
return response
}

if (pathname.startsWith('/missing')) {
response = NextResponse.next()
response.headers.set('x-cookie-missing', 'true')
return response
}

if (pathname.startsWith('/shows')) {
if (pathname.startsWith('/shows/222')) {
response = NextResponse.next()
Expand Down Expand Up @@ -119,6 +131,7 @@ export const config = {
'/headers',
{ source: '/static' },
{ source: '/cookies' },
{ source: '/matcher-cookie'},
{ source: '/shows/((?!99|88).*)' },
{
source: '/conditional',
Expand All @@ -130,5 +143,14 @@ export const config = {
},
],
},
{
source: '/missing',
missing: [
{
type: 'cookie',
key: 'missingCookie',
}
],
},
],
}
9 changes: 9 additions & 0 deletions demos/middleware/pages/matcher-cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const MatcherCookie = () => {
return (
<div>
<p>The cookie "missingCookie" should be set to true</p>
</div>
)
}

export default MatcherCookie
13 changes: 13 additions & 0 deletions demos/middleware/pages/missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react'
import Link from 'next/link'

const Missing = () => {
return (
<div>
<p>Will Check if 'missingCookie' is missing and display headers</p>
<p>To test go to <Link href="/matcher-cookie">cookies page</Link> and come back</p>
</div>
)
}

export default Missing
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Run Next.js seamlessly on Netlify",
"scripts": {
"build:demo": "cd demos/default && npm run build",
"cy:open": "cypress open --config-file cypress/config/canary.json",
"cy:open": "cypress open --config-file cypress/config/all.json",
"dev:demo": "next dev demos/default",
"format": "run-s format:check-fix:*",
"format:ci": "run-s format:check:*",
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface MiddlewareMatcher {
regexp: string
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}

// This is the format after next@12.3.0
Expand Down
Loading

0 comments on commit f30e178

Please sign in to comment.