Skip to content

Commit 12498e7

Browse files
committed
fix: added support for missing matcher
1 parent aaf0548 commit 12498e7

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/runtime/src/helpers/edge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface MiddlewareMatcher {
3131
regexp: string
3232
locale?: false
3333
has?: RouteHas[]
34+
missing?: RouteHas[]
3435
}
3536

3637
// This is the format after next@12.3.0

packages/runtime/src/templates/edge-shared/next-utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type Rewrite = {
4242
basePath?: false
4343
locale?: false
4444
has?: RouteHas[]
45+
missing?: RouteHas[]
4546
regex: string
4647
}
4748

@@ -51,6 +52,7 @@ export type Header = {
5152
locale?: false
5253
headers: Array<{ key: string; value: string }>
5354
has?: RouteHas[]
55+
missing?: RouteHas[]
5456
regex: string
5557
}
5658
export type Redirect = {
@@ -59,6 +61,7 @@ export type Redirect = {
5961
basePath?: false
6062
locale?: false
6163
has?: RouteHas[]
64+
missing?: RouteHas[]
6265
statusCode?: number
6366
permanent?: boolean
6467
regex: string
@@ -138,11 +141,11 @@ export function parseUrl(url: string): ParsedUrl {
138141

139142
// prepare-destination.ts
140143
// Changed to use WHATWG Fetch Request instead of IncomingMessage
141-
export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[], query: Params): false | Params {
144+
export function matchHas(req: Pick<Request, 'headers' | 'url'>, query: Params, has: RouteHas[] = [], missing: RouteHas[] = []): false | Params {
142145
const params: Params = {}
143146
const cookies = getCookies(req.headers)
144147
const url = new URL(req.url)
145-
const allMatch = has.every((hasItem) => {
148+
const hasMatch = (hasItem: RouteHas) => {
146149
let value: undefined | string | null
147150
let key = hasItem.key
148151

@@ -189,7 +192,9 @@ export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[],
189192
}
190193
}
191194
return false
192-
})
195+
}
196+
197+
const allMatch = has.every((item) => hasMatch(item)) && !missing.some((item) => hasMatch(item))
193198

194199
if (allMatch) {
195200
return params
@@ -371,6 +376,7 @@ export interface MiddlewareMatcher {
371376
regexp: string
372377
locale?: false
373378
has?: RouteHas[]
379+
missing?: RouteHas[]
374380
}
375381

376382
export function getMiddlewareRouteMatcher(matchers: MiddlewareMatcher[]): MiddlewareRouteMatch {
@@ -381,8 +387,8 @@ export function getMiddlewareRouteMatcher(matchers: MiddlewareMatcher[]): Middle
381387
continue
382388
}
383389

384-
if (matcher.has) {
385-
const hasParams = matchHas(req, matcher.has, query)
390+
if (matcher.has || matcher.missing) {
391+
const hasParams = matchHas(req, query, matcher.has, matcher.missing)
386392
if (!hasParams) {
387393
continue
388394
}

0 commit comments

Comments
 (0)