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

Function matchDynamicRoute

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

* Matches dynamic routes and extracts route parameters

(
  pathname: string,
  route: Route
)

Source from the content-addressed store, hash-verified

229 * Matches dynamic routes and extracts route parameters
230 */
231function matchDynamicRoute(
232 pathname: string,
233 route: Route
234): {
235 matched: boolean
236 params?: Record<string, string>
237 regexMatches?: RegExpMatchArray
238} {
239 const regex = new RegExp(route.sourceRegex)
240 const match = pathname.match(regex)
241
242 if (!match) {
243 return { matched: false }
244 }
245
246 const params: Record<string, string> = {}
247
248 // Add numbered matches
249 for (let i = 1; i < match.length; i++) {
250 if (match[i] !== undefined) {
251 params[String(i)] = match[i]
252 }
253 }
254
255 // Add named matches
256 if (match.groups) {
257 Object.assign(params, match.groups)
258 }
259
260 return { matched: true, params, regexMatches: match }
261}
262
263/**
264 * Applies headers from onMatch routes

Callers 2

checkDynamicRoutesFunction · 0.85
resolveRoutesFunction · 0.85

Calls 3

assignMethod · 0.80
matchMethod · 0.65
StringFunction · 0.50

Tested by

no test coverage detected