rsnext/packages/next/shared/lib/router/utils/format-next-pathname-info.ts
JJ Kasper acb46e1090
Fix NextUrl trailing slash normalize for data route (#41311)
x-ref: [slack
thread](https://vercel.slack.com/archives/C045FKE5P51/p1665074590321179)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
2022-10-10 16:50:31 -07:00

37 lines
1 KiB
TypeScript

import type { NextPathnameInfo } from './get-next-pathname-info'
import { removeTrailingSlash } from './remove-trailing-slash'
import { addPathPrefix } from './add-path-prefix'
import { addPathSuffix } from './add-path-suffix'
import { addLocale } from './add-locale'
interface ExtendedInfo extends NextPathnameInfo {
defaultLocale?: string
ignorePrefix?: boolean
}
export function formatNextPathnameInfo(info: ExtendedInfo) {
let pathname = addLocale(
info.pathname,
info.locale,
info.buildId ? undefined : info.defaultLocale,
info.ignorePrefix
)
if (info.buildId || !info.trailingSlash) {
pathname = removeTrailingSlash(pathname)
}
if (info.buildId) {
pathname = addPathSuffix(
addPathPrefix(pathname, `/_next/data/${info.buildId}`),
info.pathname === '/' ? 'index.json' : '.json'
)
}
pathname = addPathPrefix(pathname, info.basePath)
return !info.buildId && info.trailingSlash
? !pathname.endsWith('/')
? addPathSuffix(pathname, '/')
: pathname
: removeTrailingSlash(pathname)
}