-
Notifications
You must be signed in to change notification settings - Fork 26k
/
parse-next-url.ts
54 lines (47 loc) · 1.55 KB
/
parse-next-url.ts
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
import { getCookieParser } from '../../../../server/api-utils'
import { getLocaleMetadata } from '../../i18n/get-locale-metadata'
import { parseUrl } from './parse-url'
import type { NextConfig, DomainLocale } from '../../../../server/config-shared'
import type { ParsedUrl } from './parse-url'
import type { PathLocale } from '../../i18n/normalize-locale-path'
import { hasBasePath, replaceBasePath } from '../../../../server/router-utils'
interface Params {
headers?: { [key: string]: string | string[] | undefined }
nextConfig: NextConfig
url?: string
}
export function parseNextUrl({ headers, nextConfig, url = '/' }: Params) {
const urlParsed: ParsedNextUrl = parseUrl(url)
const { basePath } = nextConfig
if (basePath && hasBasePath(urlParsed.pathname, basePath)) {
urlParsed.pathname = replaceBasePath(urlParsed.pathname, basePath)
urlParsed.basePath = basePath
}
if (nextConfig.i18n) {
urlParsed.locale = getLocaleMetadata({
cookies: getCookieParser(headers || {}),
headers: headers,
nextConfig: {
basePath: nextConfig.basePath,
i18n: nextConfig.i18n,
trailingSlash: nextConfig.trailingSlash,
},
url: urlParsed,
})
if (urlParsed.locale?.path.detectedLocale) {
urlParsed.pathname = urlParsed.locale.path.pathname
}
}
return urlParsed
}
export interface ParsedNextUrl extends ParsedUrl {
basePath?: string
locale?: {
defaultLocale: string
domain?: DomainLocale
locale: string
path: PathLocale
redirect?: string
trailingSlash?: boolean
}
}