| import { normalizeLocalePath } from '../../i18n/normalize-locale-path' |
| import { removePathPrefix } from './remove-path-prefix' |
| import { pathHasPrefix } from './path-has-prefix' |
| import type { I18NProvider } from '../../../../server/lib/i18n-provider' |
|
|
| export interface NextPathnameInfo { |
| |
| |
| |
| basePath?: string |
| |
| |
| |
| |
| buildId?: string |
| |
| |
| |
| locale?: string |
| |
| |
| |
| |
| pathname: string |
| |
| |
| |
| |
| trailingSlash?: boolean |
| } |
|
|
| interface Options { |
| |
| |
| |
| parseData?: boolean |
| |
| |
| |
| nextConfig?: { |
| basePath?: string |
| i18n?: { locales?: readonly string[] } | null |
| trailingSlash?: boolean |
| } |
|
|
| |
| |
| |
| |
| i18nProvider?: I18NProvider |
| } |
|
|
| export function getNextPathnameInfo( |
| pathname: string, |
| options: Options |
| ): NextPathnameInfo { |
| const { basePath, i18n, trailingSlash } = options.nextConfig ?? {} |
| const info: NextPathnameInfo = { |
| pathname, |
| trailingSlash: pathname !== '/' ? pathname.endsWith('/') : trailingSlash, |
| } |
|
|
| if (basePath && pathHasPrefix(info.pathname, basePath)) { |
| info.pathname = removePathPrefix(info.pathname, basePath) |
| info.basePath = basePath |
| } |
| let pathnameNoDataPrefix = info.pathname |
|
|
| if ( |
| info.pathname.startsWith('/_next/data/') && |
| info.pathname.endsWith('.json') |
| ) { |
| const paths = info.pathname |
| .replace(/^\/_next\/data\//, '') |
| .replace(/\.json$/, '') |
| .split('/') |
|
|
| const buildId = paths[0] |
| info.buildId = buildId |
| pathnameNoDataPrefix = |
| paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/' |
|
|
| |
| |
| if (options.parseData === true) { |
| info.pathname = pathnameNoDataPrefix |
| } |
| } |
|
|
| |
| |
| if (i18n) { |
| let result = options.i18nProvider |
| ? options.i18nProvider.analyze(info.pathname) |
| : normalizeLocalePath(info.pathname, i18n.locales) |
|
|
| info.locale = result.detectedLocale |
| info.pathname = result.pathname ?? info.pathname |
|
|
| if (!result.detectedLocale && info.buildId) { |
| result = options.i18nProvider |
| ? options.i18nProvider.analyze(pathnameNoDataPrefix) |
| : normalizeLocalePath(pathnameNoDataPrefix, i18n.locales) |
|
|
| if (result.detectedLocale) { |
| info.locale = result.detectedLocale |
| } |
| } |
| } |
| return info |
| } |
|
|