( params: ResolveRoutesParams )
| 368 | } |
| 369 | |
| 370 | export async function resolveRoutes( |
| 371 | params: ResolveRoutesParams |
| 372 | ): Promise<ResolveRoutesResult> { |
| 373 | const { |
| 374 | url: initialUrl, |
| 375 | basePath, |
| 376 | requestBody, |
| 377 | headers: initialHeaders, |
| 378 | pathnames, |
| 379 | routes, |
| 380 | invokeMiddleware, |
| 381 | buildId, |
| 382 | i18n, |
| 383 | } = params |
| 384 | |
| 385 | const { shouldNormalizeNextData } = routes |
| 386 | |
| 387 | let currentUrl = new URL(initialUrl.toString()) |
| 388 | let currentRequestHeaders = new Headers(initialHeaders) |
| 389 | let currentResponseHeaders = new Headers() |
| 390 | let currentStatus: number | undefined |
| 391 | const initialOrigin = initialUrl.origin |
| 392 | |
| 393 | // Check if the original URL is a data URL and normalize if so |
| 394 | let isDataUrl = false |
| 395 | if (shouldNormalizeNextData) { |
| 396 | const dataPrefix = `${basePath}/_next/data/${buildId}/` |
| 397 | isDataUrl = initialUrl.pathname.startsWith(dataPrefix) |
| 398 | |
| 399 | if (isDataUrl) { |
| 400 | currentUrl = normalizeNextDataUrl(currentUrl, basePath, buildId) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Handle i18n locale detection and redirects |
| 405 | if (i18n && !isDataUrl) { |
| 406 | const pathname = currentUrl.pathname.startsWith(basePath) |
| 407 | ? currentUrl.pathname.slice(basePath.length) || '/' |
| 408 | : currentUrl.pathname |
| 409 | |
| 410 | // Skip locale handling for _next and api routes |
| 411 | if (!pathname.startsWith('/_next/') && !pathname.startsWith('/api/')) { |
| 412 | const hostname = currentUrl.hostname |
| 413 | const cookieHeader = currentRequestHeaders.get('cookie') || undefined |
| 414 | const acceptLanguageHeader = |
| 415 | currentRequestHeaders.get('accept-language') || undefined |
| 416 | |
| 417 | // Detect locale from path first |
| 418 | const pathLocaleResult = normalizeLocalePath(pathname, i18n.locales) |
| 419 | const localeInPath = !!pathLocaleResult.detectedLocale |
| 420 | |
| 421 | // Detect domain locale |
| 422 | const domainLocale = detectDomainLocale(i18n.domains, hostname) |
| 423 | const defaultLocale = domainLocale?.defaultLocale || i18n.defaultLocale |
| 424 | |
| 425 | // Determine target locale if locale detection is enabled |
| 426 | let targetLocale = pathLocaleResult.detectedLocale || defaultLocale |
| 427 |
no test coverage detected