Unverified Commit 86b6a4b6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add handling for invalid page config (#7921)

parent e7440858
# Invalid Page Config
#### Why This Error Occurred
In one of your pages you did `export const config` with an invalid value.
#### Possible Ways to Fix It
The page's config must be an object initialized directly when being exported.
This is not allowed
```js
export const config = 'hello world'
```
This is allowed
```js
export const config = { amp: true }
```
### Useful Links
- [Enabling AMP Support](https://github.com/zeit/next.js/#enabling-amp-support)
- [API Middlewares](https://github.com/zeit/next.js/#api-middlewares)
...@@ -60,6 +60,18 @@ export default function nextPageConfig({ ...@@ -60,6 +60,18 @@ export default function nextPageConfig({
for (const declaration of declarations) { for (const declaration of declarations) {
if (declaration.id.name !== 'config') continue if (declaration.id.name !== 'config') continue
if (declaration.init.type !== 'ObjectExpression') {
const pageName =
(state.filename || '').split(state.cwd || '').pop() ||
'unknown'
throw new Error(
`Invalid page config export found. Expected object but got ${
declaration.init.type
} in file ${pageName}. See: https://err.sh/zeit/next.js/invalid-page-config`
)
}
for (const prop of declaration.init.properties) { for (const prop of declaration.init.properties) {
const { name } = prop.key const { name } = prop.key
if (configKeys.has(name)) { if (configKeys.has(name)) {
......
import { config as hello } from '../something' import { config as hello } from '../something'
import { config as world } from '../config' import { config as world } from '../config'
// export const config = 'hello world'
export default () => ( export default () => (
<p> <p>
{hello} {world} {hello} {world}
......
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { nextBuild } from 'next-test-utils' import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const indexPage = join(appDir, 'pages/index.js')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
describe('Page Config', () => { describe('Page Config', () => {
...@@ -11,4 +14,19 @@ describe('Page Config', () => { ...@@ -11,4 +14,19 @@ describe('Page Config', () => {
const { stderr } = await nextBuild(appDir, undefined, { stderr: true }) const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).not.toMatch(/Failed to compile\./) expect(stderr).not.toMatch(/Failed to compile\./)
}) })
it('shows valid error on invalid page config', async () => {
const origContent = await fs.readFile(indexPage, 'utf8')
const newContent = origContent.replace('// export', 'export')
await fs.writeFile(indexPage, newContent, 'utf8')
try {
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(
/https:\/\/err\.sh\/zeit\/next\.js\/invalid-page-config/
)
} finally {
await fs.writeFile(indexPage, origContent, 'utf8')
}
})
}) })
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