Skip to content

Commit

Permalink
Unify serverError hmr event
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Sep 4, 2023
1 parent be3a7e4 commit 1d0a69a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
80 changes: 41 additions & 39 deletions packages/next/src/client/dev/webpack-hot-middleware-client.ts
Expand Up @@ -11,54 +11,56 @@ export default () => {
window.next.router.pathname === '/404' ||
window.next.router.pathname === '/_error'

if (obj.action === 'reloadPage') {
sendMessage(
JSON.stringify({
event: 'client-reload-page',
clientId: window.__nextDevClientId,
})
)
return window.location.reload()
}
if (obj.action === 'removedPage') {
const [page] = obj.data
if (page === window.next.router.pathname || isOnErrorPage) {
switch (obj.action) {
case 'reloadPage': {
sendMessage(
JSON.stringify({
event: 'client-removed-page',
event: 'client-reload-page',
clientId: window.__nextDevClientId,
page,
})
)
return window.location.reload()
}
return
}
if (obj.action === 'addedPage') {
const [page] = obj.data
if (
(page === window.next.router.pathname &&
typeof window.next.router.components[page] === 'undefined') ||
isOnErrorPage
) {
sendMessage(
JSON.stringify({
event: 'client-added-page',
clientId: window.__nextDevClientId,
page,
})
)
return window.location.reload()
case 'removedPage': {
const [page] = obj.data
if (page === window.next.router.pathname || isOnErrorPage) {
sendMessage(
JSON.stringify({
event: 'client-removed-page',
clientId: window.__nextDevClientId,
page,
})
)
return window.location.reload()
}
return
}
case 'addedPage': {
const [page] = obj.data
if (
(page === window.next.router.pathname &&
typeof window.next.router.components[page] === 'undefined') ||
isOnErrorPage
) {
sendMessage(
JSON.stringify({
event: 'client-added-page',
clientId: window.__nextDevClientId,
page,
})
)
return window.location.reload()
}
return
}
case 'serverError':
case 'devPagesManifestUpdate': {
return
}
default: {
throw new Error('Unexpected action ' + obj.action)
}
return
}
if (
obj.action === 'serverError' ||
obj.action === 'devPagesManifestUpdate'
) {
return
}
throw new Error('Unexpected action ' + obj.action)
})

return devClient
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/page-bootstrap.ts
Expand Up @@ -17,7 +17,7 @@ export function pageBootrap(assetPrefix: string) {
let buildIndicatorHandler: (obj: Record<string, any>) => void = () => {}

function devPagesHmrListener(payload: any) {
if (payload.event === 'server-error' && payload.errorJSON) {
if (payload.action === 'serverError' && payload.errorJSON) {
const { stack, message } = JSON.parse(payload.errorJSON)
const error = new Error(message)
error.stack = stack
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/server/dev/on-demand-entry-handler.ts
Expand Up @@ -949,8 +949,7 @@ export function onDemandEntryHandler({
if (!bufferedHmrServerError && error) {
client.send(
JSON.stringify({
event: 'server-error', // for pages dir
action: 'serverError', // for app dir
action: 'serverError',
errorJSON: stringifyError(error),
})
)
Expand Down

0 comments on commit 1d0a69a

Please sign in to comment.