rsnext/packages/next/shared/lib/router/utils/get-route-from-asset-path.ts
Jiachi Liu 10d814d31a
Fix dynamic routes with pages under index folder (#32440)
Fixes incorrect generated manifest and generated directory for `index/[...dynamic]` pages

Too much normalizing adding extra `index/` prefix to `index/[...dynamic]` routes which lead to the incorrected generated routes like `.next/server/pages/index/index/index/[...dynamic]`

## Bug

Fixes https://github.com/vercel/customer-issues/issues/146

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
2021-12-13 20:36:31 +00:00

19 lines
643 B
TypeScript

// Translate a pages asset path (relative from a common prefix) back into its logical route
import { isDynamicRoute } from './is-dynamic'
// "asset path" being its javascript file, data file, prerendered html,...
export default function getRouteFromAssetPath(
assetPath: string,
ext: string = ''
): string {
assetPath = assetPath.replace(/\\/g, '/')
assetPath =
ext && assetPath.endsWith(ext) ? assetPath.slice(0, -ext.length) : assetPath
if (assetPath.startsWith('/index/') && !isDynamicRoute(assetPath)) {
assetPath = assetPath.slice(6)
} else if (assetPath === '/index') {
assetPath = '/'
}
return assetPath
}