( url: URL, basePath: string, buildId: string )
| 3 | * ${basePath}/_next/data/$buildId/$path.json -> ${basePath}/$path |
| 4 | */ |
| 5 | export 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 |
no test coverage detected