• J
    Fix pages/index.js and pages/index/index.js behavior (#13699) · 1b36f0c0
    Jan Potoms 提交于
    Disambiguate between pages/index.js and pages/index/index.js so that they resolve differently.
    It all started with a bug in pagesmanifest that propagated throughout the codebase. After fixing pagesmanifest I was able to remove a few hacks here and there and more logic is shared now. especially the logic that resolves an entrypoint back into a route path. To sum up what happened:
    
    - `getRouteFromEntrypoint` is the inverse operation of `getPageFile` that's under `pages/_document.tsx`
    - `denormalizePagePath` is the inverse operation of `normalizePagePath`.
    
    Everything is refactored in terms of these operations, that makes their behavior uniform and easier to update/patch in a central place. Before there were subtle differences between those that made `index/index.js` hard to handle.
    
    Some potential follow up on this PR:
    - [`hot-reloader`](https://github.com/vercel/next.js/pull/13699/files#diff-6161346d2c5f4b7abc87059d8768c44bR207) still has one place that does very similar behavior to `getRouteFromEntrypoint`. It can probably be rewritten in terms of `getRouteFromEntrypoint`.
    - There are a few places where `denormalizePagePath(normalizePagePath(...))` is happening. This is a sign that `normalizePagePath` is doing some validation that is independent of its rewriting logic. That should probably be factored out in its own function. after that I should probably investigate whether `normalizePagePath` is even still needed at all.
    - a lot of code is doing `.replace(/\\/g, '')`. If wanted, that could be replaced with `normalizePathSep`.
    - It looks to me like some logic that's spread across the project can be centralized in 4 functions 
      - `getRouteFromEntrypoint` (part of this PR)
      - its inverse `getEntrypointFromRoute` (already exists in `_document.tsx` as `getPageFile`)
      - `getRouteFromPageFile` 
      - its inverse `getPageFileFromRoute` (already exists as `findPageFile ` in `server/lib/find-page-file.ts`)
    
      It could be beneficial to structure the code to keep these fuctionalities close together and name them similarly.
     - revise `index.amp` handling in pagesmanifest. I left it alone in this PR to keep it scoped, but it may be broken wrt nested index files as well. It might even make sense to reshape the pagesmanifest altogether to handle html/json/amp/... better
    1b36f0c0
hot-reloader.ts 15.8 KB