Unverified Commit b340bc74 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Turbopack HMR: extract deferring build messages and use it in server hmr (#95795)

Churn in the graph can lead to a `building` message quickly followed by
a `built` message. The underlying issue should be addressed, but for
now, extract this debounce from the client HMR implementation and use it
in server hmr.
parent 8cfa5510
import type { TurbopackMessage } from '../../../server/dev/hot-reloader-types' import type { TurbopackMessage } from '../../../server/dev/hot-reloader-types'
import type { Update as TurbopackUpdate } from '../../../build/swc/types' import type { Update as TurbopackUpdate } from '../../../build/swc/types'
import { DeferredEmit } from '../../../shared/lib/turbopack/deferred-emit'
declare global { declare global {
interface Window { interface Window {
...@@ -22,53 +23,46 @@ export class TurbopackHmr { ...@@ -22,53 +23,46 @@ export class TurbopackHmr {
#updatedModules: Set<string> #updatedModules: Set<string>
#startMsSinceEpoch: number | undefined #startMsSinceEpoch: number | undefined
#lastUpdateMsSinceEpoch: number | undefined #lastUpdateMsSinceEpoch: number | undefined
#deferredReportHmrStartId: ReturnType<typeof setTimeout> | undefined
#reportedHmrStart: boolean
constructor() {
this.#updatedModules = new Set()
this.#reportedHmrStart = false
}
// HACK: Turbopack tends to generate a lot of irrelevant "BUILDING" actions, // HACK: Turbopack tends to generate a lot of irrelevant "BUILDING" actions,
// as it reports *any* compilation, including fully no-op/cached compilations // as it reports *any* compilation, including fully no-op/cached compilations
// and those unrelated to HMR. Fixing this would require significant // and those unrelated to HMR. Fixing this would require significant
// architectural changes. // architectural changes.
// //
// Work around this by deferring any "rebuilding" message by 100ms. If we get // Work around this by deferring the `[Fast Refresh] rebuilding` message by
// a BUILT event within that threshold and nothing has changed, just suppress // 100ms. If we get a BUILT event within that threshold and nothing has
// the message entirely. // changed, just suppress the message entirely. (The dev server applies the
#runDeferredReportHmrStart() { // same defer to the BUILDING HMR message itself; see `DeferredEmit` usage in
if (this.#deferredReportHmrStartId != null) { // hot-reloader-turbopack.ts.)
console.log('[Fast Refresh] rebuilding') #deferredReportHmrStart: DeferredEmit
this.#reportedHmrStart = true #reportedHmrStart: boolean
this.#cancelDeferredReportHmrStart()
}
}
#cancelDeferredReportHmrStart() { constructor() {
clearTimeout(this.#deferredReportHmrStartId) this.#updatedModules = new Set()
this.#deferredReportHmrStartId = undefined this.#deferredReportHmrStart = new DeferredEmit()
this.#reportedHmrStart = false
} }
onBuilding() { onBuilding() {
this.#lastUpdateMsSinceEpoch = undefined this.#lastUpdateMsSinceEpoch = undefined
this.#cancelDeferredReportHmrStart() this.#deferredReportHmrStart.cancel()
this.#startMsSinceEpoch = Date.now() this.#startMsSinceEpoch = Date.now()
// report the HMR start after a short delay // report the HMR start after a short delay
this.#deferredReportHmrStartId = setTimeout( this.#deferredReportHmrStart.schedule(
() => this.#runDeferredReportHmrStart(),
// debugging feature: don't defer/suppress noisy no-op HMR update messages // debugging feature: don't defer/suppress noisy no-op HMR update messages
self.__NEXT_HMR_TURBOPACK_REPORT_NOISY_NOOP_EVENTS self.__NEXT_HMR_TURBOPACK_REPORT_NOISY_NOOP_EVENTS
? 0 ? 0
: TURBOPACK_HMR_START_DELAY_MS : TURBOPACK_HMR_START_DELAY_MS,
() => {
console.log('[Fast Refresh] rebuilding')
this.#reportedHmrStart = true
}
) )
} }
/** Helper for other `onEvent` methods. */ /** Helper for other `onEvent` methods. */
#onUpdate() { #onUpdate() {
this.#runDeferredReportHmrStart() this.#deferredReportHmrStart.flush()
this.#lastUpdateMsSinceEpoch = Date.now() this.#lastUpdateMsSinceEpoch = Date.now()
} }
...@@ -107,10 +101,10 @@ export class TurbopackHmr { ...@@ -107,10 +101,10 @@ export class TurbopackHmr {
this.#lastUpdateMsSinceEpoch != null && this.#startMsSinceEpoch != null this.#lastUpdateMsSinceEpoch != null && this.#startMsSinceEpoch != null
if (!hasUpdates && !this.#reportedHmrStart) { if (!hasUpdates && !this.#reportedHmrStart) {
// suppress the update entirely // suppress the update entirely
this.#cancelDeferredReportHmrStart() this.#deferredReportHmrStart.cancel()
return null return null
} }
this.#runDeferredReportHmrStart() this.#deferredReportHmrStart.flush()
const result = { const result = {
hasUpdates, hasUpdates,
......
...@@ -104,6 +104,7 @@ import { devIndicatorServerState } from './dev-indicator-server-state' ...@@ -104,6 +104,7 @@ import { devIndicatorServerState } from './dev-indicator-server-state'
import { getDisableDevIndicatorMiddleware } from '../../next-devtools/server/dev-indicator-middleware' import { getDisableDevIndicatorMiddleware } from '../../next-devtools/server/dev-indicator-middleware'
import { getRestartDevServerMiddleware } from '../../next-devtools/server/restart-dev-server-middleware' import { getRestartDevServerMiddleware } from '../../next-devtools/server/restart-dev-server-middleware'
import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events' import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'
import { DeferredEmit } from '../../shared/lib/turbopack/deferred-emit'
import { getSupportedBrowsers } from '../../build/get-supported-browsers' import { getSupportedBrowsers } from '../../build/get-supported-browsers'
import { printBuildErrors } from '../../build/print-build-errors' import { printBuildErrors } from '../../build/print-build-errors'
import { receiveBrowserLogsTurbopack } from './browser-logs/receive-logs' import { receiveBrowserLogsTurbopack } from './browser-logs/receive-logs'
...@@ -712,6 +713,12 @@ export async function createHotReloaderTurbopack( ...@@ -712,6 +713,12 @@ export async function createHotReloaderTurbopack(
// advance it at all when no client is connected. // advance it at all when no client is connected.
let hmrHash = 0 let hmrHash = 0
// HACK: Defer sending `building` messages. Turbopack emits a compile pass for every
// foreground-job cycle, including empty no-op recompiles scheduled by
// request/render activity that changed no files. This allows us to prevent
// sending them if we quickly get a `built` message after a `building` message.
const pendingBuilding = new DeferredEmit()
const clientsWithoutHtmlRequestId = new Set<ws>() const clientsWithoutHtmlRequestId = new Set<ws>()
const clientsByHtmlRequestId = new Map<string, ws>() const clientsByHtmlRequestId = new Map<string, ws>()
const cacheStatusesByHtmlRequestId = new Map<string, ServerCacheStatus>() const cacheStatusesByHtmlRequestId = new Map<string, ServerCacheStatus>()
...@@ -792,6 +799,7 @@ export async function createHotReloaderTurbopack( ...@@ -792,6 +799,7 @@ export async function createHotReloaderTurbopack(
const sendEnqueuedMessagesDebounce = debounce(sendEnqueuedMessages, 2) const sendEnqueuedMessagesDebounce = debounce(sendEnqueuedMessages, 2)
const sendHmr: SendHmr = (id: string, message: HmrMessageSentToBrowser) => { const sendHmr: SendHmr = (id: string, message: HmrMessageSentToBrowser) => {
pendingBuilding.flush()
for (const client of [ for (const client of [
...clientsWithoutHtmlRequestId, ...clientsWithoutHtmlRequestId,
...clientsByHtmlRequestId.values(), ...clientsByHtmlRequestId.values(),
...@@ -809,6 +817,7 @@ export async function createHotReloaderTurbopack( ...@@ -809,6 +817,7 @@ export async function createHotReloaderTurbopack(
// They are currently not handled on the client at all, so might as well not send them for now. // They are currently not handled on the client at all, so might as well not send them for now.
payload.diagnostics = [] payload.diagnostics = []
payload.issues = [] payload.issues = []
pendingBuilding.flush()
for (const client of [ for (const client of [
...clientsWithoutHtmlRequestId, ...clientsWithoutHtmlRequestId,
...@@ -1903,11 +1912,14 @@ export async function createHotReloaderTurbopack( ...@@ -1903,11 +1912,14 @@ export async function createHotReloaderTurbopack(
}) })
async function handleProjectUpdates() { async function handleProjectUpdates() {
const BUILDING_MESSAGE_DEFER_MS = 100
for await (const updateMessage of project.updateInfoSubscribe(30)) { for await (const updateMessage of project.updateInfoSubscribe(30)) {
switch (updateMessage.updateType) { switch (updateMessage.updateType) {
case 'start': { case 'start': {
updateInProgress = true updateInProgress = true
hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.BUILDING }) pendingBuilding.schedule(BUILDING_MESSAGE_DEFER_MS, () => {
hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.BUILDING })
})
// Mark that HMR has started and we need to call the callback after it settles // Mark that HMR has started and we need to call the callback after it settles
// This ensures onBeforeDeferredEntries will be called again during HMR // This ensures onBeforeDeferredEntries will be called again during HMR
if (hasDeferredEntriesConfig) { if (hasDeferredEntriesConfig) {
...@@ -1919,6 +1931,7 @@ export async function createHotReloaderTurbopack( ...@@ -1919,6 +1931,7 @@ export async function createHotReloaderTurbopack(
} }
case 'end': { case 'end': {
updateInProgress = false updateInProgress = false
pendingBuilding.cancel()
if (pendingServerComponentChanges) { if (pendingServerComponentChanges) {
pendingServerComponentChanges = false pendingServerComponentChanges = false
sendServerComponentChanges() sendServerComponentChanges()
......
/**
* Fires a callback after a delay unless it's cancelled or flushed first.
*
* `schedule()` replaces any still-pending emit.
*/
export class DeferredEmit {
#timer: ReturnType<typeof setTimeout> | undefined
#fn: (() => void) | undefined
/**
* Arm `fn` to run after `delayMs`, replacing any still-pending emit.
*
* @param delayMs The delay in milliseconds before the callback is fired.
* @param fn The callback function to be executed after the delay.
*/
schedule(delayMs: number, scheduledFn: () => void): void {
this.cancel()
this.#fn = scheduledFn
this.#timer = setTimeout(() => {
this.#timer = undefined
const fn = this.#fn
this.#fn = undefined
fn?.()
}, delayMs)
}
/**
* If an emit is pending, run it now instead of waiting for the delay.
*/
flush(): void {
if (this.#timer === undefined) {
return
}
clearTimeout(this.#timer)
this.#timer = undefined
const fn = this.#fn
this.#fn = undefined
fn?.()
}
/**
* Cancel a pending emit without running it.
*/
cancel(): void {
if (this.#timer === undefined) {
return
}
clearTimeout(this.#timer)
this.#timer = undefined
this.#fn = undefined
}
get isPending(): boolean {
return this.#timer !== undefined
}
}
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