2022-05-27 20:29:04 +02:00
|
|
|
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
|
2022-05-30 20:19:37 +02:00
|
|
|
import { parsePath } from '../shared/lib/router/utils/parse-path'
|
2020-06-30 06:06:39 +02:00
|
|
|
|
2020-07-13 18:08:12 +02:00
|
|
|
/**
|
|
|
|
* Normalizes the trailing slash of a path according to the `trailingSlash` option
|
|
|
|
* in `next.config.js`.
|
|
|
|
*/
|
2022-05-30 20:19:37 +02:00
|
|
|
export const normalizePathTrailingSlash = (path: string) => {
|
|
|
|
if (!path.startsWith('/')) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
const { pathname, query, hash } = parsePath(path)
|
|
|
|
if (process.env.__NEXT_TRAILING_SLASH) {
|
|
|
|
if (/\.[^/]+\/?$/.test(pathname)) {
|
|
|
|
return `${removeTrailingSlash(pathname)}${query}${hash}`
|
|
|
|
} else if (pathname.endsWith('/')) {
|
|
|
|
return `${pathname}${query}${hash}`
|
|
|
|
} else {
|
|
|
|
return `${pathname}/${query}${hash}`
|
2020-06-30 06:06:39 +02:00
|
|
|
}
|
2022-05-30 20:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${removeTrailingSlash(pathname)}${query}${hash}`
|
|
|
|
}
|