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