Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/canary' into add/rewrite-proxying
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 17, 2020
2 parents ba41ec6 + ee07612 commit 30e309f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 2 deletions.
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

0 comments on commit 30e309f

Please sign in to comment.