From c966f807d5239fec06d159c802b75717bccb4247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 14:55:31 +0100 Subject: [PATCH 1/6] Make sure app pages are correctly found in dev and build --- packages/next/build/index.ts | 2 +- packages/next/server/lib/find-page-file.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 685c7356b5bb43d..4e03e996ae47e7f 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -493,7 +493,7 @@ export default async function build( .traceAsyncFn(() => recursiveReadDir( appDir, - new RegExp(`page\\.(?:${config.pageExtensions.join('|')})$`) + new RegExp(`^page\\.(?:${config.pageExtensions.join('|')})$`) ) ) } diff --git a/packages/next/server/lib/find-page-file.ts b/packages/next/server/lib/find-page-file.ts index 53d2a86dd35e86d..ed306480bf02cd2 100644 --- a/packages/next/server/lib/find-page-file.ts +++ b/packages/next/server/lib/find-page-file.ts @@ -68,5 +68,5 @@ export async function findPageFile( // client, or server components with allowed page file extension. // e.g. page.js, page.server.js, page.client.tsx, etc. export function isLayoutsLeafPage(filePath: string) { - return /[\\/]?page\.((server|client)\.?)?[jt]sx?$/.test(filePath) + return /[\\/]page\.?[jt]sx?$/.test(filePath) } From 6b97775a6ea234f5d14835d548895ce8be2ffd9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 14:55:52 +0100 Subject: [PATCH 2/6] Update test --- test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js | 3 +++ test/e2e/app-dir/app/app/catch-all/[...slug]/page.js | 2 ++ test/e2e/app-dir/index.test.ts | 1 + 3 files changed, 6 insertions(+) create mode 100644 test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js diff --git a/test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js b/test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js new file mode 100644 index 000000000000000..b49da2a9eee6c65 --- /dev/null +++ b/test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js @@ -0,0 +1,3 @@ +export default function NotAPage() { + return

Not a page

+} diff --git a/test/e2e/app-dir/app/app/catch-all/[...slug]/page.js b/test/e2e/app-dir/app/app/catch-all/[...slug]/page.js index d5361f48d544757..55ba1e455951799 100644 --- a/test/e2e/app-dir/app/app/catch-all/[...slug]/page.js +++ b/test/e2e/app-dir/app/app/catch-all/[...slug]/page.js @@ -1,4 +1,5 @@ import Widget from './components/widget' +import NotAPage from './not-a-page' export default function Page({ params }) { return ( @@ -7,6 +8,7 @@ export default function Page({ params }) { hello from /catch-all/{params.slug.join('/')} + ) } diff --git a/test/e2e/app-dir/index.test.ts b/test/e2e/app-dir/index.test.ts index 14a1da1c295dafd..6af1d4603ae2978 100644 --- a/test/e2e/app-dir/index.test.ts +++ b/test/e2e/app-dir/index.test.ts @@ -726,6 +726,7 @@ describe('app dir', () => { const html = await renderViaHTTP(next.url, `/catch-all/${route}`) const $ = cheerio.load(html) expect($('#text').attr('data-params')).toBe(route) + expect($('#not-a-page').text()).toBe('Not a page') // Components under catch-all should not be treated as route that errors during build. // They should be rendered properly when imported in page route. From 64a7311d8e3db07d7cb9b91c2cde9bafe479f85b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 15:25:58 +0100 Subject: [PATCH 3/6] Make sure page without a dash is found --- packages/next/server/lib/find-page-file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/server/lib/find-page-file.ts b/packages/next/server/lib/find-page-file.ts index ed306480bf02cd2..cf32f11a59da691 100644 --- a/packages/next/server/lib/find-page-file.ts +++ b/packages/next/server/lib/find-page-file.ts @@ -68,5 +68,5 @@ export async function findPageFile( // client, or server components with allowed page file extension. // e.g. page.js, page.server.js, page.client.tsx, etc. export function isLayoutsLeafPage(filePath: string) { - return /[\\/]page\.?[jt]sx?$/.test(filePath) + return /(^page|\/page)\.?[jt]sx?$/.test(filePath) } From 42c679c81c1e9fd046919e99382a9578080369c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 15:26:09 +0100 Subject: [PATCH 4/6] Update unit test --- test/unit/find-page-file.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unit/find-page-file.test.ts b/test/unit/find-page-file.test.ts index b59853a41017d34..ddc6b0e8b3fd008 100644 --- a/test/unit/find-page-file.test.ts +++ b/test/unit/find-page-file.test.ts @@ -48,13 +48,15 @@ describe('findPageFile', () => { describe('isLayoutsLeafPage', () => { it('should determine either server or client component page file as leaf node page', () => { expect(isLayoutsLeafPage('page.js')).toBe(true) - expect(isLayoutsLeafPage('./page.server.js')).toBe(true) - expect(isLayoutsLeafPage('./page.server.jsx')).toBe(true) - expect(isLayoutsLeafPage('./page.client.ts')).toBe(true) - expect(isLayoutsLeafPage('./page.client.tsx')).toBe(true) + expect(isLayoutsLeafPage('./page.js')).toBe(true) + expect(isLayoutsLeafPage('./page.jsx')).toBe(true) + expect(isLayoutsLeafPage('/page.ts')).toBe(true) + expect(isLayoutsLeafPage('/path/page.tsx')).toBe(true) }) it('should determine other files under layout routes as non leaf node', () => { + expect(isLayoutsLeafPage('./not-a-page.js')).toBe(false) + expect(isLayoutsLeafPage('not-a-page.js')).toBe(false) expect(isLayoutsLeafPage('./page.component.jsx')).toBe(false) expect(isLayoutsLeafPage('layout.js')).toBe(false) }) From f00fa3b21c06480362b757d2978b684ab1df4a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 15:41:09 +0100 Subject: [PATCH 5/6] Use page extensions in dev as well --- packages/next/server/dev/next-dev-server.ts | 2 +- packages/next/server/lib/find-page-file.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/next/server/dev/next-dev-server.ts b/packages/next/server/dev/next-dev-server.ts index 0a1f86351132e59..389f283c367fa90 100644 --- a/packages/next/server/dev/next-dev-server.ts +++ b/packages/next/server/dev/next-dev-server.ts @@ -405,7 +405,7 @@ export default class DevServer extends Server { }) if (isAppPath) { - if (!isLayoutsLeafPage(fileName)) { + if (!isLayoutsLeafPage(fileName, this.nextConfig.pageExtensions)) { continue } diff --git a/packages/next/server/lib/find-page-file.ts b/packages/next/server/lib/find-page-file.ts index cf32f11a59da691..0b573f475166a2d 100644 --- a/packages/next/server/lib/find-page-file.ts +++ b/packages/next/server/lib/find-page-file.ts @@ -64,9 +64,9 @@ export async function findPageFile( } // Determine if the file is leaf node page file under layouts, -// The filename should start with 'page', it can be either shared, -// client, or server components with allowed page file extension. -// e.g. page.js, page.server.js, page.client.tsx, etc. -export function isLayoutsLeafPage(filePath: string) { - return /(^page|\/page)\.?[jt]sx?$/.test(filePath) +// The filename should start with 'page' and end with one of the allowed extensions +export function isLayoutsLeafPage(filePath: string, pageExtensions: string[]) { + return new RegExp(`(^page|/page)\\.(?:${pageExtensions.join('|')})$`).test( + filePath + ) } From 833579700457d13afa066606da70bd75d89328ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Born=C3=B6?= Date: Wed, 16 Nov 2022 15:41:21 +0100 Subject: [PATCH 6/6] Update unit test --- test/unit/find-page-file.test.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/unit/find-page-file.test.ts b/test/unit/find-page-file.test.ts index ddc6b0e8b3fd008..d49fb4347bd978d 100644 --- a/test/unit/find-page-file.test.ts +++ b/test/unit/find-page-file.test.ts @@ -46,18 +46,22 @@ describe('findPageFile', () => { }) describe('isLayoutsLeafPage', () => { + const pageExtensions = ['tsx', 'ts', 'jsx', 'js'] it('should determine either server or client component page file as leaf node page', () => { - expect(isLayoutsLeafPage('page.js')).toBe(true) - expect(isLayoutsLeafPage('./page.js')).toBe(true) - expect(isLayoutsLeafPage('./page.jsx')).toBe(true) - expect(isLayoutsLeafPage('/page.ts')).toBe(true) - expect(isLayoutsLeafPage('/path/page.tsx')).toBe(true) + expect(isLayoutsLeafPage('page.js', pageExtensions)).toBe(true) + expect(isLayoutsLeafPage('./page.js', pageExtensions)).toBe(true) + expect(isLayoutsLeafPage('./page.jsx', pageExtensions)).toBe(true) + expect(isLayoutsLeafPage('/page.ts', pageExtensions)).toBe(true) + expect(isLayoutsLeafPage('/path/page.tsx', pageExtensions)).toBe(true) }) it('should determine other files under layout routes as non leaf node', () => { - expect(isLayoutsLeafPage('./not-a-page.js')).toBe(false) - expect(isLayoutsLeafPage('not-a-page.js')).toBe(false) - expect(isLayoutsLeafPage('./page.component.jsx')).toBe(false) - expect(isLayoutsLeafPage('layout.js')).toBe(false) + expect(isLayoutsLeafPage('./not-a-page.js', pageExtensions)).toBe(false) + expect(isLayoutsLeafPage('not-a-page.js', pageExtensions)).toBe(false) + expect(isLayoutsLeafPage('./page.component.jsx', pageExtensions)).toBe( + false + ) + expect(isLayoutsLeafPage('layout.js', pageExtensions)).toBe(false) + expect(isLayoutsLeafPage('page', pageExtensions)).toBe(false) }) })