Commit 091276aa authored by Luke Sandberg's avatar Luke Sandberg

Fix error overlay not showing on dev error shell

The dev error shell dispatches errors to the overlay via
dispatcher.onUnhandledError + dispatcher.openErrorOverlay (app router
defaults the overlay to closed). The error is stored by hydrate() in
app-index.tsx and read by the entry points (app-next-dev.ts and
app-next-turbopack.ts) after renderAppDevOverlay() creates the React
root, ensuring the event queue is ready to receive the error.

Previously, handleClientError() was used but that only pushes to a
queue consumed by useErrorHandler in HotReloader, which never mounts
in the shell. Now we use the dev overlay's own dispatcher.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent e4d8bd00
......@@ -346,6 +346,14 @@ export type ClientInstrumentationHooks = {
) => void
}
// When the dev error shell is served, this holds the error parsed from
// the <template> tag. The caller (app-next-dev.ts) reads it after
// hydrate() returns to dispatch to the dev overlay.
let _pendingShellError: Error | null = null
export function getPendingShellError(): Error | null {
return _pendingShellError
}
export async function hydrate(
instrumentationHooks: ClientInstrumentationHooks | null,
assetPrefix: string
......@@ -372,14 +380,15 @@ export async function hydrate(
// Note: this is distinct from __next_error__ which is used by
// app-render.tsx for SSR errors that have valid RSC payloads.
//
// Replay any error embedded in the <template> tag so the overlay
// shows the initial SSR/compilation error immediately.
// Store the error from the <template> tag so the caller
// (app-next-dev.ts) can dispatch it to the dev overlay after
// mounting. We can't dispatch here because the HotReloader never
// mounts in this shell and handleClientError wouldn't reach the
// overlay, and the dev overlay hasn't been rendered yet.
const ssrErrorTemplate = document.querySelector(
'template[data-next-error-message]'
)
if (ssrErrorTemplate) {
const { handleClientError } =
require('../next-devtools/userspace/app/errors/use-error-handler') as typeof import('../next-devtools/userspace/app/errors/use-error-handler')
const message = ssrErrorTemplate.getAttribute(
'data-next-error-message'
)!
......@@ -390,7 +399,7 @@ export async function hydrate(
;(error as any).digest = digest
}
error.stack = stack || ''
handleClientError(error)
_pendingShellError = error
}
return
}
......
......@@ -2,7 +2,10 @@
import './app-webpack'
import { renderAppDevOverlay } from 'next/dist/compiled/next-devtools'
import {
dispatcher,
renderAppDevOverlay,
} from 'next/dist/compiled/next-devtools'
import { appBootstrap } from './app-bootstrap'
import { getOwnerStack } from '../next-devtools/userspace/app/errors/stitched-error'
import { isRecoverableError } from './react-client-callbacks/on-recoverable-error'
......@@ -13,10 +16,22 @@ const instrumentationHooks = require('../lib/require-instrumentation-client')
appBootstrap((assetPrefix) => {
const enableCacheIndicator = process.env.__NEXT_CACHE_COMPONENTS
const { hydrate } = require('./app-index') as typeof import('./app-index')
const { hydrate, getPendingShellError } =
require('./app-index') as typeof import('./app-index')
try {
hydrate(instrumentationHooks, assetPrefix)
} finally {
renderAppDevOverlay(getOwnerStack, isRecoverableError, enableCacheIndicator)
// If the dev error shell was served, dispatch the embedded error to the
// overlay. This must happen after renderAppDevOverlay() so the overlay's
// event queue is set up. Read after hydrate() since the synchronous
// part of hydrate() sets it before returning.
// Also open the error overlay (app router defaults to closed).
const shellError = getPendingShellError()
if (shellError) {
dispatcher.onUnhandledError(shellError)
dispatcher.openErrorOverlay()
}
}
})
......@@ -9,7 +9,8 @@ window.next.turbopack = true
const instrumentationHooks = require('../lib/require-instrumentation-client')
appBootstrap((assetPrefix) => {
const { hydrate } = require('./app-index') as typeof import('./app-index')
const { hydrate, getPendingShellError } =
require('./app-index') as typeof import('./app-index')
try {
hydrate(instrumentationHooks, assetPrefix)
} finally {
......@@ -17,13 +18,22 @@ appBootstrap((assetPrefix) => {
const enableCacheIndicator = process.env.__NEXT_CACHE_COMPONENTS
const { getOwnerStack } =
require('../next-devtools/userspace/app/errors/stitched-error') as typeof import('../next-devtools/userspace/app/errors/stitched-error')
const { renderAppDevOverlay } =
const { dispatcher, renderAppDevOverlay } =
require('next/dist/compiled/next-devtools') as typeof import('next/dist/compiled/next-devtools')
renderAppDevOverlay(
getOwnerStack,
isRecoverableError,
enableCacheIndicator
)
// If the dev error shell was served, dispatch the embedded error to the
// overlay after mounting it, and open the error overlay automatically
// (app router defaults to overlay closed).
const shellError = getPendingShellError()
if (shellError) {
dispatcher.onUnhandledError(shellError)
dispatcher.openErrorOverlay()
}
}
}
})
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