Unverified Commit bca80141 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix require cache conflict between app and pages (#46736)

Related NEXT-472

When you have `app/page.js` and `pages/page.js`, the `pagePath` are the
same, which is `/page`. This will result require cache conflicts. When
you visit the `app/` page first, then the `pages/` page, the 2nd request
will still get the app dir page module, which result in server error.

Solution: use different cache key of pagePathCache for pages/ and app/ 

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent 45492eac
...@@ -2400,7 +2400,7 @@ export default async function build( ...@@ -2400,7 +2400,7 @@ export default async function build(
// remove server bundles that were exported // remove server bundles that were exported
for (const page of staticPages) { for (const page of staticPages) {
const serverBundle = getPagePath(page, distDir) const serverBundle = getPagePath(page, distDir, undefined, false)
await promises.unlink(serverBundle) await promises.unlink(serverBundle)
} }
...@@ -2514,7 +2514,12 @@ export default async function build( ...@@ -2514,7 +2514,12 @@ export default async function build(
.traceAsyncFn(async () => { .traceAsyncFn(async () => {
file = `${file}.${ext}` file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file) const orig = path.join(exportOptions.outdir, file)
const pagePath = getPagePath(originPage, distDir) const pagePath = getPagePath(
originPage,
distDir,
undefined,
false
)
const relativeDest = path const relativeDest = path
.relative( .relative(
......
...@@ -719,7 +719,7 @@ export default async function exportApp( ...@@ -719,7 +719,7 @@ export default async function exportApp(
} }
route = normalizePagePath(route) route = normalizePagePath(route)
const pagePath = getPagePath(pageName, distDir) const pagePath = getPagePath(pageName, distDir, undefined, false)
const distPagesDir = join( const distPagesDir = join(
pagePath, pagePath,
// strip leading / and then recurse number of nested dirs // strip leading / and then recurse number of nested dirs
......
...@@ -29,10 +29,10 @@ const pagePathCache = ...@@ -29,10 +29,10 @@ const pagePathCache =
export function getMaybePagePath( export function getMaybePagePath(
page: string, page: string,
distDir: string, distDir: string,
locales?: string[], locales: string[] | undefined,
appDirEnabled?: boolean isAppPath: boolean
): string | null { ): string | null {
const cacheKey = `${page}:${distDir}:${locales}` const cacheKey = `${page}:${distDir}:${locales}:${isAppPath}`
if (pagePathCache.has(cacheKey)) { if (pagePathCache.has(cacheKey)) {
return pagePathCache.get(cacheKey) as string | null return pagePathCache.get(cacheKey) as string | null
...@@ -41,7 +41,7 @@ export function getMaybePagePath( ...@@ -41,7 +41,7 @@ export function getMaybePagePath(
const serverBuildPath = join(distDir, SERVER_DIRECTORY) const serverBuildPath = join(distDir, SERVER_DIRECTORY)
let appPathsManifest: undefined | PagesManifest let appPathsManifest: undefined | PagesManifest
if (appDirEnabled) { if (isAppPath) {
appPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST)) appPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST))
} }
const pagesManifest = require(join( const pagesManifest = require(join(
...@@ -94,10 +94,10 @@ export function getMaybePagePath( ...@@ -94,10 +94,10 @@ export function getMaybePagePath(
export function getPagePath( export function getPagePath(
page: string, page: string,
distDir: string, distDir: string,
locales?: string[], locales: string[] | undefined,
appDirEnabled?: boolean isAppPath: boolean
): string { ): string {
const pagePath = getMaybePagePath(page, distDir, locales, appDirEnabled) const pagePath = getMaybePagePath(page, distDir, locales, isAppPath)
if (!pagePath) { if (!pagePath) {
throw new PageNotFoundError(page) throw new PageNotFoundError(page)
...@@ -109,9 +109,9 @@ export function getPagePath( ...@@ -109,9 +109,9 @@ export function getPagePath(
export function requirePage( export function requirePage(
page: string, page: string,
distDir: string, distDir: string,
appDirEnabled?: boolean isAppPath: boolean
): any { ): any {
const pagePath = getPagePath(page, distDir, undefined, appDirEnabled) const pagePath = getPagePath(page, distDir, undefined, isAppPath)
if (pagePath.endsWith('.html')) { if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8').catch((err) => { return promises.readFile(pagePath, 'utf8').catch((err) => {
throw new MissingStaticPage(page, err.message) throw new MissingStaticPage(page, err.message)
......
export default function Root({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
export default () => '(app/page.js)'
module.exports = {
experimental: {
appDir: true,
},
}
export default () => '(pages/page.js)'
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir similar pages paths',
{
files: __dirname,
skipDeployment: true,
},
({ next, isNextDev }) => {
// TODO: enable development test
if (isNextDev) {
it('should skip dev test', () => {})
return
}
it('should redirect route when requesting it directly', async () => {
const res1 = await next.fetch('/')
expect(res1.status).toBe(200)
expect(await res1.text()).toContain('(app/page.js)')
const res2 = await next.fetch('/page')
expect(res2.status).toBe(200)
expect(await res2.text()).toContain('(pages/page.js)')
})
}
)
...@@ -52,18 +52,20 @@ describe('normalizePagePath', () => { ...@@ -52,18 +52,20 @@ describe('normalizePagePath', () => {
describe('getPagePath', () => { describe('getPagePath', () => {
it('Should not append /index to the / page', () => { it('Should not append /index to the / page', () => {
expect(() => getPagePath('/', distDir)).toThrow( expect(() => getPagePath('/', distDir, undefined, false)).toThrow(
'Cannot find module for page: /' 'Cannot find module for page: /'
) )
}) })
it('Should prepend / when a page does not have it', () => { it('Should prepend / when a page does not have it', () => {
const pagePath = getPagePath('_error', distDir) const pagePath = getPagePath('_error', distDir, undefined, false)
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`)) expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
}) })
it('Should throw with paths containing ../', () => { it('Should throw with paths containing ../', () => {
expect(() => getPagePath('/../../package.json', distDir)).toThrow() expect(() =>
getPagePath('/../../package.json', distDir, undefined, false)
).toThrow()
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment