Skip to content

Commit

Permalink
Fix page folder being wrongly resolved as page file (#42348)
Browse files Browse the repository at this point in the history
Closes #42010.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] 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)
  • Loading branch information
shuding committed Nov 2, 2022
1 parent 468f2c4 commit b69dac1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/next/build/webpack/loaders/next-app-loader.ts
Expand Up @@ -17,6 +17,8 @@ export const FILE_TYPES = {
'not-found': 'not-found',
} as const

const PAGE_SEGMENT = 'page$'

// TODO-APP: check if this can be narrowed.
type ComponentModule = () => any
export type ComponentsType = {
Expand Down Expand Up @@ -59,10 +61,8 @@ async function createTreeCodeFromPath({
}

for (const [parallelKey, parallelSegment] of parallelSegments) {
const parallelSegmentPath = segmentPath + '/' + parallelSegment

if (parallelSegment === 'page') {
const matchedPagePath = `${appDirPrefix}${parallelSegmentPath}`
if (parallelSegment === PAGE_SEGMENT) {
const matchedPagePath = `${appDirPrefix}${segmentPath}/page`
const resolvedPagePath = await resolve(matchedPagePath)
if (resolvedPagePath) pages.push(resolvedPagePath)

Expand All @@ -73,6 +73,7 @@ async function createTreeCodeFromPath({
continue
}

const parallelSegmentPath = segmentPath + '/' + parallelSegment
const subtree = await createSubtreePropsFromSegmentPath([
...segments,
parallelSegment,
Expand Down Expand Up @@ -175,12 +176,18 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
const matched: Record<string, string> = {}
for (const path of normalizedAppPaths) {
if (path.startsWith(pathname + '/')) {
const restPath = path.slice(pathname.length + 1)
const rest = path.slice(pathname.length + 1).split('/')

let matchedSegment = rest[0]
// It is the actual page, mark it sepcially.
if (rest.length === 1 && matchedSegment === 'page') {
matchedSegment = PAGE_SEGMENT
}

const matchedSegment = restPath.split('/')[0]
const matchedKey = matchedSegment.startsWith('@')
? matchedSegment.slice(1)
: 'children'

matched[matchedKey] = matchedSegment
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/app/app/dashboard/page/page.jsx
@@ -0,0 +1,7 @@
export default function DashboardPagePage() {
return (
<>
<p>hello dashboard/page!</p>
</>
)
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -292,6 +292,11 @@ describe('app dir', () => {
)
})

it('should serve page as a segment name correctly', async () => {
const html = await renderViaHTTP(next.url, '/dashboard/page')
expect(html).toContain('hello dashboard/page!')
})

it('should include document html and body', async () => {
const html = await renderViaHTTP(next.url, '/dashboard')
const $ = cheerio.load(html)
Expand Down

0 comments on commit b69dac1

Please sign in to comment.