Skip to content

Commit abfc98f

Browse files
authored
Streamline HMR message handling (#83336)
For unknown reasons, 3 of the 17 different HMR messages used an `event` property instead of `action`, leading to an unnecessarily convoluted handling of the HMR messages, where the `in` operator had to be used. > [!TIP] > Best reviewed with hidden whitespace changes.
1 parent 2bcb4bd commit abfc98f

File tree

8 files changed

+120
-128
lines changed

8 files changed

+120
-128
lines changed

packages/next/src/client/dev/amp-dev.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ async function tryApplyUpdates() {
7777
}
7878

7979
addMessageListener((message) => {
80-
if (!('action' in message)) {
81-
return
82-
}
83-
8480
try {
8581
// actions which are not related to amp-dev
8682
if (

packages/next/src/client/dev/hot-reloader/app/hot-reloader-app.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,6 @@ export function processMessage(
202202
processTurbopackMessage: (msg: TurbopackMsgToBrowser) => void,
203203
staticIndicatorState: StaticIndicatorState
204204
) {
205-
if (!('action' in obj)) {
206-
return
207-
}
208-
209205
function handleErrors(errors: ReadonlyArray<unknown>) {
210206
// "Massage" webpack messages.
211207
const formatted = formatWebpackMessages({
@@ -452,6 +448,11 @@ export function processMessage(
452448
dispatcher.onDevToolsConfig(obj.data)
453449
return
454450
}
451+
case HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES:
452+
case HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES:
453+
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES:
454+
// These action types are handled in src/client/page-bootstrap.ts
455+
break
455456
default: {
456457
obj satisfies never
457458
}

packages/next/src/client/dev/hot-reloader/pages/hot-reloader-pages.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ export default function connect() {
7474
register()
7575

7676
addMessageListener((payload) => {
77-
if (!('action' in payload)) {
78-
return
79-
}
80-
8177
try {
8278
processMessage(payload)
8379
} catch (err: unknown) {
@@ -253,10 +249,6 @@ export function handleStaticIndicator() {
253249

254250
/** Handles messages from the server for the Pages Router. */
255251
function processMessage(obj: HMR_ACTION_TYPES) {
256-
if (!('action' in obj)) {
257-
return
258-
}
259-
260252
switch (obj.action) {
261253
case HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST: {
262254
isrManifest = obj.data
@@ -374,6 +366,11 @@ function processMessage(obj: HMR_ACTION_TYPES) {
374366
case HMR_ACTIONS_SENT_TO_BROWSER.DEVTOOLS_CONFIG:
375367
dispatcher.onDevToolsConfig(obj.data)
376368
break
369+
case HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES:
370+
case HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES:
371+
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES:
372+
// These action types are handled in src/client/page-bootstrap.ts
373+
break
377374
default:
378375
obj satisfies never
379376
}

packages/next/src/client/dev/hot-reloader/pages/websocket.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ export function connectHMR(options: { path: string; assetPrefix: string }) {
4949
// Coerce into HMR_ACTION_TYPES as that is the format.
5050
const msg: HMR_ACTION_TYPES = JSON.parse(event.data)
5151

52-
if (
53-
'action' in msg &&
54-
msg.action === HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED
55-
) {
52+
if (msg.action === HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED) {
5653
if (
5754
serverSessionId !== null &&
5855
serverSessionId !== msg.data.sessionId

packages/next/src/client/page-bootstrap.ts

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -26,111 +26,106 @@ export function pageBootstrap(assetPrefix: string) {
2626

2727
addMessageListener((payload) => {
2828
if (reloading) return
29-
if ('action' in payload) {
30-
switch (payload.action) {
31-
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR: {
32-
const { stack, message } = JSON.parse(payload.errorJSON)
33-
const error = new Error(message)
34-
error.stack = stack
35-
throw error
36-
}
37-
case HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE: {
38-
reloading = true
39-
window.location.reload()
40-
break
41-
}
42-
case HMR_ACTIONS_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE: {
43-
fetch(
44-
`${assetPrefix}/_next/static/development/_devPagesManifest.json`
45-
)
46-
.then((res) => res.json())
47-
.then((manifest) => {
48-
window.__DEV_PAGES_MANIFEST = manifest
49-
})
50-
.catch((err) => {
51-
console.log(`Failed to fetch devPagesManifest`, err)
52-
})
53-
break
54-
}
55-
case HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE:
56-
case HMR_ACTIONS_SENT_TO_BROWSER.REMOVED_PAGE:
57-
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES:
58-
case HMR_ACTIONS_SENT_TO_BROWSER.SYNC:
59-
case HMR_ACTIONS_SENT_TO_BROWSER.BUILT:
60-
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING:
61-
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE:
62-
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED:
63-
case HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST:
64-
case HMR_ACTIONS_SENT_TO_BROWSER.DEVTOOLS_CONFIG:
65-
// Most of these action types are handled in
66-
// src/client/dev/hot-reloader/pages/hot-reloader-pages.ts
67-
break
68-
default:
69-
payload satisfies never
29+
30+
switch (payload.action) {
31+
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR: {
32+
const { stack, message } = JSON.parse(payload.errorJSON)
33+
const error = new Error(message)
34+
error.stack = stack
35+
throw error
7036
}
71-
} else if ('event' in payload) {
72-
switch (payload.event) {
73-
case HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES: {
74-
return window.location.reload()
75-
}
76-
case HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES: {
77-
// This is used in `../server/dev/turbopack-utils.ts`.
78-
const isOnErrorPage = window.next.router.pathname === '/_error'
79-
// On the error page we want to reload the page when a page was changed
80-
if (isOnErrorPage) {
81-
if (RuntimeErrorHandler.hadRuntimeError) {
82-
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
83-
}
84-
reloading = true
85-
performFullReload(null)
86-
}
87-
break
88-
}
89-
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES: {
37+
case HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE: {
38+
reloading = true
39+
window.location.reload()
40+
break
41+
}
42+
case HMR_ACTIONS_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE: {
43+
fetch(
44+
`${assetPrefix}/_next/static/development/_devPagesManifest.json`
45+
)
46+
.then((res) => res.json())
47+
.then((manifest) => {
48+
window.__DEV_PAGES_MANIFEST = manifest
49+
})
50+
.catch((err) => {
51+
console.log(`Failed to fetch devPagesManifest`, err)
52+
})
53+
break
54+
}
55+
case HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES: {
56+
return window.location.reload()
57+
}
58+
case HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES: {
59+
// This is used in `../server/dev/turbopack-utils.ts`.
60+
const isOnErrorPage = window.next.router.pathname === '/_error'
61+
// On the error page we want to reload the page when a page was changed
62+
if (isOnErrorPage) {
9063
if (RuntimeErrorHandler.hadRuntimeError) {
9164
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
92-
performFullReload(null)
9365
}
66+
reloading = true
67+
performFullReload(null)
68+
}
69+
break
70+
}
71+
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES: {
72+
if (RuntimeErrorHandler.hadRuntimeError) {
73+
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
74+
performFullReload(null)
75+
}
9476

95-
const { pages } = payload
77+
const { pages } = payload
9678

97-
// Make sure to reload when the dev-overlay is showing for an
98-
// API route
99-
// TODO: Fix `__NEXT_PAGE` type
100-
if (pages.includes(router.query.__NEXT_PAGE as string)) {
101-
return window.location.reload()
102-
}
79+
// Make sure to reload when the dev-overlay is showing for an
80+
// API route
81+
// TODO: Fix `__NEXT_PAGE` type
82+
if (pages.includes(router.query.__NEXT_PAGE as string)) {
83+
return window.location.reload()
84+
}
10385

104-
if (!router.clc && pages.includes(router.pathname)) {
105-
console.log('Refreshing page data due to server-side change')
106-
dispatcher.buildingIndicatorShow()
107-
const clearIndicator = dispatcher.buildingIndicatorHide
86+
if (!router.clc && pages.includes(router.pathname)) {
87+
console.log('Refreshing page data due to server-side change')
88+
dispatcher.buildingIndicatorShow()
89+
const clearIndicator = dispatcher.buildingIndicatorHide
10890

109-
router
110-
.replace(
111-
router.pathname +
112-
'?' +
113-
String(
114-
assign(
115-
urlQueryToSearchParams(router.query),
116-
new URLSearchParams(location.search)
117-
)
118-
),
119-
router.asPath,
120-
{ scroll: false }
121-
)
122-
.catch(() => {
123-
// trigger hard reload when failing to refresh data
124-
// to show error overlay properly
125-
location.reload()
126-
})
127-
.finally(clearIndicator)
128-
}
129-
break
91+
router
92+
.replace(
93+
router.pathname +
94+
'?' +
95+
String(
96+
assign(
97+
urlQueryToSearchParams(router.query),
98+
new URLSearchParams(location.search)
99+
)
100+
),
101+
router.asPath,
102+
{ scroll: false }
103+
)
104+
.catch(() => {
105+
// trigger hard reload when failing to refresh data
106+
// to show error overlay properly
107+
location.reload()
108+
})
109+
.finally(clearIndicator)
130110
}
131-
default:
132-
payload satisfies never
111+
break
133112
}
113+
case HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE:
114+
case HMR_ACTIONS_SENT_TO_BROWSER.REMOVED_PAGE:
115+
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES:
116+
case HMR_ACTIONS_SENT_TO_BROWSER.SYNC:
117+
case HMR_ACTIONS_SENT_TO_BROWSER.BUILT:
118+
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING:
119+
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE:
120+
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED:
121+
case HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST:
122+
case HMR_ACTIONS_SENT_TO_BROWSER.DEVTOOLS_CONFIG:
123+
// Most of these action types are handled in
124+
// src/client/dev/hot-reloader/pages/hot-reloader-pages.ts and
125+
// src/client/dev/hot-reloader/app/hot-reloader-app.tsx
126+
break
127+
default:
128+
payload satisfies never
134129
}
135130
})
136131
})

packages/next/src/server/dev/hot-reloader-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ interface ServerComponentChangesAction {
9191
}
9292

9393
interface MiddlewareChangesAction {
94-
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES
94+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES
9595
}
9696

9797
interface ClientChangesAction {
98-
event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES
98+
action: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES
9999
}
100100

101101
interface ServerOnlyChangesAction {
102-
event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES
102+
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES
103103
pages: ReadonlyArray<string>
104104
}
105105

packages/next/src/server/dev/hot-reloader-webpack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,13 +1466,13 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
14661466

14671467
if (middlewareChanges.length > 0) {
14681468
this.send({
1469-
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
1469+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
14701470
})
14711471
}
14721472

14731473
if (pageChanges.length > 0) {
14741474
this.send({
1475-
event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
1475+
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
14761476
pages: serverOnlyChanges.map((pg) =>
14771477
denormalizePagePath(pg.slice('pages'.length))
14781478
),

packages/next/src/server/dev/turbopack-utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export async function handleRouteType({
266266
// Report the next compilation again
267267
readyIds?.delete(pathname)
268268
return {
269-
event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
269+
action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
270270
pages: [page],
271271
}
272272
},
@@ -283,7 +283,7 @@ export async function handleRouteType({
283283
route.htmlEndpoint,
284284
() => {
285285
return {
286-
event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
286+
action: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
287287
}
288288
},
289289
(e) => {
@@ -650,12 +650,12 @@ export async function handleEntrypoints({
650650
await dev?.hooks.unsubscribeFromChanges(key)
651651
currentEntryIssues.delete(key)
652652
dev.hooks.sendHmr('middleware', {
653-
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
653+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
654654
})
655655
} else if (!currentEntrypoints.global.middleware && middleware) {
656656
// Went from no middleware to middleware
657657
dev.hooks.sendHmr('middleware', {
658-
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
658+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
659659
})
660660
}
661661

@@ -763,11 +763,13 @@ export async function handleEntrypoints({
763763
})
764764

765765
finishBuilding?.()
766-
return { event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES }
766+
return {
767+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
768+
}
767769
},
768770
() => {
769771
return {
770-
event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
772+
action: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES,
771773
}
772774
}
773775
)
@@ -879,7 +881,9 @@ export async function handlePagesErrorRoute({
879881
() => {
880882
// There's a special case for this in `../client/page-bootstrap.ts`.
881883
// https://github.com/vercel/next.js/blob/08d7a7e5189a835f5dcb82af026174e587575c0e/packages/next/src/client/page-bootstrap.ts#L69-L71
882-
return { event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES }
884+
return {
885+
action: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
886+
}
883887
},
884888
() => {
885889
return {
@@ -932,7 +936,9 @@ export async function handlePagesErrorRoute({
932936
() => {
933937
// There's a special case for this in `../client/page-bootstrap.ts`.
934938
// https://github.com/vercel/next.js/blob/08d7a7e5189a835f5dcb82af026174e587575c0e/packages/next/src/client/page-bootstrap.ts#L69-L71
935-
return { event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES }
939+
return {
940+
action: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
941+
}
936942
},
937943
(e) => {
938944
return {

0 commit comments

Comments
 (0)