Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/next/export/worker.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/integration/auto-export-query-error/next.config.js
@@ -0,0 +1,10 @@
module.exports = {
// target: 'serverless',
exportPathMap() {
return {
'/': { page: '/hello', query: { first: 'second' } },
'/amp': { page: '/amp' },
'/ssr': { page: '/ssr' },
}
},
}
3 changes: 3 additions & 0 deletions test/integration/auto-export-query-error/pages/amp.js
@@ -0,0 +1,3 @@
export const config = { amp: 'hybrid' }

export default () => 'hi from hybrid AMP'
1 change: 1 addition & 0 deletions test/integration/auto-export-query-error/pages/hello.js
@@ -0,0 +1 @@
export default () => 'hi'
11 changes: 11 additions & 0 deletions test/integration/auto-export-query-error/pages/ssg.js
@@ -0,0 +1,11 @@
const Page = () => "I'm SSRed"

export async function getStaticProps() {
return {
props: {
hello: 'world',
},
}
}

export default Page
5 changes: 5 additions & 0 deletions test/integration/auto-export-query-error/pages/ssr.js
@@ -0,0 +1,5 @@
const Page = () => "I'm SSRed"

Page.getInitialProps = () => ({ hello: 'world' })

export default Page
64 changes: 64 additions & 0 deletions test/integration/auto-export-query-error/test/index.test.js
@@ -0,0 +1,64 @@
/* 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()
})
})
3 changes: 2 additions & 1 deletion test/lib/next-test-utils.js
Expand Up @@ -111,8 +111,9 @@ export function runNextCommand(argv, options = {}) {
})
}

instance.on('close', () => {
instance.on('close', code => {
resolve({
code,
stdout: stdoutOutput,
stderr: stderrOutput,
})
Expand Down