rsnext/packages/next/next-server/server/get-route-from-entrypoint.ts
Jan Potoms bc2cb2210f
Fix prerendered nested index handling (#14383)
Noticed this while reviewing https://github.com/vercel/next.js/pull/14376. After having done https://github.com/vercel/next.js/pull/13699, this code didn't feel right to me:
```js
function prepareRoute(path: string) {
  path = delBasePath(path || '')
  // this /index rewrite is problematic, it makes pages/index.js 
  // and pages/index/index.js point to the same thing:
  return toRoute(!path || path === '/' ? '/index' : path)
}
```
Added a nested index page to the prerender tests and found it was rendering the `/` route on navigation. This uncovered 2 more places around the dataroute where the index path was not translated correctly.

**edit:**

Just to note that there was nothing wrong with https://github.com/vercel/next.js/pull/14376, the issue was already there, I just noticed it while reading that PR
2020-06-23 05:49:48 +00:00

33 lines
982 B
TypeScript

import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
// matches static/<buildid>/pages/:page*.js
// const SERVER_ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)$/
// matches pages/:page*.js
const SERVER_ROUTE_NAME_REGEX = /^pages[/\\](.*)$/
// matches static/pages/:page*.js
const BROWSER_ROUTE_NAME_REGEX = /^static[/\\]pages[/\\](.*)$/
function matchBundle(regex: RegExp, input: string): string | null {
const result = regex.exec(input)
if (!result) {
return null
}
return getRouteFromAssetPath(`/${result[1]}`)
}
export default function getRouteFromEntrypoint(
entryFile: string,
// TODO: Remove this parameter
_isServerlessLike: boolean = false
): string | null {
let pagePath = matchBundle(SERVER_ROUTE_NAME_REGEX, entryFile)
if (pagePath) {
return pagePath
}
// Potentially the passed item is a browser bundle so we try to match that also
return matchBundle(BROWSER_ROUTE_NAME_REGEX, entryFile)
}