forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix prerendered nested index handling (vercel#14383)
Noticed this while reviewing vercel#14376. After having done vercel#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 vercel#14376, the issue was already there, I just noticed it while reading that PR
- Loading branch information
Showing
13 changed files
with
164 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/next/next-server/lib/router/utils/get-asset-path-from-route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Translates a logical route into its pages asset path (relative from a common prefix) | ||
// "asset path" being its javascript file, data file, prerendered html,... | ||
export default function getAssetPathFromRoute( | ||
route: string, | ||
ext: string = '' | ||
): string { | ||
const path = | ||
route === '/' | ||
? '/index' | ||
: /^\/index(\/|$)/.test(route) | ||
? `/index${route}` | ||
: `${route}` | ||
return path + ext | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/next/next-server/lib/router/utils/get-route-from-asset-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Translate a pages asset path (relative from a common prefix) back into its logical route | ||
// "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/')) { | ||
assetPath = assetPath.slice(6) | ||
} else if (assetPath === '/index') { | ||
assetPath = '/' | ||
} | ||
return assetPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export const getStaticProps = () => { | ||
return { | ||
props: { | ||
nested: true, | ||
hello: 'hello', | ||
}, | ||
} | ||
} | ||
|
||
export default function Index({ hello, nested }) { | ||
const { query, pathname } = useRouter() | ||
return ( | ||
<> | ||
<p id="nested">{nested ? 'yes' : 'no'}</p> | ||
<p id="prop">{hello} world</p> | ||
<p id="query">{JSON.stringify(query)}</p> | ||
<p id="pathname">{pathname}</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Link from 'next/link' | ||
|
||
export async function getStaticProps() { | ||
return { | ||
props: { world: 'nested index' }, | ||
} | ||
} | ||
|
||
export default ({ world }) => { | ||
return ( | ||
<> | ||
<p>hello {world}</p> | ||
<Link href="/"> | ||
<a id="home">to home</a> | ||
</Link> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters