5b9ad8da90
* Move next-server directory files to server directory * Update tests * Update paths in other places
24 lines
758 B
TypeScript
24 lines
758 B
TypeScript
import { posix } from '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)) {
|
|
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
|
|
}
|