Unverified Commit fdc07c00 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

App files ending with page registred as page files (#42996)

When building (`next/build/index.ts`) files ending with `page.js` got
picked up by the RegEx which meant files like `not-a-page.js` ended up
as a page. This change makes sure the segment actually starts with
`page`.

Dev had the same issue and also missed taking `pageExtensions` into
account.

Fixes #42972

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent ec07e309
...@@ -493,7 +493,7 @@ export default async function build( ...@@ -493,7 +493,7 @@ export default async function build(
.traceAsyncFn(() => .traceAsyncFn(() =>
recursiveReadDir( recursiveReadDir(
appDir, appDir,
new RegExp(`page\\.(?:${config.pageExtensions.join('|')})$`) new RegExp(`^page\\.(?:${config.pageExtensions.join('|')})$`)
) )
) )
} }
......
...@@ -405,7 +405,7 @@ export default class DevServer extends Server { ...@@ -405,7 +405,7 @@ export default class DevServer extends Server {
}) })
if (isAppPath) { if (isAppPath) {
if (!isLayoutsLeafPage(fileName)) { if (!isLayoutsLeafPage(fileName, this.nextConfig.pageExtensions)) {
continue continue
} }
......
...@@ -64,9 +64,9 @@ export async function findPageFile( ...@@ -64,9 +64,9 @@ export async function findPageFile(
} }
// Determine if the file is leaf node page file under layouts, // Determine if the file is leaf node page file under layouts,
// The filename should start with 'page', it can be either shared, // The filename should start with 'page' and end with one of the allowed extensions
// client, or server components with allowed page file extension. export function isLayoutsLeafPage(filePath: string, pageExtensions: string[]) {
// e.g. page.js, page.server.js, page.client.tsx, etc. return new RegExp(`(^page|/page)\\.(?:${pageExtensions.join('|')})$`).test(
export function isLayoutsLeafPage(filePath: string) { filePath
return /[\\/]?page\.((server|client)\.?)?[jt]sx?$/.test(filePath) )
} }
export default function NotAPage() {
return <h1 id="not-a-page">Not a page</h1>
}
import Widget from './components/widget' import Widget from './components/widget'
import NotAPage from './not-a-page'
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
...@@ -7,6 +8,7 @@ export default function Page({ params }) { ...@@ -7,6 +8,7 @@ export default function Page({ params }) {
hello from /catch-all/{params.slug.join('/')} hello from /catch-all/{params.slug.join('/')}
</h1> </h1>
<Widget /> <Widget />
<NotAPage />
</> </>
) )
} }
...@@ -726,6 +726,7 @@ describe('app dir', () => { ...@@ -726,6 +726,7 @@ describe('app dir', () => {
const html = await renderViaHTTP(next.url, `/catch-all/${route}`) const html = await renderViaHTTP(next.url, `/catch-all/${route}`)
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#text').attr('data-params')).toBe(route) 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. // Components under catch-all should not be treated as route that errors during build.
// They should be rendered properly when imported in page route. // They should be rendered properly when imported in page route.
......
...@@ -46,16 +46,22 @@ describe('findPageFile', () => { ...@@ -46,16 +46,22 @@ describe('findPageFile', () => {
}) })
describe('isLayoutsLeafPage', () => { describe('isLayoutsLeafPage', () => {
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
it('should determine either server or client component page file as leaf node page', () => { it('should determine either server or client component page file as leaf node page', () => {
expect(isLayoutsLeafPage('page.js')).toBe(true) expect(isLayoutsLeafPage('page.js', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.server.js')).toBe(true) expect(isLayoutsLeafPage('./page.js', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.server.jsx')).toBe(true) expect(isLayoutsLeafPage('./page.jsx', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.client.ts')).toBe(true) expect(isLayoutsLeafPage('/page.ts', pageExtensions)).toBe(true)
expect(isLayoutsLeafPage('./page.client.tsx')).toBe(true) expect(isLayoutsLeafPage('/path/page.tsx', pageExtensions)).toBe(true)
}) })
it('should determine other files under layout routes as non leaf node', () => { it('should determine other files under layout routes as non leaf node', () => {
expect(isLayoutsLeafPage('./page.component.jsx')).toBe(false) expect(isLayoutsLeafPage('./not-a-page.js', pageExtensions)).toBe(false)
expect(isLayoutsLeafPage('layout.js')).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)
}) })
}) })
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