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

Function processRoutes

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

* Processes a list of routes and updates the URL if any match

(
  routes: Route[],
  url: URL,
  requestHeaders: Headers,
  responseHeaders: Headers,
  initialOrigin: string
)

Source from the content-addressed store, hash-verified

75 * Processes a list of routes and updates the URL if any match
76 */
77function processRoutes(
78 routes: Route[],
79 url: URL,
80 requestHeaders: Headers,
81 responseHeaders: Headers,
82 initialOrigin: string
83): {
84 url: URL
85 externalRewrite?: URL
86 redirect?: {
87 url: URL
88 status: number
89 }
90 stopped: boolean
91 status?: number
92} {
93 let currentUrl = url
94 let currentStatus: number | undefined
95
96 for (const route of routes) {
97 const match = matchRoute(route, currentUrl, requestHeaders)
98
99 if (match.matched) {
100 if (match.headers) {
101 for (const [key, value] of Object.entries(match.headers)) {
102 responseHeaders.set(key, value)
103 }
104 }
105
106 if (route.status) {
107 currentStatus = route.status
108 }
109
110 if (match.destination) {
111 // Check if route has redirect status and Location/Refresh header
112 if (
113 isRedirectStatus(route.status) &&
114 match.headers &&
115 hasRedirectHeaders(match.headers)
116 ) {
117 const redirectUrl = isExternalDestination(match.destination)
118 ? new URL(match.destination)
119 : applyDestination(currentUrl, match.destination)
120
121 return {
122 url: currentUrl,
123 redirect: {
124 url: redirectUrl,
125 status: route.status!,
126 },
127 stopped: true,
128 status: currentStatus,
129 }
130 }
131
132 // Check if it's an external rewrite
133 if (isExternalDestination(match.destination)) {
134 return {

Callers 1

resolveRoutesFunction · 0.70

Calls 7

isRedirectStatusFunction · 0.90
hasRedirectHeadersFunction · 0.90
isExternalDestinationFunction · 0.90
applyDestinationFunction · 0.90
matchRouteFunction · 0.70
setMethod · 0.65
entriesMethod · 0.45

Tested by

no test coverage detected