Unverified Commit 4199de63 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove old reloading code (#13554)

This code existed mostly to work around webpack 2 (yes 2.x) limitations where it crashed in certain cases where files didn't exist anymore. We have tests for that behavior and latest webpack has fixed these. Hence why this can be removed 👍 
parent fef3b8c2
...@@ -333,28 +333,6 @@ export default class HotReloader { ...@@ -333,28 +333,6 @@ export default class HotReloader {
} }
} }
async reload(): Promise<void> {
this.stats = null
this.serverStats = null
await this.clean()
const configs = await this.getWebpackConfig()
const compiler = webpack(configs)
const buildTools = await this.prepareBuildTools(compiler)
// [Client, Server]
;[
this.stats,
this.serverStats,
] = ((await this.waitUntilValid()) as any).stats
const oldWebpackDevMiddleware = this.webpackDevMiddleware
this.assignBuildTools(buildTools)
await this.stop(oldWebpackDevMiddleware!)
}
assignBuildTools({ assignBuildTools({
webpackDevMiddleware, webpackDevMiddleware,
webpackHotMiddleware, webpackHotMiddleware,
...@@ -509,7 +487,6 @@ export default class HotReloader { ...@@ -509,7 +487,6 @@ export default class HotReloader {
{ {
buildId: this.buildId, buildId: this.buildId,
pagesDir: this.pagesDir, pagesDir: this.pagesDir,
reload: this.reload.bind(this),
pageExtensions: this.config.pageExtensions, pageExtensions: this.config.pageExtensions,
...(this.config.onDemandEntries as { ...(this.config.onDemandEntries as {
maxInactiveAge: number maxInactiveAge: number
...@@ -536,8 +513,6 @@ export default class HotReloader { ...@@ -536,8 +513,6 @@ export default class HotReloader {
async getCompilationErrors(page: string) { async getCompilationErrors(page: string) {
const normalizedPage = normalizePage(page) const normalizedPage = normalizePage(page)
// When we are reloading, we need to wait until it's reloaded properly.
await this.onDemandEntries.waitUntilReloaded()
if (this.stats.hasErrors()) { if (this.stats.hasErrors()) {
const { compilation } = this.stats const { compilation } = this.stats
......
...@@ -10,10 +10,7 @@ import { isWriteable } from '../build/is-writeable' ...@@ -10,10 +10,7 @@ import { isWriteable } from '../build/is-writeable'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { ClientPagesLoaderOptions } from '../build/webpack/loaders/next-client-pages-loader' import { ClientPagesLoaderOptions } from '../build/webpack/loaders/next-client-pages-loader'
import { API_ROUTE } from '../lib/constants' import { API_ROUTE } from '../lib/constants'
import { import { ROUTE_NAME_REGEX } from '../next-server/lib/constants'
IS_BUNDLED_PAGE_REGEX,
ROUTE_NAME_REGEX,
} from '../next-server/lib/constants'
import { normalizePagePath } from '../next-server/server/normalize-page-path' import { normalizePagePath } from '../next-server/server/normalize-page-path'
import { pageNotFoundError } from '../next-server/server/require' import { pageNotFoundError } from '../next-server/server/require'
import { findPageFile } from './lib/find-page-file' import { findPageFile } from './lib/find-page-file'
...@@ -44,14 +41,12 @@ export default function onDemandEntryHandler( ...@@ -44,14 +41,12 @@ export default function onDemandEntryHandler(
{ {
buildId, buildId,
pagesDir, pagesDir,
reload,
pageExtensions, pageExtensions,
maxInactiveAge, maxInactiveAge,
pagesBufferLength, pagesBufferLength,
}: { }: {
buildId: string buildId: string
pagesDir: string pagesDir: string
reload: any
pageExtensions: string[] pageExtensions: string[]
maxInactiveAge: number maxInactiveAge: number
pagesBufferLength: number pagesBufferLength: number
...@@ -62,9 +57,6 @@ export default function onDemandEntryHandler( ...@@ -62,9 +57,6 @@ export default function onDemandEntryHandler(
let entries: any = {} let entries: any = {}
let lastAccessPages = [''] let lastAccessPages = ['']
let doneCallbacks: EventEmitter | null = new EventEmitter() let doneCallbacks: EventEmitter | null = new EventEmitter()
let reloading = false
let stopped = false
let reloadCallbacks: EventEmitter | null = new EventEmitter()
for (const compiler of compilers) { for (const compiler of compilers) {
compiler.hooks.make.tapPromise( compiler.hooks.make.tapPromise(
...@@ -101,29 +93,6 @@ export default function onDemandEntryHandler( ...@@ -101,29 +93,6 @@ export default function onDemandEntryHandler(
) )
} }
function findHardFailedPages(errors: any[]) {
return errors
.filter((e) => {
// Make sure to only pick errors which marked with missing modules
const hasNoModuleFoundError =
/ENOENT/.test(e.message) || /Module not found/.test(e.message)
if (!hasNoModuleFoundError) return false
// The page itself is missing. So this is a failed page.
if (IS_BUNDLED_PAGE_REGEX.test(e.module.name)) return true
// No dependencies means this is a top level page.
// So this is a failed page.
return e.module.dependencies.length === 0
})
.map((e) => e.module.chunks)
.reduce((a, b) => [...a, ...b], [])
.map((c: any) => {
const pageName = ROUTE_NAME_REGEX.exec(c.name)![1]
return normalizePage(`/${pageName}`)
})
}
function getPagePathsFromEntrypoints(entrypoints: any) { function getPagePathsFromEntrypoints(entrypoints: any) {
const pagePaths = [] const pagePaths = []
for (const [, entrypoint] of entrypoints.entries()) { for (const [, entrypoint] of entrypoints.entries()) {
...@@ -146,12 +115,6 @@ export default function onDemandEntryHandler( ...@@ -146,12 +115,6 @@ export default function onDemandEntryHandler(
multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => { multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => {
const [clientStats, serverStats] = multiStats.stats const [clientStats, serverStats] = multiStats.stats
const hardFailedPages = [
...new Set([
...findHardFailedPages(clientStats.compilation.errors),
...findHardFailedPages(serverStats.compilation.errors),
]),
]
const pagePaths = new Set([ const pagePaths = new Set([
...getPagePathsFromEntrypoints(clientStats.compilation.entrypoints), ...getPagePathsFromEntrypoints(clientStats.compilation.entrypoints),
...getPagePathsFromEntrypoints(serverStats.compilation.entrypoints), ...getPagePathsFromEntrypoints(serverStats.compilation.entrypoints),
...@@ -176,30 +139,9 @@ export default function onDemandEntryHandler( ...@@ -176,30 +139,9 @@ export default function onDemandEntryHandler(
} }
invalidator.doneBuilding() invalidator.doneBuilding()
if (hardFailedPages.length > 0 && !reloading) {
console.log(
`> Reloading webpack due to inconsistant state of pages(s): ${hardFailedPages.join(
', '
)}`
)
reloading = true
reload()
.then(() => {
console.log('> Webpack reloaded.')
reloadCallbacks!.emit('done')
stop()
})
.catch((err: Error) => {
console.error(`> Webpack reloading failed: ${err.message}`)
console.error(err.stack)
process.exit(1)
})
}
}) })
const disposeHandler = setInterval(function () { const disposeHandler = setInterval(function () {
if (stopped) return
disposeInactiveEntries( disposeInactiveEntries(
devMiddleware, devMiddleware,
entries, entries,
...@@ -210,13 +152,6 @@ export default function onDemandEntryHandler( ...@@ -210,13 +152,6 @@ export default function onDemandEntryHandler(
disposeHandler.unref() disposeHandler.unref()
function stop() {
clearInterval(disposeHandler)
stopped = true
doneCallbacks = null
reloadCallbacks = null
}
function handlePing(pg: string) { function handlePing(pg: string) {
const page = normalizePage(pg) const page = normalizePage(pg)
const entryInfo = entries[page] const entryInfo = entries[page]
...@@ -252,17 +187,7 @@ export default function onDemandEntryHandler( ...@@ -252,17 +187,7 @@ export default function onDemandEntryHandler(
} }
return { return {
waitUntilReloaded() {
if (!reloading) return Promise.resolve(true)
return new Promise((resolve) => {
reloadCallbacks!.once('done', function () {
resolve()
})
})
},
async ensurePage(page: string) { async ensurePage(page: string) {
await this.waitUntilReloaded()
let normalizedPagePath: string let normalizedPagePath: string
try { try {
normalizedPagePath = normalizePagePath(page) normalizedPagePath = normalizePagePath(page)
...@@ -335,40 +260,23 @@ export default function onDemandEntryHandler( ...@@ -335,40 +260,23 @@ export default function onDemandEntryHandler(
middleware() { middleware() {
return (req: IncomingMessage, res: ServerResponse, next: Function) => { return (req: IncomingMessage, res: ServerResponse, next: Function) => {
if (stopped) { if (!/^\/_next\/webpack-hmr/.test(req.url!)) return next()
// If this handler is stopped, we need to reload the user's browser.
// So the user could connect to the actually running handler. const { query } = parse(req.url!, true)
res.statusCode = 302 const page = query.page
res.setHeader('Location', req.url!) if (!page) return next()
res.end('302')
} else if (reloading) {
// Webpack config is reloading. So, we need to wait until it's done and
// reload user's browser.
// So the user could connect to the new handler and webpack setup.
this.waitUntilReloaded().then(() => {
res.statusCode = 302
res.setHeader('Location', req.url!)
res.end('302')
})
} else {
if (!/^\/_next\/webpack-hmr/.test(req.url!)) return next()
const { query } = parse(req.url!, true)
const page = query.page
if (!page) return next()
const runPing = () => {
const data = handlePing(query.page as string)
if (!data) return
res.write('data: ' + JSON.stringify(data) + '\n\n')
}
const pingInterval = setInterval(() => runPing(), 5000)
req.on('close', () => { const runPing = () => {
clearInterval(pingInterval) const data = handlePing(query.page as string)
}) if (!data) return
next() res.write('data: ' + JSON.stringify(data) + '\n\n')
} }
const pingInterval = setInterval(() => runPing(), 5000)
req.on('close', () => {
clearInterval(pingInterval)
})
next()
} }
}, },
} }
......
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