MCPcopy
hub / github.com/vercel/next.js / matchRoute

Function matchRoute

packages/next-routing/src/resolve-routes.ts:21–72  ·  view source on GitHub ↗

* Attempts to match a route against the current URL and conditions

(
  route: Route,
  url: URL,
  headers: Headers
)

Source from the content-addressed store, hash-verified

19 * Attempts to match a route against the current URL and conditions
20 */
21function matchRoute(
22 route: Route,
23 url: URL,
24 headers: Headers
25): {
26 matched: boolean
27 destination?: string
28 headers?: Record<string, string>
29 regexMatches?: RegExpMatchArray
30 hasCaptures?: Record<string, string>
31} {
32 // Check if source regex matches the pathname
33 const regex = new RegExp(route.sourceRegex)
34 const regexMatches = url.pathname.match(regex)
35
36 if (!regexMatches) {
37 return { matched: false }
38 }
39
40 // Check has conditions
41 const hasResult = checkHasConditions(route.has, url, headers)
42 if (!hasResult.matched) {
43 return { matched: false }
44 }
45
46 // Check missing conditions
47 const missingMatched = checkMissingConditions(route.missing, url, headers)
48 if (!missingMatched) {
49 return { matched: false }
50 }
51
52 // Replace placeholders in destination
53 const destination = route.destination
54 ? replaceDestination(route.destination, regexMatches, hasResult.captures)
55 : undefined
56 const resolvedHeaders = route.headers
57 ? Object.fromEntries(
58 Object.entries(route.headers).map(([key, value]) => [
59 replaceDestination(key, regexMatches, hasResult.captures),
60 replaceDestination(value, regexMatches, hasResult.captures),
61 ])
62 )
63 : undefined
64
65 return {
66 matched: true,
67 destination,
68 headers: resolvedHeaders,
69 regexMatches,
70 hasCaptures: hasResult.captures,
71 }
72}
73
74/**
75 * Processes a list of routes and updates the URL if any match

Callers 3

processRoutesFunction · 0.70
applyOnMatchHeadersFunction · 0.70
resolveRoutesFunction · 0.70

Calls 6

checkHasConditionsFunction · 0.90
checkMissingConditionsFunction · 0.90
replaceDestinationFunction · 0.90
matchMethod · 0.65
mapMethod · 0.45
entriesMethod · 0.45

Tested by

no test coverage detected