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

Fix Turbopack HMR recovery after dropped connections (#97966)

### What?

Track the latest applied Turbopack HMR 'hash' in the browser runtime and
include it when subscriptions are restored after reconnecting. Reload
the page when the client's hash differs from the server's current hash.

Add an App Router development test that interrupts HMR traffic, misses
an update, restores the connection, and verifies the page reloads into
the current revision.

### Why?

A browser that temporarily loses its HMR connection can miss updates and
remain out of sync after reconnecting.

### How?

Attach the current HMR hash to Turbopack connection and update messages.
The runtime records the last hash it processed and sends it with each
subscription. The development server compares that value against its
current hash and requests a full reload on mismatch.

<!-- NEXT_JS_LLM -->
parent 8330e4c4
...@@ -386,6 +386,7 @@ export function processMessage( ...@@ -386,6 +386,7 @@ export function processMessage(
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED, type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED,
data: { data: {
sessionId: message.data.sessionId, sessionId: message.data.sessionId,
hmrVersion: message.data.hmrVersion,
}, },
}) })
break break
...@@ -396,6 +397,7 @@ export function processMessage( ...@@ -396,6 +397,7 @@ export function processMessage(
processTurbopackMessage({ processTurbopackMessage({
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE, type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
data: message.data, data: message.data,
hmrVersion: message.hmrVersion,
}) })
if (RuntimeErrorHandler.hadRuntimeError) { if (RuntimeErrorHandler.hadRuntimeError) {
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR) console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
...@@ -406,6 +408,10 @@ export function processMessage( ...@@ -406,6 +408,10 @@ export function processMessage(
} }
// TODO-APP: make server component change more granular // TODO-APP: make server component change more granular
case HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES: { case HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES: {
processTurbopackMessage({
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES,
hmrVersion: message.hmrVersion,
})
turbopackHmr?.onServerComponentChanges() turbopackHmr?.onServerComponentChanges()
sendMessage( sendMessage(
JSON.stringify({ JSON.stringify({
......
...@@ -340,6 +340,9 @@ function processMessage(message: HmrMessageSentToBrowser) { ...@@ -340,6 +340,9 @@ function processMessage(message: HmrMessageSentToBrowser) {
return handleSuccess() return handleSuccess()
} }
case HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES: { case HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES: {
for (const listener of turbopackMessageListeners) {
listener(message)
}
turbopackHmr?.onServerComponentChanges() turbopackHmr?.onServerComponentChanges()
if (hasCompileErrors || RuntimeErrorHandler.hadRuntimeError) { if (hasCompileErrors || RuntimeErrorHandler.hadRuntimeError) {
window.location.reload() window.location.reload()
...@@ -376,6 +379,7 @@ function processMessage(message: HmrMessageSentToBrowser) { ...@@ -376,6 +379,7 @@ function processMessage(message: HmrMessageSentToBrowser) {
listener({ listener({
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE, type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
data: message.data, data: message.data,
hmrVersion: message.hmrVersion,
}) })
} }
if (RuntimeErrorHandler.hadRuntimeError) { if (RuntimeErrorHandler.hadRuntimeError) {
......
...@@ -821,7 +821,7 @@ export async function createHotReloaderTurbopack( ...@@ -821,7 +821,7 @@ export async function createHotReloaderTurbopack(
} }
let hmrEventHappened = false let hmrEventHappened = false
// A counter identifying the current version of the compiled output, included // A counter identifying the current version of the server component output, included
// by `"use cache"` in dev cache keys so that cached entries revalidate after // by `"use cache"` in dev cache keys so that cached entries revalidate after
// an edit. It advances once per HMR change event (for App Router pages that // an edit. It advances once per HMR change event (for App Router pages that
// is an RSC change, which is what a cached render depends on), independent of // is an RSC change, which is what a cached render depends on), independent of
...@@ -829,7 +829,9 @@ export async function createHotReloaderTurbopack( ...@@ -829,7 +829,9 @@ export async function createHotReloaderTurbopack(
// messages: those are sent per connected client on every compilation, so // messages: those are sent per connected client on every compilation, so
// advancing there would both churn the hash without an edit and fail to // advancing there would both churn the hash without an edit and fail to
// advance it at all when no client is connected. // advance it at all when no client is connected.
let hmrHash = 0 let serverComponentsHmrRefreshVersion = 0
let clientHmrVersion = 0
let clientHmrEventHappened = false
// Undefined until the first entrypoints emission. That one has nothing to // Undefined until the first entrypoints emission. That one has nothing to
// compare against, so every route it lists would look added. // compare against, so every route it lists would look added.
let previousRouteKeys: Set<string> | undefined let previousRouteKeys: Set<string> | undefined
...@@ -919,6 +921,7 @@ export async function createHotReloaderTurbopack( ...@@ -919,6 +921,7 @@ export async function createHotReloaderTurbopack(
sendToClient(client, { sendToClient(client, {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE, type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
data: state.turbopackUpdates, data: state.turbopackUpdates,
hmrVersion: String(clientHmrVersion),
}) })
state.turbopackUpdates.length = 0 state.turbopackUpdates.length = 0
} }
...@@ -928,14 +931,23 @@ export async function createHotReloaderTurbopack( ...@@ -928,14 +931,23 @@ export async function createHotReloaderTurbopack(
const sendHmr: SendHmr = (id: string, message: HmrMessageSentToBrowser) => { const sendHmr: SendHmr = (id: string, message: HmrMessageSentToBrowser) => {
pendingBuilding.flush() pendingBuilding.flush()
if (message.type === HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES) {
if (!clientHmrEventHappened) {
clientHmrVersion++
clientHmrEventHappened = true
}
message.hmrVersion = String(clientHmrVersion)
}
hmrEventHappened = true
for (const client of [ for (const client of [
...clientsWithoutHtmlRequestId, ...clientsWithoutHtmlRequestId,
...clientsByHtmlRequestId.values(), ...clientsByHtmlRequestId.values(),
]) { ]) {
clientStates.get(client)?.messages.set(id, message) clientStates.get(client)?.messages.set(id, message)
} }
hmrEventHappened = true
sendEnqueuedMessagesDebounce() sendEnqueuedMessagesDebounce()
} }
...@@ -954,6 +966,10 @@ export async function createHotReloaderTurbopack( ...@@ -954,6 +966,10 @@ export async function createHotReloaderTurbopack(
clientStates.get(client)?.turbopackUpdates.push(payload) clientStates.get(client)?.turbopackUpdates.push(payload)
} }
if (!clientHmrEventHappened) {
clientHmrVersion++
clientHmrEventHappened = true
}
hmrEventHappened = true hmrEventHappened = true
sendEnqueuedMessagesDebounce() sendEnqueuedMessagesDebounce()
} }
...@@ -984,7 +1000,10 @@ export async function createHotReloaderTurbopack( ...@@ -984,7 +1000,10 @@ export async function createHotReloaderTurbopack(
for await (const change of changed) { for await (const change of changed) {
processIssues(currentEntryIssues, key, change, false, true) processIssues(currentEntryIssues, key, change, false, true)
// TODO: Get an actual content hash from Turbopack. // TODO: Get an actual content hash from Turbopack.
const message = await createMessage(change, String(++hmrHash)) const message = await createMessage(
change,
String(++serverComponentsHmrRefreshVersion)
)
if (message) { if (message) {
sendHmr(key, message) sendHmr(key, message)
} }
...@@ -1581,6 +1600,16 @@ export async function createHotReloaderTurbopack( ...@@ -1581,6 +1600,16 @@ export async function createHotReloaderTurbopack(
// Turbopack messages // Turbopack messages
switch (parsedData.type) { switch (parsedData.type) {
case 'turbopack-subscribe': case 'turbopack-subscribe':
if (
parsedData.hmrVersion !== undefined &&
parsedData.hmrVersion !== String(clientHmrVersion)
) {
sendToClient(client, {
type: HMR_MESSAGE_SENT_TO_BROWSER.RELOAD_PAGE,
data: 'HMR hash mismatch',
})
break
}
subscribeToClientHmrEvents(client, parsedData.path) subscribeToClientHmrEvents(client, parsedData.path)
break break
...@@ -1597,7 +1626,7 @@ export async function createHotReloaderTurbopack( ...@@ -1597,7 +1626,7 @@ export async function createHotReloaderTurbopack(
const turbopackConnectedMessage: TurbopackConnectedMessage = { const turbopackConnectedMessage: TurbopackConnectedMessage = {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED, type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED,
data: { sessionId }, data: { sessionId, hmrVersion: String(clientHmrVersion) },
} }
sendToClient(client, turbopackConnectedMessage) sendToClient(client, turbopackConnectedMessage)
...@@ -1658,13 +1687,9 @@ export async function createHotReloaderTurbopack( ...@@ -1658,13 +1687,9 @@ export async function createHotReloaderTurbopack(
}, },
getServerComponentsHmrRefreshHash() { getServerComponentsHmrRefreshHash() {
// Only the change subscription (an actual recompile) advances `hmrHash`; // Only a change subscription advances the refresh version, so reloads
// reloads and config invalidations don't, so the value stays stable // and config invalidations leave cache keys stable until a real edit.
// across requests until a real edit. `sessionId` stands in for a key return `${sessionId}-${serverComponentsHmrRefreshVersion}`
// derived from the compiled implementation, which would let entries
// outlive a restart when the code didn't change (see the note on Action
// IDs in `use-cache-wrapper.ts`).
return `${sessionId}-${hmrHash}`
}, },
sendToLegacyClients(action) { sendToLegacyClients(action) {
...@@ -2105,7 +2130,7 @@ export async function createHotReloaderTurbopack( ...@@ -2105,7 +2130,7 @@ export async function createHotReloaderTurbopack(
// Report the current version without advancing it: a completed // Report the current version without advancing it: a completed
// compilation is not itself an edit, and this hash is not // compilation is not itself an edit, and this hash is not
// consumed by the Turbopack client. // consumed by the Turbopack client.
hash: String(hmrHash), hash: String(serverComponentsHmrRefreshVersion),
errors: [...clientErrors.values()], errors: [...clientErrors.values()],
warnings: [], warnings: [],
}) })
...@@ -2118,6 +2143,7 @@ export async function createHotReloaderTurbopack( ...@@ -2118,6 +2143,7 @@ export async function createHotReloaderTurbopack(
Log.event(`Compiled in ${timeMessage}`) Log.event(`Compiled in ${timeMessage}`)
hmrEventHappened = false hmrEventHappened = false
} }
clientHmrEventHappened = false
// Call onBeforeDeferredEntries after compilation completes during HMR // Call onBeforeDeferredEntries after compilation completes during HMR
// This ensures the callback is invoked even when non-deferred entries change // This ensures the callback is invoked even when non-deferred entries change
......
...@@ -65,6 +65,7 @@ export interface ServerErrorMessage { ...@@ -65,6 +65,7 @@ export interface ServerErrorMessage {
export interface TurbopackMessage { export interface TurbopackMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE
data: TurbopackUpdate | TurbopackUpdate[] data: TurbopackUpdate | TurbopackUpdate[]
hmrVersion: string
} }
export interface BuildingMessage { export interface BuildingMessage {
...@@ -117,6 +118,7 @@ export interface ReloadPageMessage { ...@@ -117,6 +118,7 @@ export interface ReloadPageMessage {
export interface ServerComponentChangesMessage { export interface ServerComponentChangesMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES
hmrVersion?: string
} }
/** /**
...@@ -151,7 +153,7 @@ export interface DevPagesManifestUpdateMessage { ...@@ -151,7 +153,7 @@ export interface DevPagesManifestUpdateMessage {
export interface TurbopackConnectedMessage { export interface TurbopackConnectedMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED
data: { sessionId: number } data: { sessionId: number; hmrVersion: string }
} }
export interface AppIsrManifestMessage { export interface AppIsrManifestMessage {
...@@ -232,10 +234,15 @@ export type TurbopackMessageSentToBrowser = ...@@ -232,10 +234,15 @@ export type TurbopackMessageSentToBrowser =
| { | {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE
data: any data: any
hmrVersion: string
} }
| { | {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED
data: { sessionId: number } data: { sessionId: number; hmrVersion: string }
}
| {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES
hmrVersion?: string
} }
export interface NextJsHotReloaderInterface { export interface NextJsHotReloaderInterface {
......
'use client'
export default function Page() {
return <p id="hmr-value">Initial</p>
}
import { FileRef, nextTestSetup } from 'e2e-utils' import { FileRef, nextTestSetup, Playwright } from 'e2e-utils'
import { retry, waitFor } from 'next-test-utils' import { retry, waitFor } from 'next-test-utils'
import path from 'path' import path from 'path'
...@@ -11,6 +11,86 @@ describe(`app-dir-hmr`, () => { ...@@ -11,6 +11,86 @@ describe(`app-dir-hmr`, () => {
}) })
describe('filesystem changes', () => { describe('filesystem changes', () => {
// @force-gate turbopack
it('reloads the page if the server advanced while the client was disconnected', async () => {
const componentPath = 'app/hmr-reconnect/page.js'
const originalComponent = await next.readFile(componentPath)
let forwardHmrTraffic = true
let droppedHmrMessages = 0
let reconnectHmr = () => {}
const subscriptionHashes: Array<string | undefined> = []
const getHmrValue = (browser: Playwright<unknown>) =>
browser.eval(`document.querySelector('#hmr-value')?.textContent`)
try {
const browser = await next.browser('/hmr-reconnect', {
async beforePageLoad(page) {
await page.routeWebSocket(/\/_next\/hmr/, (clientSocket) => {
const serverSocket = clientSocket.connectToServer()
function connectServerSocket() {
serverSocket.onMessage((message) => {
if (forwardHmrTraffic) {
clientSocket.send(message)
} else {
droppedHmrMessages++
}
})
}
connectServerSocket()
clientSocket.onMessage((message) => {
if (typeof message === 'string') {
const parsed = JSON.parse(message)
if (parsed.type === 'turbopack-subscribe') {
subscriptionHashes.push(parsed.hmrVersion)
}
}
serverSocket.send(message)
})
reconnectHmr = () => serverSocket.close()
})
},
})
await retry(async () => {
expect(await getHmrValue(browser)).toBe('Initial')
})
await next.patchFile(
componentPath,
originalComponent.replace('Initial', 'First edit')
)
await retry(async () => {
expect(await getHmrValue(browser)).toBe('First edit')
})
await browser.eval(`window.__hmrTestDocument = true`)
forwardHmrTraffic = false
await next.patchFile(
componentPath,
originalComponent.replace('Initial', 'Second edit')
)
await retry(async () => {
expect(droppedHmrMessages).toBeGreaterThan(0)
})
expect(await getHmrValue(browser)).not.toBe('Second edit')
expect(await browser.eval(`window.__hmrTestDocument`)).toBe(true)
forwardHmrTraffic = true
reconnectHmr()
await retry(async () => {
expect(subscriptionHashes.some((hash) => hash !== undefined)).toBe(
true
)
})
await retry(async () => {
expect(await getHmrValue(browser)).toBe('Second edit')
}, 10_000)
expect(await browser.eval(`window.__hmrTestDocument`)).toBeUndefined()
} finally {
await next.patchFile(componentPath, originalComponent)
}
})
it('should not continously poll when hitting a not found page', async () => { it('should not continously poll when hitting a not found page', async () => {
let requestCount = 0 let requestCount = 0
......
...@@ -7,10 +7,16 @@ type SendMessage = (msg: any) => void ...@@ -7,10 +7,16 @@ type SendMessage = (msg: any) => void
export type WebSocketMessage = export type WebSocketMessage =
| { | {
type: 'turbopack-connected' type: 'turbopack-connected'
data?: { hmrVersion: string }
} }
| { | {
type: 'turbopack-message' type: 'turbopack-message'
data: Record<string, any> data: Record<string, any>
hmrVersion?: string
}
| {
type: 'server-component-changes'
hmrVersion?: string
} }
export type ClientOptions = { export type ClientOptions = {
...@@ -23,6 +29,8 @@ export type ClientOptions = { ...@@ -23,6 +29,8 @@ export type ClientOptions = {
export const TURBOPACK_CHUNK_UPDATE_LISTENERS_GLOBAL = export const TURBOPACK_CHUNK_UPDATE_LISTENERS_GLOBAL =
'TURBOPACK_CHUNK_UPDATE_LISTENERS' 'TURBOPACK_CHUNK_UPDATE_LISTENERS'
let lastSeenHmrVersion: string | undefined
export function connect({ export function connect({
addMessageListener, addMessageListener,
sendMessage, sendMessage,
...@@ -32,9 +40,13 @@ export function connect({ ...@@ -32,9 +40,13 @@ export function connect({
addMessageListener((msg) => { addMessageListener((msg) => {
switch (msg.type) { switch (msg.type) {
case 'turbopack-connected': case 'turbopack-connected':
if (lastSeenHmrVersion === undefined && msg.data !== undefined) {
lastSeenHmrVersion = msg.data.hmrVersion
}
handleSocketConnected(sendMessage) handleSocketConnected(sendMessage)
break break
default: case 'turbopack-message':
lastSeenHmrVersion = msg.hmrVersion
try { try {
if (Array.isArray(msg.data)) { if (Array.isArray(msg.data)) {
for (let i = 0; i < msg.data.length; i++) { for (let i = 0; i < msg.data.length; i++) {
...@@ -57,6 +69,11 @@ export function connect({ ...@@ -57,6 +69,11 @@ export function connect({
location.reload() location.reload()
} }
break break
case 'server-component-changes':
lastSeenHmrVersion = msg.hmrVersion
break
default:
break
} }
}) })
...@@ -108,6 +125,7 @@ function subscribeToUpdates( ...@@ -108,6 +125,7 @@ function subscribeToUpdates(
sendJSON(sendMessage, { sendJSON(sendMessage, {
type: 'turbopack-subscribe', type: 'turbopack-subscribe',
...resource, ...resource,
hmrVersion: lastSeenHmrVersion,
}) })
return () => { return () => {
......
...@@ -98,6 +98,7 @@ type ResourceIdentifier = { ...@@ -98,6 +98,7 @@ type ResourceIdentifier = {
type ClientMessageSubscribe = { type ClientMessageSubscribe = {
type: 'turbopack-subscribe' type: 'turbopack-subscribe'
hmrVersion?: string
} & ResourceIdentifier } & ResourceIdentifier
type ClientMessageUnsubscribe = { type ClientMessageUnsubscribe = {
......
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