forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 11
/
normalize-page-path.ts
32 lines (29 loc) · 1.02 KB
/
normalize-page-path.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
import { ensureLeadingSlash } from './ensure-leading-slash'
import { isDynamicRoute } from '../router/utils'
import { posix } from '../isomorphic/path'
import { NormalizeError } from '../utils'
/**
* Takes a page and transforms it into its file counterpart ensuring that the
* output is normalized. Note this function is not idempotent because a page
* `/index` can be referencing `/index/index.js` and `/index/index` could be
* referencing `/index/index/index.js`. Examples:
* - `/` -> `/index`
* - `/index/foo` -> `/index/index/foo`
* - `/index` -> `/index/index`
*/
export function normalizePagePath(page: string): string {
const normalized = ensureLeadingSlash(
/^\/index(\/|$)/.test(page) && !isDynamicRoute(page)
? `/index${page}`
: page === '/'
? '/index'
: page
)
const resolvedPage = posix.normalize(normalized)
if (resolvedPage !== normalized) {
throw new NormalizeError(
`Requested and resolved page mismatch: ${normalized} ${resolvedPage}`
)
}
return normalized
}