Commit ee076125 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Error on query values in exportPathMap for auto export page (#9908)

* Add warning when providing query values in exportPathMap for auto-export page

* Update test for SSG page also

* Update to error instead of warn

* Update warn -> Error and check exit code
parent 22e015f7
......@@ -37,12 +37,24 @@ export default async function({
}
try {
let { query = {} } = pathMap
const { query: originalQuery = {} } = pathMap
const { page } = pathMap
const filePath = path === '/' ? '/index' : path
const ampPath = `${filePath}.amp`
let query = { ...originalQuery }
let params
// We need to show a warning if they try to provide query values
// for an auto-exported page since they won't be available
const hasOrigQueryValues = Object.keys(originalQuery).length > 0
const queryWithAutoExportWarn = () => {
if (hasOrigQueryValues) {
throw new Error(
`\nError: you provided query values for ${path} which is an auto-exported page. These can not be applied since the page can no longer be re-rendered on the server. To disable auto-export for this page add \`getInitialProps\`\n`
)
}
}
// Check if the page is a specified dynamic route
if (isDynamicRoute(page) && page !== path) {
params = getRouteMatcher(getRouteRegex(page))(path)
......@@ -130,6 +142,7 @@ export default async function({
// if it was auto-exported the HTML is loaded here
if (typeof mod === 'string') {
html = mod
queryWithAutoExportWarn()
} else {
// for non-dynamic SSG pages we should have already
// prerendered the file
......@@ -176,6 +189,7 @@ export default async function({
if (typeof components.Component === 'string') {
html = components.Component
queryWithAutoExportWarn()
} else {
curRenderOpts = { ...components, ...renderOpts, ampPath }
html = await renderMethod(req, res, page, query, curRenderOpts)
......
module.exports = {
// target: 'serverless',
exportPathMap() {
return {
'/': { page: '/hello', query: { first: 'second' } },
'/amp': { page: '/amp' },
'/ssr': { page: '/ssr' },
}
},
}
export const config = { amp: 'hybrid' }
export default () => 'hi from hybrid AMP'
const Page = () => "I'm SSRed"
export async function getStaticProps() {
return {
props: {
hello: 'world',
},
}
}
export default Page
const Page = () => "I'm SSRed"
Page.getInitialProps = () => ({ hello: 'world' })
export default Page
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild, nextExport } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
const outdir = path.join(__dirname, 'out')
const nextConfig = path.join(appDir, 'next.config.js')
let stderr
let exitCode
const runTests = () => {
it('should show warning for query provided for auto exported page correctly', async () => {
expect(exitCode).toBe(1)
expect(stderr).toContain(
'Error: you provided query values for / which is an auto-exported page. These can not be applied since the page can no longer be re-rendered on the server. To disable auto-export for this page add `getInitialProps`'
)
expect(stderr).not.toContain('/amp')
expect(stderr).not.toContain('/ssr')
expect(stderr).not.toContain('/ssg')
expect(stderr).not.toContain('/hello')
})
}
let origNextConfig
describe('Auto Export', () => {
describe('server mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
const { stderr: curStderr, code: curCode } = await nextExport(
appDir,
{ outdir },
{ stderr: true }
)
stderr = curStderr
exitCode = curCode
})
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await nextBuild(appDir)
const { stderr: curStderr, code: curCode } = await nextExport(
appDir,
{ outdir },
{ stderr: true }
)
stderr = curStderr
exitCode = curCode
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
})
runTests()
})
})
......@@ -111,8 +111,9 @@ export function runNextCommand(argv, options = {}) {
})
}
instance.on('close', () => {
instance.on('close', code => {
resolve({
code,
stdout: stdoutOutput,
stderr: stderrOutput,
})
......
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