Skip to content

Commit

Permalink
Add warning when reserved pages are nested (vercel#13449)
Browse files Browse the repository at this point in the history
This adds a warning when a user has nested a reserved page `_error`, `_app`, or `_document` in their pages directory since it causes it to not be picked up and used by Next.js in the expected way

x-ref: vercel#13425

<details>
<summary>Example of warning</summary>

<img width="927" alt="Screen Shot 2020-05-27 at 10 39 09" src="https://user-images.githubusercontent.com/22380829/83042315-24276c00-a007-11ea-9dc4-cabcc93571d2.png">
</details>
  • Loading branch information
ijjk authored May 27, 2020
1 parent c731d59 commit e261935
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
15 changes: 15 additions & 0 deletions errors/nested-reserved-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Nested Reserved Page

#### Why This Error Occurred

In your pages folder you nested a reserved page e.g. `_app`, `_error`, or `_document` which causes the page to not be used since they must be located directly under the pages folder.

#### Possible Ways to Fix It

Move the reserved pages directly under your pages folder so that they are picked up and used correctly.

### Useful Links

- [Custom `_app` Documentation](https://nextjs.org/docs/advanced-features/custom-app)
- [Custom `_error` Documentation](https://nextjs.org/docs/advanced-features/custom-error-page)
- [Custom `_document` Documentation](https://nextjs.org/docs/advanced-features/custom-document)
20 changes: 20 additions & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ export default async function build(dir: string, conf = null): Promise<void> {
)
}

const nestedReservedPages = pageKeys.filter((page) => {
return (
page.match(/\/(_app|_document|_error)$/) && path.dirname(page) !== '/'
)
})

if (nestedReservedPages.length) {
console.warn(
'\n' +
chalk.bold.yellow(`Warning: `) +
chalk.bold(
`The following reserved Next.js pages were detected not directly under the pages directory:\n`
) +
nestedReservedPages.join('\n') +
chalk.bold(
`\nSee more info here: https://err.sh/next.js/nested-reserved-page\n`
)
)
}

const buildCustomRoute = (
r: {
source: string
Expand Down
8 changes: 7 additions & 1 deletion test/integration/auto-export-error-bail/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ const runTests = () => {
console.log(output)
}

const combinedOutput = output.stderr + output.stdout

expect(output.code).toBe(0)
expect(output.stderr + output.stdout).not.toContain(
expect(combinedOutput).not.toContain(
'You have opted-out of Automatic Static Optimization due to'
)
expect(combinedOutput).toContain(
'The following reserved Next.js pages were detected not directly under the pages directory'
)
expect(combinedOutput).toContain('/app/_error')
})
}

Expand Down

0 comments on commit e261935

Please sign in to comment.