Unverified Commit 83bc82e4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix auto export opting out from _error edge case (#13425)

This fixes an edge case where an application can seemingly randomly be opted out of the automatic static optimization from having an `_error` file that isn't directly nested under the `pages` folder e.g. `pages/hello/_error.js`. 

Since in serverless mode we use the `_app` export from the first serverless page, if this `_error.js` file is used to check for custom `getIniitalProps` in `_app` and the `_error.js` page had a custom `getInitialProps` itself it would cause the check to fail opting the application out of the static optimization. 

This fixes it by having the check be explicit instead of relying on the bundle name and it also adds regression tests for this. It might be good to also add a warning when `_error` or `_app` are not directly nested under the pages folder in a follow up PR
parent 4a02dc5a
...@@ -497,7 +497,8 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -497,7 +497,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
: ['server', 'static', buildId, 'pages']), : ['server', 'static', buildId, 'pages']),
'_error.js' '_error.js'
), ),
runtimeEnvConfig runtimeEnvConfig,
false
)) ))
const analysisBegin = process.hrtime() const analysisBegin = process.hrtime()
...@@ -540,7 +541,8 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -540,7 +541,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
SERVER_DIRECTORY, SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js` `/static/${buildId}/pages/_app.js`
), ),
runtimeEnvConfig runtimeEnvConfig,
true
) )
namedExports = getNamedExports( namedExports = getNamedExports(
......
...@@ -776,16 +776,16 @@ export async function isPageStatic( ...@@ -776,16 +776,16 @@ export async function isPageStatic(
export function hasCustomGetInitialProps( export function hasCustomGetInitialProps(
bundle: string, bundle: string,
runtimeEnvConfig: any runtimeEnvConfig: any,
checkingApp: boolean
): boolean { ): boolean {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig) require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
let mod = require(bundle) let mod = require(bundle)
if (bundle.endsWith('_app.js') || bundle.endsWith('_error.js')) { if (checkingApp) {
mod = mod.default || mod mod = mod._app || mod.default || mod
} else { } else {
// since we don't output _app in serverless mode get it from a page mod = mod.default || mod
mod = mod._app
} }
return mod.getInitialProps !== mod.origGetInitialProps return mod.getInitialProps !== mod.origGetInitialProps
} }
......
import Error from 'next/error'
function MyError(props) {
return <Error {...props} />
}
MyError.getInitialProps = async (ctx) => {
return Error.getInitialProps(ctx)
}
export default MyError
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 1)
const appDir = path.join(__dirname, '..')
const nextConfig = path.join(appDir, 'next.config.js')
const runTests = () => {
it('should not opt-out of auto static optimization from invalid _error', async () => {
const output = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
if (output.code) {
console.log(output)
}
expect(output.code).toBe(0)
expect(output.stderr + output.stdout).not.toContain(
'You have opted-out of Automatic Static Optimization due to'
)
})
}
describe('Auto Export _error bail', () => {
describe('server mode', () => {
runTests()
})
describe('serverless mode', () => {
beforeAll(() =>
fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
)
afterAll(() => fs.remove(nextConfig))
runTests()
})
})
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