Unverified Commit aa4b87e3 authored by Kevin Mårtensson's avatar Kevin Mårtensson Committed by GitHub

Handle cases where `config` is exported after its declaration (#16211)

AMP will still not work correctly when switching between non-AMP and AMP pages in development mode because of https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-page-config.ts#L116-L120.

Fixes #15704.
parent f8534a6e
...@@ -6,7 +6,7 @@ In one of your pages you did `export const config` with an invalid value. ...@@ -6,7 +6,7 @@ In one of your pages you did `export const config` with an invalid value.
#### Possible Ways to Fix It #### Possible Ways to Fix It
The page's config must be an object initialized directly when being exported. The page's config must be an object initialized directly when being exported and not modified dynamically.
This is not allowed This is not allowed
...@@ -14,6 +14,19 @@ This is not allowed ...@@ -14,6 +14,19 @@ This is not allowed
export const config = 'hello world' export const config = 'hello world'
``` ```
This is not allowed
```js
const config = {}
config.amp = true
```
This is not allowed
```js
export { config } from '../config'
```
This is allowed This is allowed
```js ```js
......
...@@ -2,6 +2,8 @@ import { NodePath, PluginObj, types as BabelTypes } from '@babel/core' ...@@ -2,6 +2,8 @@ import { NodePath, PluginObj, types as BabelTypes } from '@babel/core'
import { PageConfig } from 'next/types' import { PageConfig } from 'next/types'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants' import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants'
const CONFIG_KEY = 'config'
// replace program path with just a variable with the drop identifier // replace program path with just a variable with the drop identifier
function replaceBundle(path: any, t: typeof BabelTypes): void { function replaceBundle(path: any, t: typeof BabelTypes): void {
path.parentPath.replaceWith( path.parentPath.replaceWith(
...@@ -41,27 +43,59 @@ export default function nextPageConfig({ ...@@ -41,27 +43,59 @@ export default function nextPageConfig({
enter(path, state: ConfigState) { enter(path, state: ConfigState) {
path.traverse( path.traverse(
{ {
ExportDeclaration(exportPath, exportState) {
if (
BabelTypes.isExportNamedDeclaration(exportPath) &&
(exportPath.node as BabelTypes.ExportNamedDeclaration).specifiers?.some(
(specifier) => {
return specifier.exported.name === CONFIG_KEY
}
) &&
BabelTypes.isStringLiteral(
(exportPath.node as BabelTypes.ExportNamedDeclaration)
.source
)
) {
throw new Error(
errorMessage(
exportState,
'Expected object but got export from'
)
)
}
},
ExportNamedDeclaration( ExportNamedDeclaration(
exportPath: NodePath<BabelTypes.ExportNamedDeclaration>, exportPath: NodePath<BabelTypes.ExportNamedDeclaration>,
exportState: any exportState: any
) { ) {
if (exportState.bundleDropped || !exportPath.node.declaration) {
return
}
if ( if (
!BabelTypes.isVariableDeclaration(exportPath.node.declaration) exportState.bundleDropped ||
(!exportPath.node.declaration &&
exportPath.node.specifiers.length === 0)
) { ) {
return return
} }
const { declarations } = exportPath.node.declaration
const config: PageConfig = {} const config: PageConfig = {}
const declarations = [
...(exportPath.node.declaration?.declarations || []),
exportPath.scope.getBinding(CONFIG_KEY)?.path.node,
].filter(Boolean)
for (const declaration of declarations) { for (const declaration of declarations) {
if ( if (
!BabelTypes.isIdentifier(declaration.id, { name: 'config' }) !BabelTypes.isIdentifier(declaration.id, {
name: CONFIG_KEY,
})
) { ) {
if (BabelTypes.isImportSpecifier(declaration)) {
throw new Error(
errorMessage(
exportState,
`Expected object but got import`
)
)
}
continue continue
} }
......
...@@ -12,6 +12,11 @@ export default function Nav() { ...@@ -12,6 +12,11 @@ export default function Nav() {
<a id="only-amp-link">AMP First Page</a> <a id="only-amp-link">AMP First Page</a>
</Link> </Link>
</li> </li>
<li>
<Link href="/var-before-export">
<a id="var-before-export-link">Another AMP First Page</a>
</Link>
</li>
</ul> </ul>
) )
} }
const config = { amp: true }
const Page = () => {
return (
<div>
<p id="var-before-export">Only AMP for me...</p>
</div>
)
}
export default Page
export { config }
...@@ -74,6 +74,15 @@ describe('AMP Usage', () => { ...@@ -74,6 +74,15 @@ describe('AMP Usage', () => {
expect(result).toBe(null) expect(result).toBe(null)
}) })
it('should not output client pages for AMP only with config exported after declaration', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.elementByCss('#var-before-export-link').click()
const result = await browser.eval('window.NAV_PAGE_LOADED')
expect(result).toBe(null)
})
it('should add link preload for amp script', async () => { it('should add link preload for amp script', async () => {
const html = await renderViaHTTP(appPort, '/?amp=1') const html = await renderViaHTTP(appPort, '/?amp=1')
await validateAMP(html) await validateAMP(html)
...@@ -267,6 +276,15 @@ describe('AMP Usage', () => { ...@@ -267,6 +276,15 @@ describe('AMP Usage', () => {
expect(result).toBe(null) expect(result).toBe(null)
}) })
it('should not output client pages for AMP only with config exported after declaration', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.elementByCss('#var-before-export-link').click()
const result = await browser.eval('window.NAV_PAGE_LOADED')
expect(result).toBe(null)
})
}) })
describe('AMP dev no-warn', () => { describe('AMP dev no-warn', () => {
......
// export { config } from '../../config'
export default () => <p>hello world</p>
// eslint-disable-next-line no-unused-vars
import { config } from '../../config'
// export { config }
export default () => <p>hello world</p>
...@@ -2,4 +2,4 @@ const props = { amp: true } ...@@ -2,4 +2,4 @@ const props = { amp: true }
// export const config = { ...props } // export const config = { ...props }
export default () => <p>{props}</p> export default () => <p>{JSON.stringify(props)}</p>
...@@ -88,4 +88,30 @@ describe('Page Config', () => { ...@@ -88,4 +88,30 @@ describe('Page Config', () => {
await reset() await reset()
} }
}) })
it('shows error when page config is export from', async () => {
const reset = await uncommentExport('invalid/export-from.js')
try {
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(
/https:\/\/err\.sh\/vercel\/next\.js\/invalid-page-config/
)
} finally {
await reset()
}
})
it('shows error when page config is imported and exported', async () => {
const reset = await uncommentExport('invalid/import-export.js')
try {
const { stderr } = await nextBuild(appDir, undefined, { stderr: true })
expect(stderr).toMatch(
/https:\/\/err\.sh\/vercel\/next\.js\/invalid-page-config/
)
} finally {
await reset()
}
})
}) })
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