Unverified Commit 85d87a37 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure dev overlay is triggered for more _app/_document errors (#24328)

This expands on https://github.com/vercel/next.js/issues/24070 and ensures we show the dev overlay for additional cases like where `_app` or `_document` have syntax errors causing compilation to not be able to complete. This achieves showing the dev overlay even when compilation fails from a syntax error by doing a third minimal compilation in development with the needed client-side assets to render the dev overlay. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

x-ref: https://github.com/vercel/next.js/issues/24070
parent c481147b
...@@ -189,6 +189,7 @@ export default async function getBaseWebpackConfig( ...@@ -189,6 +189,7 @@ export default async function getBaseWebpackConfig(
reactProductionProfiling = false, reactProductionProfiling = false,
entrypoints, entrypoints,
rewrites, rewrites,
isDevFallback = false,
}: { }: {
buildId: string buildId: string
config: NextConfig config: NextConfig
...@@ -199,6 +200,7 @@ export default async function getBaseWebpackConfig( ...@@ -199,6 +200,7 @@ export default async function getBaseWebpackConfig(
reactProductionProfiling?: boolean reactProductionProfiling?: boolean
entrypoints: WebpackEntrypoints entrypoints: WebpackEntrypoints
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
let plugins: PluginMetaData[] = [] let plugins: PluginMetaData[] = []
...@@ -916,7 +918,9 @@ export default async function getBaseWebpackConfig( ...@@ -916,7 +918,9 @@ export default async function getBaseWebpackConfig(
? isWebpack5 && !dev ? isWebpack5 && !dev
? '../[name].js' ? '../[name].js'
: '[name].js' : '[name].js'
: `static/chunks/[name]${dev ? '' : '-[chunkhash]'}.js`, : `static/chunks/${isDevFallback ? 'fallback/' : ''}[name]${
dev ? '' : '-[chunkhash]'
}.js`,
library: isServer ? undefined : '_N_E', library: isServer ? undefined : '_N_E',
libraryTarget: isServer ? 'commonjs2' : 'assign', libraryTarget: isServer ? 'commonjs2' : 'assign',
hotUpdateChunkFilename: isWebpack5 hotUpdateChunkFilename: isWebpack5
...@@ -928,7 +932,9 @@ export default async function getBaseWebpackConfig( ...@@ -928,7 +932,9 @@ export default async function getBaseWebpackConfig(
// This saves chunks with the name given via `import()` // This saves chunks with the name given via `import()`
chunkFilename: isServer chunkFilename: isServer
? `${dev ? '[name]' : '[name].[contenthash]'}.js` ? `${dev ? '[name]' : '[name].[contenthash]'}.js`
: `static/chunks/${dev ? '[name]' : '[name].[contenthash]'}.js`, : `static/chunks/${isDevFallback ? 'fallback/' : ''}${
dev ? '[name]' : '[name].[contenthash]'
}.js`,
strictModuleExceptionHandling: true, strictModuleExceptionHandling: true,
crossOriginLoading: crossOrigin, crossOriginLoading: crossOrigin,
futureEmitAssets: !dev, futureEmitAssets: !dev,
...@@ -1188,6 +1194,7 @@ export default async function getBaseWebpackConfig( ...@@ -1188,6 +1194,7 @@ export default async function getBaseWebpackConfig(
new BuildManifestPlugin({ new BuildManifestPlugin({
buildId, buildId,
rewrites, rewrites,
isDevFallback,
}), }),
!dev && !dev &&
!isServer && !isServer &&
......
...@@ -95,13 +95,15 @@ const processRoute = (r: Rewrite) => { ...@@ -95,13 +95,15 @@ const processRoute = (r: Rewrite) => {
export default class BuildManifestPlugin { export default class BuildManifestPlugin {
private buildId: string private buildId: string
private rewrites: CustomRoutes['rewrites'] private rewrites: CustomRoutes['rewrites']
private isDevFallback: boolean
constructor(options: { constructor(options: {
buildId: string buildId: string
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
}) { }) {
this.buildId = options.buildId this.buildId = options.buildId
this.isDevFallback = !!options.isDevFallback
this.rewrites = { this.rewrites = {
beforeFiles: [], beforeFiles: [],
afterFiles: [], afterFiles: [],
...@@ -165,7 +167,6 @@ export default class BuildManifestPlugin { ...@@ -165,7 +167,6 @@ export default class BuildManifestPlugin {
for (const entrypoint of compilation.entrypoints.values()) { for (const entrypoint of compilation.entrypoints.values()) {
if (systemEntrypoints.has(entrypoint.name)) continue if (systemEntrypoints.has(entrypoint.name)) continue
const pagePath = getRouteFromEntrypoint(entrypoint.name) const pagePath = getRouteFromEntrypoint(entrypoint.name)
if (!pagePath) { if (!pagePath) {
...@@ -177,13 +178,13 @@ export default class BuildManifestPlugin { ...@@ -177,13 +178,13 @@ export default class BuildManifestPlugin {
assetMap.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])] assetMap.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])]
} }
if (!this.isDevFallback) {
// Add the runtime build manifest file (generated later in this file) // Add the runtime build manifest file (generated later in this file)
// as a dependency for the app. If the flag is false, the file won't be // as a dependency for the app. If the flag is false, the file won't be
// downloaded by the client. // downloaded by the client.
assetMap.lowPriorityFiles.push( assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js` `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
) )
// Add the runtime ssg manifest file as a lazy-loaded file dependency. // Add the runtime ssg manifest file as a lazy-loaded file dependency.
// We also stub this file out for development mode (when it is not // We also stub this file out for development mode (when it is not
// generated). // generated).
...@@ -192,16 +193,24 @@ export default class BuildManifestPlugin { ...@@ -192,16 +193,24 @@ export default class BuildManifestPlugin {
const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js` const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`
assetMap.lowPriorityFiles.push(ssgManifestPath) assetMap.lowPriorityFiles.push(ssgManifestPath)
assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest) assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest)
}
assetMap.pages = Object.keys(assetMap.pages) assetMap.pages = Object.keys(assetMap.pages)
.sort() .sort()
// eslint-disable-next-line // eslint-disable-next-line
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any) .reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)
assets[BUILD_MANIFEST] = new sources.RawSource( let buildManifestName = BUILD_MANIFEST
if (this.isDevFallback) {
buildManifestName = `fallback-${BUILD_MANIFEST}`
}
assets[buildManifestName] = new sources.RawSource(
JSON.stringify(assetMap, null, 2) JSON.stringify(assetMap, null, 2)
) )
if (!this.isDevFallback) {
const clientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js` const clientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
assets[clientManifestPath] = new sources.RawSource( assets[clientManifestPath] = new sources.RawSource(
...@@ -211,6 +220,7 @@ export default class BuildManifestPlugin { ...@@ -211,6 +220,7 @@ export default class BuildManifestPlugin {
this.rewrites this.rewrites
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()` )};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
) )
}
return assets return assets
}) })
......
...@@ -44,8 +44,8 @@ export async function loadDefaultErrorComponents(distDir: string) { ...@@ -44,8 +44,8 @@ export async function loadDefaultErrorComponents(distDir: string) {
App, App,
Document, Document,
Component, Component,
buildManifest: require(join(distDir, BUILD_MANIFEST)), buildManifest: require(join(distDir, `fallback-${BUILD_MANIFEST}`)),
reactLoadableManifest: require(join(distDir, REACT_LOADABLE_MANIFEST)), reactLoadableManifest: {},
ComponentMod, ComponentMod,
} }
} }
...@@ -74,9 +74,20 @@ export async function loadComponents( ...@@ -74,9 +74,20 @@ export async function loadComponents(
} as LoadComponentsReturnType } as LoadComponentsReturnType
} }
const DocumentMod = await requirePage('/_document', distDir, serverless) let DocumentMod
const AppMod = await requirePage('/_app', distDir, serverless) let AppMod
const ComponentMod = await requirePage(pathname, distDir, serverless) let ComponentMod
try {
DocumentMod = await requirePage('/_document', distDir, serverless)
AppMod = await requirePage('/_app', distDir, serverless)
ComponentMod = await requirePage(pathname, distDir, serverless)
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error(`Failed to load ${pathname}`)
}
throw err
}
const [ const [
buildManifest, buildManifest,
......
...@@ -92,6 +92,7 @@ import cookie from 'next/dist/compiled/cookie' ...@@ -92,6 +92,7 @@ import cookie from 'next/dist/compiled/cookie'
import escapePathDelimiters from '../lib/router/utils/escape-path-delimiters' import escapePathDelimiters from '../lib/router/utils/escape-path-delimiters'
import { getUtils } from '../../build/webpack/loaders/next-serverless-loader/utils' import { getUtils } from '../../build/webpack/loaders/next-serverless-loader/utils'
import { PreviewData } from 'next/types' import { PreviewData } from 'next/types'
import HotReloader from '../../server/hot-reloader'
const getCustomRouteMatcher = pathMatch(true) const getCustomRouteMatcher = pathMatch(true)
...@@ -2051,6 +2052,8 @@ export default class Server { ...@@ -2051,6 +2052,8 @@ export default class Server {
res.statusCode = 500 res.statusCode = 500
if (this.renderOpts.dev) { if (this.renderOpts.dev) {
await ((this as any).hotReloader as HotReloader).buildFallbackError()
const fallbackResult = await loadDefaultErrorComponents(this.distDir) const fallbackResult = await loadDefaultErrorComponents(this.distDir)
return this.renderToHTMLWithComponents( return this.renderToHTMLWithComponents(
req, req,
......
...@@ -27,25 +27,62 @@ import http from 'http' ...@@ -27,25 +27,62 @@ import http from 'http'
export class WebpackHotMiddleware { export class WebpackHotMiddleware {
eventStream: EventStream eventStream: EventStream
latestStats: webpack.Stats | null latestStats: webpack.Stats | null
clientLatestStats: webpack.Stats | null
closed: boolean closed: boolean
serverError: boolean
constructor(compiler: webpack.Compiler) { constructor(compilers: webpack.Compiler[]) {
this.eventStream = new EventStream() this.eventStream = new EventStream()
this.latestStats = null this.latestStats = null
this.clientLatestStats = null
this.serverError = false
this.closed = false this.closed = false
compiler.hooks.invalid.tap('webpack-hot-middleware', this.onInvalid) compilers[0].hooks.invalid.tap(
compiler.hooks.done.tap('webpack-hot-middleware', this.onDone) 'webpack-hot-middleware',
this.onClientInvalid
)
compilers[0].hooks.done.tap('webpack-hot-middleware', this.onClientDone)
compilers[1].hooks.invalid.tap(
'webpack-hot-middleware',
this.onServerInvalid
)
compilers[1].hooks.done.tap('webpack-hot-middleware', this.onServerDone)
} }
onInvalid = () => { onServerInvalid = () => {
if (this.closed) return if (!this.serverError) return
this.serverError = false
if (this.clientLatestStats) {
this.latestStats = this.clientLatestStats
this.publishStats('built', this.latestStats)
}
}
onClientInvalid = () => {
if (this.closed || this.serverError) return
this.latestStats = null this.latestStats = null
this.eventStream.publish({ action: 'building' }) this.eventStream.publish({ action: 'building' })
} }
onDone = (statsResult: webpack.Stats) => { onServerDone = (statsResult: webpack.Stats) => {
if (this.closed) return if (this.closed) return
// Keep hold of latest stats so they can be propagated to new clients // Keep hold of latest stats so they can be propagated to new clients
// this.latestStats = statsResult
// this.publishStats('built', this.latestStats)
this.serverError = statsResult.hasErrors()
if (this.serverError) {
this.latestStats = statsResult
this.publishStats('built', this.latestStats)
}
}
onClientDone = (statsResult: webpack.Stats) => {
this.clientLatestStats = statsResult
if (this.closed || this.serverError) return
// Keep hold of latest stats so they can be propagated to new clients
this.latestStats = statsResult this.latestStats = statsResult
this.publishStats('built', this.latestStats) this.publishStats('built', this.latestStats)
} }
......
...@@ -144,6 +144,7 @@ export default class HotReloader { ...@@ -144,6 +144,7 @@ export default class HotReloader {
private previewProps: __ApiPreviewProps private previewProps: __ApiPreviewProps
private watcher: any private watcher: any
private rewrites: CustomRoutes['rewrites'] private rewrites: CustomRoutes['rewrites']
private fallbackWatcher: any
public isWebpack5: any public isWebpack5: any
constructor( constructor(
...@@ -300,6 +301,51 @@ export default class HotReloader { ...@@ -300,6 +301,51 @@ export default class HotReloader {
]) ])
} }
public async buildFallbackError(): Promise<void> {
if (this.fallbackWatcher) return
const fallbackConfig = await getBaseWebpackConfig(this.dir, {
dev: true,
isServer: false,
config: this.config,
buildId: this.buildId,
pagesDir: this.pagesDir,
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: [],
},
isDevFallback: true,
entrypoints: createEntrypoints(
{
'/_app': 'next/dist/pages/_app',
'/_error': 'next/dist/pages/_error',
},
'server',
this.buildId,
this.previewProps,
this.config,
[]
).client,
})
const fallbackCompiler = webpack(fallbackConfig)
this.fallbackWatcher = await new Promise((resolve) => {
let bootedFallbackCompiler = false
fallbackCompiler.watch(
// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
fallbackConfig.watchOptions,
// Errors are handled separately
(_err: any) => {
if (!bootedFallbackCompiler) {
bootedFallbackCompiler = true
resolve(true)
}
}
)
})
}
public async start(): Promise<void> { public async start(): Promise<void> {
await this.clean() await this.clean()
...@@ -492,7 +538,7 @@ export default class HotReloader { ...@@ -492,7 +538,7 @@ export default class HotReloader {
) )
this.webpackHotMiddleware = new WebpackHotMiddleware( this.webpackHotMiddleware = new WebpackHotMiddleware(
multiCompiler.compilers[0] multiCompiler.compilers
) )
let booted = false let booted = false
...@@ -534,10 +580,18 @@ export default class HotReloader { ...@@ -534,10 +580,18 @@ export default class HotReloader {
} }
public async stop(): Promise<void> { public async stop(): Promise<void> {
return new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
this.watcher.close((err: any) => (err ? reject(err) : resolve())) this.watcher.close((err: any) => (err ? reject(err) : resolve(true)))
})
if (this.fallbackWatcher) {
await new Promise((resolve, reject) => {
this.fallbackWatcher.close((err: any) =>
err ? reject(err) : resolve(true)
)
}) })
} }
}
public async getCompilationErrors(page: string) { public async getCompilationErrors(page: string) {
const normalizedPage = normalizePathSep(page) const normalizedPage = normalizePathSep(page)
......
...@@ -1380,3 +1380,205 @@ test('_document top level error shows logbox', async () => { ...@@ -1380,3 +1380,205 @@ test('_document top level error shows logbox', async () => {
expect(await session.hasRedbox()).toBe(false) expect(await session.hasRedbox()).toBe(false)
await cleanup() await cleanup()
}) })
test('empty _app shows logbox', async () => {
const [session, cleanup] = await sandbox(
undefined,
new Map([
[
'pages/_app.js',
`
`,
],
])
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
`"Error: The default export is not a React Component in page: \\"/_app\\""`
)
await session.patch(
'pages/_app.js',
`
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp
`
)
expect(await session.hasRedbox()).toBe(false)
await cleanup()
})
test('empty _document shows logbox', async () => {
const [session, cleanup] = await sandbox(
undefined,
new Map([
[
'pages/_document.js',
`
`,
],
])
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
`"Error: The default export is not a React Component in page: \\"/_document\\""`
)
await session.patch(
'pages/_document.js',
`
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
`
)
expect(await session.hasRedbox()).toBe(false)
await cleanup()
})
test('_app syntax error shows logbox', async () => {
const [session, cleanup] = await sandbox(
undefined,
new Map([
[
'pages/_app.js',
`
function MyApp({ Component, pageProps }) {
return <<Component {...pageProps} />;
}
export default MyApp
`,
],
]),
undefined,
/ready - started server on/i
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"./pages/_app.js:3:20
Syntax error: Unexpected token
1 |
2 | function MyApp({ Component, pageProps }) {
> 3 | return <<Component {...pageProps} />;
| ^
4 | }
5 | export default MyApp
6 |"
`)
await session.patch(
'pages/_app.js',
`
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp
`
)
expect(await session.hasRedbox()).toBe(false)
await cleanup()
})
test('_document syntax error shows logbox', async () => {
const [session, cleanup] = await sandbox(
undefined,
new Map([
[
'pages/_document.js',
`
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {{
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
`,
],
]),
undefined,
/ready - started server on/i
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"./pages/_document.js:4:45
Syntax error: Unexpected token
2 | import Document, { Html, Head, Main, NextScript } from 'next/document'
3 |
> 4 | class MyDocument extends Document {{
| ^
5 | static async getInitialProps(ctx) {
6 | const initialProps = await Document.getInitialProps(ctx)
7 | return { ...initialProps }"
`)
await session.patch(
'pages/_document.js',
`
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
`
)
expect(await session.hasRedbox()).toBe(false)
await cleanup()
})
...@@ -9,7 +9,8 @@ const rootSandboxDirectory = path.join(__dirname, '__tmp__') ...@@ -9,7 +9,8 @@ const rootSandboxDirectory = path.join(__dirname, '__tmp__')
export async function sandbox( export async function sandbox(
id = nanoid(), id = nanoid(),
initialFiles = new Map(), initialFiles = new Map(),
defaultFiles = true defaultFiles = true,
readyLog
) { ) {
const sandboxDirectory = path.join(rootSandboxDirectory, id) const sandboxDirectory = path.join(rootSandboxDirectory, id)
...@@ -35,6 +36,7 @@ export async function sandbox( ...@@ -35,6 +36,7 @@ export async function sandbox(
const appPort = await findPort() const appPort = await findPort()
const app = await launchApp(sandboxDirectory, appPort, { const app = await launchApp(sandboxDirectory, appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 }, env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
bootupMarker: readyLog,
}) })
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
return [ return [
......
...@@ -189,6 +189,7 @@ export function runNextCommandDev(argv, stdOut, opts = {}) { ...@@ -189,6 +189,7 @@ export function runNextCommandDev(argv, stdOut, opts = {}) {
start: /started server/i, start: /started server/i,
} }
if ( if (
(opts.bootupMarker && opts.bootupMarker.test(message)) ||
bootupMarkers[opts.nextStart || stdOut ? 'start' : 'dev'].test(message) bootupMarkers[opts.nextStart || stdOut ? 'start' : 'dev'].test(message)
) { ) {
if (!didResolve) { if (!didResolve) {
......
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