Unverified Commit cac35c38 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix hot reloader edge case with broken webpack plugins (#15659)

parent 6b0c4948
......@@ -143,8 +143,10 @@ export default class HotReloader {
private pagesDir: string
private webpackHotMiddleware: (NextHandleFunction & any) | null
private config: any
private stats: any
private serverStats: any
private stats: webpack.Stats | null
private serverStats: webpack.Stats | null
private clientError: Error | null = null
private serverError: Error | null = null
private serverPrevDocumentHash: string | null
private prevChunkNames?: Set<any>
private onDemandEntries: any
......@@ -349,9 +351,17 @@ export default class HotReloader {
watchCompilers(multiCompiler.compilers[0], multiCompiler.compilers[1])
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
multiCompiler.compilers[1].hooks.failed.tap(
'NextjsHotReloaderForServer',
(err: Error) => {
this.serverError = err
this.serverStats = null
}
)
multiCompiler.compilers[1].hooks.done.tap(
'NextjsHotReloaderForServer',
(stats) => {
this.serverError = null
this.serverStats = stats
const { compilation } = stats
......@@ -384,9 +394,19 @@ export default class HotReloader {
}
)
multiCompiler.compilers[0].hooks.failed.tap(
'NextjsHotReloaderForClient',
(err: Error) => {
this.clientError = err
this.stats = null
}
)
multiCompiler.compilers[0].hooks.done.tap(
'NextjsHotReloaderForClient',
(stats) => {
this.clientError = null
this.stats = stats
const { compilation } = stats
const chunkNames = new Set(
compilation.chunks
......@@ -415,7 +435,6 @@ export default class HotReloader {
}
}
this.stats = stats
this.prevChunkNames = chunkNames
}
)
......@@ -471,7 +490,9 @@ export default class HotReloader {
public async getCompilationErrors(page: string) {
const normalizedPage = normalizePathSep(page)
if (this.stats.hasErrors()) {
if (this.clientError || this.serverError) {
return [this.clientError || this.serverError]
} else if (this.stats?.hasErrors()) {
const { compilation } = this.stats
const failedPages = erroredPages(compilation)
......@@ -499,6 +520,9 @@ export default class HotReloader {
if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
return
}
if (this.serverError || this.clientError) {
return Promise.reject(this.serverError || this.clientError)
}
return this.onDemandEntries.ensurePage(page)
}
}
......
......@@ -16,8 +16,8 @@ import { launchEditor } from './internal/helpers/launchEditor'
export type OverlayMiddlewareOptions = {
rootDirectory: string
stats(): webpack.Stats
serverStats(): webpack.Stats
stats(): webpack.Stats | null
serverStats(): webpack.Stats | null
}
export type OriginalStackFrameResponse = {
......
class BrokenPlugin {
apply(compiler) {
compiler.hooks.watchRun.tapPromise('BrokenPlugin', async () => {
throw new Error('oops')
})
}
}
module.exports = {
webpack(config, { isServer }) {
if (!isServer) {
config.plugins.push(new BrokenPlugin())
}
return config
},
}
export default function Test() {
return <p>hello from index</p>
}
/* eslint-env jest */
import { findPort, killApp, launchApp, renderViaHTTP } from 'next-test-utils'
import { join } from 'path'
import waitPort from 'wait-port'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
let appPort
let app
describe('Handles a broken webpack plugin (precompile)', () => {
let stderr = ''
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
stderr: true,
nextStart: true,
onStderr(text) {
stderr += text
},
})
await waitPort({
host: 'localhost',
port: appPort,
})
})
afterAll(() => killApp(app))
beforeEach(() => {
stderr = ''
})
it('should render error correctly', async () => {
const text = await renderViaHTTP(appPort, '/')
expect(text).toContain('Internal Server Error')
expect(stderr).toMatch('Error: oops')
})
})
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