2bdf1bc023
x-ref: #36190 x-ref: #31506 * Move nodejs ptah module usage to next-server, keep base-server and web-server headless for `'path'` * Use a native module `path` for nodejs runtime and `path` polyfill for edge runtime
27 lines
887 B
TypeScript
27 lines
887 B
TypeScript
import path from '../shared/lib/isomorphic/path'
|
|
import { isDynamicRoute } from '../shared/lib/router/utils'
|
|
|
|
const { posix } = path
|
|
|
|
export { normalizePathSep, denormalizePagePath } from './denormalize-page-path'
|
|
|
|
export function normalizePagePath(page: string): string {
|
|
// If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages
|
|
if (page === '/') {
|
|
page = '/index'
|
|
} else if (/^\/index(\/|$)/.test(page) && !isDynamicRoute(page)) {
|
|
page = `/index${page}`
|
|
}
|
|
// Resolve on anything that doesn't start with `/`
|
|
if (!page.startsWith('/')) {
|
|
page = `/${page}`
|
|
}
|
|
// Throw when using ../ etc in the pathname
|
|
const resolvedPage = posix.normalize(page)
|
|
if (page !== resolvedPage) {
|
|
throw new Error(
|
|
`Requested and resolved page mismatch: ${page} ${resolvedPage}`
|
|
)
|
|
}
|
|
return page
|
|
}
|