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

Function checkDynamicRoutes

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

* Checks dynamic routes for a match and returns result if found

(
  dynamicRoutes: Route[],
  url: URL,
  pathnames: string[],
  requestHeaders: Headers,
  responseHeaders: Headers,
  onMatchRoutes: Route[],
  basePath: string,
  buildId: string,
  shouldNormalizeNextData?: boolean,
  isDataUrl?: boolean
)

Source from the content-addressed store, hash-verified

288 * Checks dynamic routes for a match and returns result if found
289 */
290function checkDynamicRoutes(
291 dynamicRoutes: Route[],
292 url: URL,
293 pathnames: string[],
294 requestHeaders: Headers,
295 responseHeaders: Headers,
296 onMatchRoutes: Route[],
297 basePath: string,
298 buildId: string,
299 shouldNormalizeNextData?: boolean,
300 isDataUrl?: boolean
301): {
302 matched: boolean
303 result?: ResolveRoutesResult
304 resetUrl?: URL
305} {
306 // Denormalize before checking dynamic routes if this was originally a data URL
307 let checkUrl = url
308 if (isDataUrl && shouldNormalizeNextData) {
309 checkUrl = denormalizeNextDataUrl(url, basePath, buildId)
310 }
311
312 for (const route of dynamicRoutes) {
313 const match = matchDynamicRoute(checkUrl.pathname, route)
314
315 if (match.matched) {
316 // Check has/missing conditions
317 const hasResult = checkHasConditions(route.has, checkUrl, requestHeaders)
318 const missingMatched = checkMissingConditions(
319 route.missing,
320 checkUrl,
321 requestHeaders
322 )
323
324 if (hasResult.matched && missingMatched) {
325 const replacedDestination = route.destination
326 ? replaceDestination(
327 route.destination,
328 match.regexMatches || null,
329 hasResult.captures
330 )
331 : undefined
332 // Check if the destination pathname (template path) is in the provided pathnames list
333 // For dynamic routes, the destination contains the template path like /dynamic/[slug]
334 const pathnameToCheck = replacedDestination
335 ? replacedDestination.split('?')[0]
336 : checkUrl.pathname
337 const matchedPath = matchesPathname(pathnameToCheck, pathnames)
338 if (matchedPath) {
339 const resolvedUrl = replacedDestination
340 ? mergeDestinationQueryIntoUrl(checkUrl, replacedDestination)
341 : checkUrl
342 const finalHeaders = applyOnMatchHeaders(
343 onMatchRoutes,
344 resolvedUrl,
345 requestHeaders,
346 responseHeaders
347 )

Callers 1

resolveRoutesFunction · 0.85

Calls 10

denormalizeNextDataUrlFunction · 0.90
checkHasConditionsFunction · 0.90
checkMissingConditionsFunction · 0.90
replaceDestinationFunction · 0.90
matchDynamicRouteFunction · 0.85
matchesPathnameFunction · 0.85
applyOnMatchHeadersFunction · 0.85
splitMethod · 0.80

Tested by

no test coverage detected