Unverified Commit 38943204 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update check for fallback pages during export (#33323)

This fixes our check for fallback pages during `next export` as we are currently erroring even when the erroneous pages are not included in the `exportPathMap`. This also adds additional tests to certify the expected behavior for the error. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/29135
parent aaa77dd7
...@@ -448,14 +448,12 @@ export default async function exportApp( ...@@ -448,14 +448,12 @@ export default async function exportApp(
if (prerenderManifest && !options.buildExport) { if (prerenderManifest && !options.buildExport) {
const fallbackEnabledPages = new Set() const fallbackEnabledPages = new Set()
for (const key of Object.keys(prerenderManifest.dynamicRoutes)) { for (const path of Object.keys(exportPathMap)) {
// only error if page is included in path map const page = exportPathMap[path].page
if (!exportPathMap[key] && !excludedPrerenderRoutes.has(key)) { const prerenderInfo = prerenderManifest.dynamicRoutes[page]
continue
}
if (prerenderManifest.dynamicRoutes[key].fallback !== false) { if (prerenderInfo && prerenderInfo.fallback !== false) {
fallbackEnabledPages.add(key) fallbackEnabledPages.add(page)
} }
} }
......
module.exports = {
exportPathMap() {
return {
'/first': { page: '/[slug]' },
}
},
}
...@@ -148,6 +148,9 @@ export class NextInstance { ...@@ -148,6 +148,9 @@ export class NextInstance {
} }
} }
public async export(): Promise<{ exitCode?: number; cliOutput?: string }> {
return {}
}
public async setup(): Promise<void> {} public async setup(): Promise<void> {}
public async start(): Promise<void> {} public async start(): Promise<void> {}
public async stop(): Promise<void> { public async stop(): Promise<void> {
......
...@@ -6,6 +6,7 @@ import { spawn, SpawnOptions } from 'child_process' ...@@ -6,6 +6,7 @@ import { spawn, SpawnOptions } from 'child_process'
export class NextStartInstance extends NextInstance { export class NextStartInstance extends NextInstance {
private _buildId: string private _buildId: string
private _cliOutput: string private _cliOutput: string
private spawnOpts: SpawnOptions
public get buildId() { public get buildId() {
return this._buildId return this._buildId
...@@ -19,11 +20,26 @@ export class NextStartInstance extends NextInstance { ...@@ -19,11 +20,26 @@ export class NextStartInstance extends NextInstance {
await super.createTestDir() await super.createTestDir()
} }
private handleStdio = (childProcess) => {
childProcess.stdout.on('data', (chunk) => {
const msg = chunk.toString()
process.stdout.write(chunk)
this._cliOutput += msg
this.emit('stdout', [msg])
})
childProcess.stderr.on('data', (chunk) => {
const msg = chunk.toString()
process.stderr.write(chunk)
this._cliOutput += msg
this.emit('stderr', [msg])
})
}
public async start() { public async start() {
if (this.childProcess) { if (this.childProcess) {
throw new Error('next already started') throw new Error('next already started')
} }
const spawnOpts: SpawnOptions = { this.spawnOpts = {
cwd: this.testDir, cwd: this.testDir,
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
shell: false, shell: false,
...@@ -34,20 +50,6 @@ export class NextStartInstance extends NextInstance { ...@@ -34,20 +50,6 @@ export class NextStartInstance extends NextInstance {
__NEXT_RAND_PORT: '1', __NEXT_RAND_PORT: '1',
}, },
} }
const handleStdio = () => {
this.childProcess.stdout.on('data', (chunk) => {
const msg = chunk.toString()
process.stdout.write(chunk)
this._cliOutput += msg
this.emit('stdout', [msg])
})
this.childProcess.stderr.on('data', (chunk) => {
const msg = chunk.toString()
process.stderr.write(chunk)
this._cliOutput += msg
this.emit('stderr', [msg])
})
}
let buildArgs = ['yarn', 'next', 'build'] let buildArgs = ['yarn', 'next', 'build']
let startArgs = ['yarn', 'next', 'start'] let startArgs = ['yarn', 'next', 'start']
...@@ -60,8 +62,12 @@ export class NextStartInstance extends NextInstance { ...@@ -60,8 +62,12 @@ export class NextStartInstance extends NextInstance {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
console.log('running', buildArgs.join(' ')) console.log('running', buildArgs.join(' '))
this.childProcess = spawn(buildArgs[0], buildArgs.slice(1), spawnOpts) this.childProcess = spawn(
handleStdio() buildArgs[0],
buildArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess)
this.childProcess.on('exit', (code, signal) => { this.childProcess.on('exit', (code, signal) => {
if (code || signal) if (code || signal)
reject( reject(
...@@ -85,8 +91,12 @@ export class NextStartInstance extends NextInstance { ...@@ -85,8 +91,12 @@ export class NextStartInstance extends NextInstance {
console.log('running', startArgs.join(' ')) console.log('running', startArgs.join(' '))
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
this.childProcess = spawn(startArgs[0], startArgs.slice(1), spawnOpts) this.childProcess = spawn(
handleStdio() startArgs[0],
startArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess)
this.childProcess.on('close', (code, signal) => { this.childProcess.on('close', (code, signal) => {
if (this.isStopping) return if (this.isStopping) return
...@@ -108,4 +118,33 @@ export class NextStartInstance extends NextInstance { ...@@ -108,4 +118,33 @@ export class NextStartInstance extends NextInstance {
this.on('stdout', readyCb) this.on('stdout', readyCb)
}) })
} }
public async export() {
return new Promise((resolve) => {
const curOutput = this._cliOutput.length
const exportArgs = ['yarn', 'next', 'export']
if (this.childProcess) {
throw new Error(
`can not run export while server is running, use next.stop() first`
)
}
console.log('running', exportArgs.join(' '))
this.childProcess = spawn(
exportArgs[0],
exportArgs.slice(1),
this.spawnOpts
)
this.handleStdio(this.childProcess)
this.childProcess.on('exit', (code, signal) => {
this.childProcess = undefined
resolve({
exitCode: signal || code,
cliOutput: this.cliOutput.substr(curOutput),
})
})
})
}
} }
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import { join } from 'path'
describe('fallback export error', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'pages')),
},
})
})
afterAll(() => next.destroy())
it('should have built', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('index page')
})
it('should not error with default exportPathMap', async () => {
await next.stop()
const result = await next.export()
console.log(result.cliOutput)
expect(result.exitCode).toBe(0)
expect(result.cliOutput).not.toContain(
'Found pages with `fallback` enabled'
)
})
it('should not error with valid exportPathMap', async () => {
await next.stop()
await next.patchFile(
'next.config.js',
`
module.exports = {
exportPathMap() {
return {
'/': { page: '/' },
}
}
}
`
)
try {
const result = await next.export()
console.log(result.cliOutput)
expect(result.exitCode).toBe(0)
expect(result.cliOutput).not.toContain(
'Found pages with `fallback` enabled'
)
} finally {
await next.deleteFile('next.config.js')
}
})
it('should error with invalid exportPathMap', async () => {
await next.stop()
await next.patchFile(
'next.config.js',
`
module.exports = {
exportPathMap() {
return {
'/': { page: '/' },
'/second': { page: '/[...slug]' }
}
}
}
`
)
try {
const result = await next.export()
console.log(result.cliOutput)
expect(result.exitCode).toBe(1)
expect(result.cliOutput).toContain('Found pages with `fallback` enabled')
} finally {
await next.deleteFile('next.config.js')
}
})
})
export function getStaticProps() {
return {
props: {
now: Date.now(),
},
}
}
export function getStaticPaths() {
return {
paths: ['/first'],
fallback: 'blocking',
}
}
export default function Page() {
return <p>catch-all page</p>
}
export default function Page() {
return <p>index page</p>
}
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