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

Function normalizeNextDataUrl

packages/next-routing/src/next-data.ts:5–30  ·  view source on GitHub ↗
(
  url: URL,
  basePath: string,
  buildId: string
)

Source from the content-addressed store, hash-verified

3 * ${basePath}/_next/data/$buildId/$path.json -> ${basePath}/$path
4 */
5export function normalizeNextDataUrl(
6 url: URL,
7 basePath: string,
8 buildId: string
9): URL {
10 const newUrl = new URL(url.toString())
11 let pathname = newUrl.pathname
12
13 // Pattern: ${basePath}/_next/data/${buildId}/${path}.json
14 const dataPrefix = `${basePath}/_next/data/${buildId}/`
15
16 if (pathname.startsWith(dataPrefix)) {
17 // Remove the /_next/data/${buildId}/ part, keeping what comes after
18 let pathAfterData = pathname.slice(dataPrefix.length)
19
20 // Remove .json extension if present
21 if (pathAfterData.endsWith('.json')) {
22 pathAfterData = pathAfterData.slice(0, -5)
23 }
24
25 pathname = basePath ? `${basePath}/${pathAfterData}` : `/${pathAfterData}`
26 newUrl.pathname = pathname
27 }
28
29 return newUrl
30}
31
32/**
33 * Denormalizes URL by adding /_next/data/{buildId}/ prefix and .json extension

Callers 1

resolveRoutesFunction · 0.90

Calls 4

startsWithMethod · 0.80
toStringMethod · 0.45
sliceMethod · 0.45
endsWithMethod · 0.45

Tested by

no test coverage detected