Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App files ending with page registred as page files #42996

Merged
merged 7 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/index.ts
Expand Up @@ -493,7 +493,7 @@ export default async function build(
.traceAsyncFn(() =>
recursiveReadDir(
appDir,
new RegExp(`page\\.(?:${config.pageExtensions.join('|')})$`)
new RegExp(`^page\\.(?:${config.pageExtensions.join('|')})$`)
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/dev/next-dev-server.ts
Expand Up @@ -405,7 +405,7 @@ export default class DevServer extends Server {
})

if (isAppPath) {
if (!isLayoutsLeafPage(fileName)) {
if (!isLayoutsLeafPage(fileName, this.nextConfig.pageExtensions)) {
continue
}

Expand Down
10 changes: 5 additions & 5 deletions packages/next/server/lib/find-page-file.ts
Expand Up @@ -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\.((server|client)\.?)?[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
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/app/catch-all/[...slug]/not-a-page.js
@@ -0,0 +1,3 @@
export default function NotAPage() {
return <h1 id="not-a-page">Not a page</h1>
}
2 changes: 2 additions & 0 deletions 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 (
Expand All @@ -7,6 +8,7 @@ export default function Page({ params }) {
hello from /catch-all/{params.slug.join('/')}
</h1>
<Widget />
<NotAPage />
</>
)
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -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.
Expand Down
20 changes: 13 additions & 7 deletions test/unit/find-page-file.test.ts
Expand Up @@ -46,16 +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.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', 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('./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)
})
})