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 ...@@ -26,8 +26,6 @@ import { createInitialRouterState } from './components/router-reducer/create-ini
import { MissingSlotContext } from '../shared/lib/app-router-context.shared-runtime' import { MissingSlotContext } from '../shared/lib/app-router-context.shared-runtime'
import { setAppBuildId } from './app-build-id' import { setAppBuildId } from './app-build-id'
import { shouldRenderRootLevelErrorOverlay } from './lib/is-error-thrown-while-rendering-rsc' 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" /> /// <reference types="react-dom/experimental" />
...@@ -231,16 +229,6 @@ function Root({ children }: React.PropsWithChildren<{}>) { ...@@ -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 return children
} }
...@@ -295,26 +283,3 @@ export function hydrate() { ...@@ -295,26 +283,3 @@ export function hydrate() {
linkGc() 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 { OverlayState } from '../shared'
import type { GlobalErrorComponent } from '../../error-boundary' import type { GlobalErrorComponent } from '../../error-boundary'
import { useState } from 'react' import { useEffect, useState } from 'react'
import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary' import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary'
import { FontStyles } from '../font/font-styles' import { FontStyles } from '../font/font-styles'
import { DevOverlay } from '../ui/dev-overlay' 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({ export function AppDevOverlay({
state, state,
...@@ -23,6 +75,7 @@ export function AppDevOverlay({ ...@@ -23,6 +75,7 @@ export function AppDevOverlay({
globalError={globalError} globalError={globalError}
onError={setIsErrorOverlayOpen} onError={setIsErrorOverlayOpen}
> >
<ReplaySsrOnlyErrors />
{children} {children}
</AppDevOverlayErrorBoundary> </AppDevOverlayErrorBoundary>
......
...@@ -90,7 +90,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -90,7 +90,7 @@ describe('ReactRefreshLogBox app', () => {
if (isTurbopack) { if (isTurbopack) {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 2, "count": 1,
"description": "Error: no", "description": "Error: no",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
...@@ -106,7 +106,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -106,7 +106,7 @@ describe('ReactRefreshLogBox app', () => {
} else { } else {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 3, "count": 2,
"description": "Error: no", "description": "Error: no",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
...@@ -363,7 +363,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -363,7 +363,7 @@ describe('ReactRefreshLogBox app', () => {
} else { } else {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 2, "count": 1,
"description": "Error: ", "description": "Error: ",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
...@@ -1642,7 +1642,7 @@ export default function Home() { ...@@ -1642,7 +1642,7 @@ export default function Home() {
// FIXME: display the sourcemapped stack frames // FIXME: display the sourcemapped stack frames
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 2, "count": 1,
"description": "Error: utils error", "description": "Error: utils error",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
...@@ -1659,7 +1659,7 @@ export default function Home() { ...@@ -1659,7 +1659,7 @@ export default function Home() {
// FIXME: Webpack stack frames are not source mapped // FIXME: Webpack stack frames are not source mapped
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 3, "count": 2,
"description": "Error: utils error", "description": "Error: utils error",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
......
...@@ -217,6 +217,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -217,6 +217,7 @@ describe('Error overlay for hydration errors in App router', () => {
<HotReload assetPrefix="" globalError={[...]}> <HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlay state={{nextId:1, ...}} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}>
<ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary> <DevRootHTTPAccessFallbackBoundary>
<HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}> <HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}>
<HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...> <HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...>
...@@ -240,6 +241,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -240,6 +241,7 @@ describe('Error overlay for hydration errors in App router', () => {
<HotReload assetPrefix="" globalError={[...]}> <HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlay state={{nextId:1, ...}} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function bound dispatchSetState}>
<ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary> <DevRootHTTPAccessFallbackBoundary>
<HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}> <HTTPAccessFallbackBoundary notFound={<NotAllowedRootHTTPFallbackError>}>
<HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...> <HTTPAccessFallbackErrorBoundary pathname="/" notFound={<NotAllowedRootHTTPFallbackError>} ...>
......
...@@ -139,10 +139,9 @@ describe('app-dir - owner-stack', () => { ...@@ -139,10 +139,9 @@ describe('app-dir - owner-stack', () => {
}, },
}) })
// TODO(veil): Should only display one error.
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"count": 2, "count": 1,
"description": "Error: ssr error", "description": "Error: ssr error",
"environmentLabel": null, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
......
'use client' 'use client'
export default function Page() { function Component() {
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
throw new Error('SSR only error') throw new Error('SSR only error')
} }
return <p>hello world</p> return <p>hello world</p>
} }
export default function Page() {
return <Component />
}
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { import { assertNoRedbox, hasErrorToast } from 'next-test-utils'
assertNoRedbox,
getRedboxDescription,
getRedboxSource,
hasErrorToast,
openRedbox,
} from 'next-test-utils'
describe('ssr-only-error', () => { describe('ssr-only-error', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
...@@ -15,29 +9,19 @@ describe('ssr-only-error', () => { ...@@ -15,29 +9,19 @@ describe('ssr-only-error', () => {
it('should show ssr only error in error overlay', async () => { it('should show ssr only error in error overlay', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
// Ensure it's not like server error that is shown by default // TODO(veil): Missing Owner Stack
await hasErrorToast(browser) await expect(browser).toDisplayCollapsedRedbox(`
await openRedbox(browser)
const description = await getRedboxDescription(browser)
const source = await getRedboxSource(browser)
expect({
description,
source,
}).toMatchInlineSnapshot(`
{ {
"count": 1,
"description": "Error: SSR only error", "description": "Error: SSR only error",
"source": "app/page.tsx (5:11) @ Page "environmentLabel": null,
"label": "Unhandled Runtime Error",
3 | export default function Page() { "source": "app/page.tsx (5:11) @ Component
4 | if (typeof window === 'undefined') {
> 5 | throw new Error('SSR only error') > 5 | throw new Error('SSR only error')
| ^ | ^",
6 | } "stack": [
7 | return <p>hello world</p> "Component app/page.tsx (5:11)",
8 | }", ],
} }
`) `)
}) })
......
...@@ -13,11 +13,10 @@ describe('next-link', () => { ...@@ -13,11 +13,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/invalid-href') const browser = await webdriver(next.appPort, '/invalid-href')
if (isNextDev) { 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 // TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(` 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. "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.", Open your browser's console to view the Component stack trace.",
"environmentLabel": null, "environmentLabel": null,
...@@ -41,11 +40,10 @@ describe('next-link', () => { ...@@ -41,11 +40,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/no-children') const browser = await webdriver(next.appPort, '/no-children')
if (isNextDev) { 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 // TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(` 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", "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, "environmentLabel": null,
"label": "Unhandled Runtime Error", "label": "Unhandled Runtime Error",
...@@ -67,11 +65,10 @@ describe('next-link', () => { ...@@ -67,11 +65,10 @@ describe('next-link', () => {
const browser = await webdriver(next.appPort, '/multiple-children') const browser = await webdriver(next.appPort, '/multiple-children')
if (isNextDev) { 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 // TODO(veil): https://linear.app/vercel/issue/NDX-554/hide-the-anonymous-frames-which-are-between-2-ignored-frames
await expect(browser).toDisplayRedbox(` 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 "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.", Open your browser's console to view the Component stack trace.",
"environmentLabel": null, "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