Skip to content

Commit

Permalink
Add handling for invalid page config (vercel#7921)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Jul 15, 2019
1 parent e744085 commit 86b6a4b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions errors/invalid-page-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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)
12 changes: 12 additions & 0 deletions packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ export default function nextPageConfig({
for (const declaration of declarations) {
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) {
const { name } = prop.key
if (configKeys.has(name)) {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/page-config/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { config as hello } from '../something'
import { config as world } from '../config'

// export const config = 'hello world'

export default () => (
<p>
{hello} {world}
Expand Down
18 changes: 18 additions & 0 deletions test/integration/page-config/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'

const appDir = join(__dirname, '..')
const indexPage = join(appDir, 'pages/index.js')

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

describe('Page Config', () => {
it('builds without error when export const config is used outside page', async () => {
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
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')
}
})
})

0 comments on commit 86b6a4b

Please sign in to comment.