File size: 3,163 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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 {
  /**
   * The base path in case the pathname included it.
   */
  basePath?: string
  /**
   * The buildId for when the parsed URL is a data URL. Parsing it can be
   * disabled with the `parseData` option.
   */
  buildId?: string
  /**
   * If there was a locale in the pathname, this will hold its value.
   */
  locale?: string
  /**
   * The processed pathname without a base path, locale, or data URL elements
   * when parsing it is enabled.
   */
  pathname: string
  /**
   * A boolean telling if the pathname had a trailingSlash. This can be only
   * true if trailingSlash is enabled.
   */
  trailingSlash?: boolean
}

interface Options {
  /**
   * When passed to true, this function will also parse Nextjs data URLs.
   */
  parseData?: boolean
  /**
   * A partial of the Next.js configuration to parse the URL.
   */
  nextConfig?: {
    basePath?: string
    i18n?: { locales?: readonly string[] } | null
    trailingSlash?: boolean
  }

  /**
   * If provided, this normalizer will be used to detect the locale instead of
   * the default locale detection.
   */
  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('/')}` : '/'

    // update pathname with normalized if enabled although
    // we use normalized to populate locale info still
    if (options.parseData === true) {
      info.pathname = pathnameNoDataPrefix
    }
  }

  // If provided, use the locale route normalizer to detect the locale instead
  // of the function below.
  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
}