Unverified Commit 86aef9d9 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Only log unrecoverable SSR shell errors once (#76484)

parent 000a1427
......@@ -26,8 +26,6 @@ import { createInitialRouterState } from './components/router-reducer/create-ini
import { MissingSlotContext } from '../shared/lib/app-router-context.shared-runtime'
import { setAppBuildId } from './app-build-id'
import { shouldRenderRootLevelErrorOverlay } from './lib/is-error-thrown-while-rendering-rsc'
import { handleClientError } from './components/errors/use-error-handler'
import { isNextRouterError } from './components/is-next-router-error'
/// <reference types="react-dom/experimental" />
......@@ -231,16 +229,6 @@ function Root({ children }: React.PropsWithChildren<{}>) {
}, [])
}
if (process.env.NODE_ENV !== 'production') {
const ssrError = devQueueSsrError()
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useEffect(() => {
if (ssrError) {
handleClientError(ssrError, [])
}
}, [ssrError])
}
return children
}
......@@ -295,26 +283,3 @@ export function hydrate() {
linkGc()
}
}
function devQueueSsrError(): Error | undefined {
const ssrErrorTemplateTag = document.querySelector(
'template[data-next-error-message]'
)
if (ssrErrorTemplateTag) {
const message: string = ssrErrorTemplateTag.getAttribute(
'data-next-error-message'
)!
const stack = ssrErrorTemplateTag.getAttribute('data-next-error-stack')
const digest = ssrErrorTemplateTag.getAttribute('data-next-error-digest')
const error = new Error(message)
if (digest) {
;(error as any).digest = digest
}
// Skip Next.js SSR'd internal errors that which will be handled by the error boundaries.
if (isNextRouterError(error)) {
return
}
error.stack = stack || ''
return error
}
}
import type { OverlayState } from '../shared'
import type { GlobalErrorComponent } from '../../error-boundary'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary'
import { FontStyles } from '../font/font-styles'
import { DevOverlay } from '../ui/dev-overlay'
import { handleClientError } from '../../errors/use-error-handler'
import { isNextRouterError } from '../../is-next-router-error'
function readSsrError(): Error | null {
if (typeof document === 'undefined') {
return null
}
const ssrErrorTemplateTag = document.querySelector(
'template[data-next-error-message]'
)
if (ssrErrorTemplateTag) {
const message: string = ssrErrorTemplateTag.getAttribute(
'data-next-error-message'
)!
const stack = ssrErrorTemplateTag.getAttribute('data-next-error-stack')
const digest = ssrErrorTemplateTag.getAttribute('data-next-error-digest')
const error = new Error(message)
if (digest) {
;(error as any).digest = digest
}
// Skip Next.js SSR'd internal errors that which will be handled by the error boundaries.
if (isNextRouterError(error)) {
return null
}
error.stack = stack || ''
return error
}
return null
}
// Needs to be in the same error boundary as the shell.
// If it commits, we know we recovered from an SSR error.
// If it doesn't commit, we errored again and React will take care of error reporting.
function ReplaySsrOnlyErrors() {
if (process.env.NODE_ENV !== 'production') {
// Need to read during render. The attributes will be gone after commit.
const ssrError = readSsrError()
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (ssrError !== null) {
// TODO(veil): Produces wrong Owner Stack
// TODO(veil): Mark as recoverable error
// TODO(veil): console.error
handleClientError(ssrError, [])
}
}, [ssrError])
}
return null
}
export function AppDevOverlay({
state,
......@@ -23,6 +75,7 @@ export function AppDevOverlay({
globalError={globalError}
onError={setIsErrorOverlayOpen}
>
<ReplaySsrOnlyErrors />
{children}
</AppDevOverlayErrorBoundary>
......
......@@ -90,7 +90,7 @@ describe('ReactRefreshLogBox app', () => {
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: no",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......@@ -106,7 +106,7 @@ describe('ReactRefreshLogBox app', () => {
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 3,
"count": 2,
"description": "Error: no",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......@@ -363,7 +363,7 @@ describe('ReactRefreshLogBox app', () => {
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: ",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......@@ -1642,7 +1642,7 @@ export default function Home() {
// FIXME: display the sourcemapped stack frames
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: utils error",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......@@ -1659,7 +1659,7 @@ export default function Home() {
// FIXME: Webpack stack frames are not source mapped
await expect(browser).toDisplayRedbox(`
{
"count": 3,
"count": 2,
"description": "Error: utils error",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......
......@@ -217,6 +217,7 @@ describe('Error overlay for hydration errors in App router', () => {
<HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}>
<ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary>
<HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}>
<HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...>
......@@ -240,6 +241,7 @@ describe('Error overlay for hydration errors in App router', () => {
<HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}>
<ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary>
<HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}>
<HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...>
......
......@@ -139,10 +139,9 @@ describe('app-dir - owner-stack', () => {
},
})
// TODO(veil): Should only display one error.
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: ssr error",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......
'use client'
export default function Page() {
function Component() {
if (typeof window === 'undefined') {
throw new Error('SSR only error')
}
return <p>hello world</p>
}
export default function Page() {
return <Component />
}
import { nextTestSetup } from 'e2e-utils'
import {
assertNoRedbox,
getRedboxDescription,
getRedboxSource,
hasErrorToast,
openRedbox,
} from 'next-test-utils'
import { assertNoRedbox, hasErrorToast } from 'next-test-utils'
describe('ssr-only-error', () => {
const { next } = nextTestSetup({
......@@ -15,29 +9,19 @@ describe('ssr-only-error', () => {
it('should show ssr only error in error overlay', async () => {
const browser = await next.browser('/')
// Ensure it's not like server error that is shown by default
await hasErrorToast(browser)
await openRedbox(browser)
const description = await getRedboxDescription(browser)
const source = await getRedboxSource(browser)
expect({
description,
source,
}).toMatchInlineSnapshot(`
// TODO(veil): Missing Owner Stack
await expect(browser).toDisplayCollapsedRedbox(`
{
"count": 1,
"description": "Error: SSR only error",
"source": "app/page.tsx (5:11) @ Page
3 | export default function Page() {
4 | if (typeof window === 'undefined') {
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "app/page.tsx (5:11) @ Component
> 5 | throw new Error('SSR only error')
| ^
6 | }
7 | return <p>hello world</p>
8 | }",
| ^",
"stack": [
"Component app/page.tsx (5:11)",
],
}
`)
})
......
......@@ -13,11 +13,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/invalid-href')
if (isNextDev) {
// TODO(veil): Should be a single error
// TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: Failed prop type: The prop \`href\` expects a \`string\` or \`object\` in \`<Link>\`, but got \`undefined\` instead.
Open your browser's console to view the Component stack trace.",
"environmentLabel": null,
......@@ -41,11 +40,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/no-children')
if (isNextDev) {
// TODO(veil): Should be a single error
// TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: No children were passed to <Link> with \`href\` of \`/about\` but one child is required https://nextjs.org/docs/messages/link-no-children",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
......@@ -67,11 +65,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/multiple-children')
if (isNextDev) {
// TODO(veil): Should be a single error
// TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"count": 1,
"description": "Error: Multiple children were passed to <Link> with \`href\` of \`/\` but only one child is supported https://nextjs.org/docs/messages/link-multiple-children
Open your browser's console to view the Component stack trace.",
"environmentLabel": null,
......
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