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

Turbopack HMR: Log when more errors cause full page reload (#63220)

Turbopack HMR: Log when more errors cause full page reload

Depends on vercel/turbo#7715

This adds messaging when HMR updates fail because:

- An HMR update could not be applied, such as updating an anonymous
function component
- An update follows a server-rendered error

Test Plan: See now passing tests in the manifest

Closes PACK-2728
parent fd692b61
This diff is collapsed.
......@@ -37,11 +37,11 @@ swc_core = { version = "0.90.22", features = [
testing = { version = "0.35.20" }
# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240314.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240315.2" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240314.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240315.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240314.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240315.2" }
# General Deps
......
......@@ -142,6 +142,7 @@ function performFullReload(err: any, sendMessage: any) {
event: 'client-full-reload',
stackTrace,
hadRuntimeError: !!RuntimeErrorHandler.hadRuntimeError,
dependencyChain: err ? err.dependencyChain : undefined,
})
)
......@@ -514,7 +515,9 @@ export default function HotReload({
const webSocketRef = useWebsocket(assetPrefix)
useWebsocketPing(webSocketRef)
const sendMessage = useSendMessage(webSocketRef)
const processTurbopackMessage = useTurbopack(sendMessage)
const processTurbopackMessage = useTurbopack(sendMessage, (err) =>
performFullReload(err, sendMessage)
)
const router = useRouter()
......
......@@ -33,7 +33,10 @@ export function useSendMessage(webSocketRef: ReturnType<typeof useWebsocket>) {
return sendMessage
}
export function useTurbopack(sendMessage: ReturnType<typeof useSendMessage>) {
export function useTurbopack(
sendMessage: ReturnType<typeof useSendMessage>,
onUpdateError: (err: unknown) => void
) {
const turbopackState = useRef<{
init: boolean
queue: Array<TurbopackMsgToBrowser> | undefined
......@@ -78,9 +81,10 @@ export function useTurbopack(sendMessage: ReturnType<typeof useSendMessage>) {
current.queue = undefined
},
sendMessage,
onUpdateError,
})
})
}, [sendMessage])
}, [sendMessage, onUpdateError])
return processTurbopackMessage
}
......
......@@ -46,6 +46,7 @@ import type {
} from '../../../../server/dev/hot-reloader-types'
import { extractModulesFromTurbopackMessage } from '../../../../server/dev/extract-modules-from-turbopack-message'
import { REACT_REFRESH_FULL_RELOAD_FROM_ERROR } from '../shared'
import { RuntimeErrorHandler } from '../internal/helpers/runtime-error-handler'
// This alternative WebpackDevServer combines the functionality of:
// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
......@@ -65,7 +66,6 @@ declare global {
window.__nextDevClientId = Math.round(Math.random() * 100 + Date.now())
let hadRuntimeError = false
let customHmrEventHandler: any
let turbopackMessageListeners: ((msg: TurbopackMsgToBrowser) => void)[] = []
let MODE: 'webpack' | 'turbopack' = 'webpack'
......@@ -92,7 +92,7 @@ export default function connect(mode: 'webpack' | 'turbopack') {
customHmrEventHandler = handler
},
onUnrecoverableError() {
hadRuntimeError = true
RuntimeErrorHandler.hadRuntimeError = true
},
addTurbopackMessageListener(cb: (msg: TurbopackMsgToBrowser) => void) {
turbopackMessageListeners.push(cb)
......@@ -100,6 +100,9 @@ export default function connect(mode: 'webpack' | 'turbopack') {
sendTurbopackMessage(msg: string) {
sendMessage(msg)
},
handleUpdateError(err: unknown) {
performFullReload(err)
},
}
}
......@@ -343,7 +346,7 @@ function processMessage(obj: HMR_ACTION_TYPES) {
data: obj.data,
})
}
if (hadRuntimeError) {
if (RuntimeErrorHandler.hadRuntimeError) {
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
performFullReload(null)
}
......@@ -409,7 +412,7 @@ function tryApplyUpdates(
}
function handleApplyUpdates(err: any, updatedModules: string[] | null) {
if (err || hadRuntimeError || !updatedModules) {
if (err || RuntimeErrorHandler.hadRuntimeError || !updatedModules) {
if (err) {
console.warn(
'[Fast Refresh] performing full reload\n\n' +
......@@ -419,7 +422,7 @@ function tryApplyUpdates(
'It is also possible the parent component of the component you edited is a class component, which disables Fast Refresh.\n' +
'Fast Refresh requires at least one parent function component in your React tree.'
)
} else if (hadRuntimeError) {
} else if (RuntimeErrorHandler.hadRuntimeError) {
console.warn(
'[Fast Refresh] performing full reload because your application had an unrecoverable error'
)
......@@ -478,7 +481,7 @@ function tryApplyUpdates(
)
}
function performFullReload(err: any) {
export function performFullReload(err: any) {
const stackTrace =
err &&
((err.stack && err.stack.split('\n').slice(0, 5).join('\n')) ||
......@@ -489,7 +492,8 @@ function performFullReload(err: any) {
JSON.stringify({
event: 'client-full-reload',
stackTrace,
hadRuntimeError: !!hadRuntimeError,
hadRuntimeError: !!RuntimeErrorHandler.hadRuntimeError,
dependencyChain: err ? err.dependencyChain : undefined,
})
)
......
......@@ -44,6 +44,7 @@ initialize({
devClient.addTurbopackMessageListener(cb)
},
sendMessage: devClient.sendTurbopackMessage,
onUpdateError: devClient.handleUpdateError,
})
return pageBootrap(assetPrefix)
......
......@@ -12,6 +12,9 @@ import {
urlQueryToSearchParams,
} from '../shared/lib/router/utils/querystring'
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../server/dev/hot-reloader-types'
import { RuntimeErrorHandler } from './components/react-dev-overlay/internal/helpers/runtime-error-handler'
import { REACT_REFRESH_FULL_RELOAD_FROM_ERROR } from './components/react-dev-overlay/shared'
import { performFullReload } from './components/react-dev-overlay/pages/hot-reloader-client'
export function pageBootrap(assetPrefix: string) {
connectHMR({ assetPrefix, path: '/_next/webpack-hmr' })
......@@ -69,7 +72,13 @@ export function pageBootrap(assetPrefix: string) {
// This is used in `../server/dev/turbopack-utils.ts`.
const isOnErrorPage = window.next.router.pathname === '/_error'
// On the error page we want to reload the page when a page was changed
if (isOnErrorPage) return window.location.reload()
if (isOnErrorPage) {
if (RuntimeErrorHandler.hadRuntimeError) {
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
}
reloading = true
performFullReload(null)
}
break
}
case HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES: {
......
......@@ -580,10 +580,21 @@ export async function createHotReloaderTurbopack(
case 'client-reload-page': // { clientId }
case 'client-removed-page': // { page }
case 'client-full-reload': // { stackTrace, hadRuntimeError }
const { hadRuntimeError } = parsedData
const { hadRuntimeError, dependencyChain } = parsedData
if (hadRuntimeError) {
Log.warn(FAST_REFRESH_RUNTIME_RELOAD)
}
if (
Array.isArray(dependencyChain) &&
typeof dependencyChain[0] === 'string'
) {
const cleanedModulePath = dependencyChain[0]
.replace(/^\[project\]/, '.')
.replace(/ \[.*\] \(.*\)$/, '')
Log.warn(
`Fast Refresh had to perform a full reload when ${cleanedModulePath} changed. Read more: https://nextjs.org/docs/messages/fast-refresh-reload`
)
}
break
case 'client-added-page':
// TODO
......
......@@ -1573,6 +1573,8 @@
"basic HMR, basePath: \"\" Error Recovery should recover from 404 after a page has been added with dynamic segments",
"basic HMR, basePath: \"\" Error Recovery should recover from errors in getInitialProps in client",
"basic HMR, basePath: \"\" Error Recovery should recover from errors in the render function",
"basic HMR, basePath: \"\" Full reload should warn about full reload in cli output - anonymous page function",
"basic HMR, basePath: \"\" Full reload should warn about full reload in cli output - runtime-error",
"basic HMR, basePath: \"\" Hot Module Reloading delete a page and add it back should load the page properly",
"basic HMR, basePath: \"\" Hot Module Reloading editing a page should detect the changes and display it",
"basic HMR, basePath: \"\" Hot Module Reloading editing a page should not reload unrelated pages",
......@@ -1595,6 +1597,8 @@
"basic HMR, basePath: \"/docs\" Error Recovery should recover from 404 after a page has been added with dynamic segments",
"basic HMR, basePath: \"/docs\" Error Recovery should recover from errors in getInitialProps in client",
"basic HMR, basePath: \"/docs\" Error Recovery should recover from errors in the render function",
"basic HMR, basePath: \"/docs\" Full reload should warn about full reload in cli output - anonymous page function",
"basic HMR, basePath: \"/docs\" Full reload should warn about full reload in cli output - runtime-error",
"basic HMR, basePath: \"/docs\" Hot Module Reloading delete a page and add it back should load the page properly",
"basic HMR, basePath: \"/docs\" Hot Module Reloading editing a page should detect the changes and display it",
"basic HMR, basePath: \"/docs\" Hot Module Reloading editing a page should not reload unrelated pages",
......@@ -1608,11 +1612,7 @@
],
"failed": [
"basic HMR, basePath: \"\" Error Recovery should recover after webpack parse error in an imported file",
"basic HMR, basePath: \"\" Full reload should warn about full reload in cli output - anonymous page function",
"basic HMR, basePath: \"\" Full reload should warn about full reload in cli output - runtime-error",
"basic HMR, basePath: \"/docs\" Error Recovery should recover after webpack parse error in an imported file",
"basic HMR, basePath: \"/docs\" Full reload should warn about full reload in cli output - anonymous page function",
"basic HMR, basePath: \"/docs\" Full reload should warn about full reload in cli output - runtime-error"
"basic HMR, basePath: \"/docs\" Error Recovery should recover after webpack parse error in an imported file"
],
"pending": [],
"flakey": [],
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