Commit 181e7d52 authored by Jiachi Liu's avatar Jiachi Liu

Add stack trace to client rendering bailout error (#61200)

parent 06f01e59
...@@ -22,6 +22,22 @@ function setMessage(error: Error, message: string): void { ...@@ -22,6 +22,22 @@ function setMessage(error: Error, message: string): void {
} }
} }
/**
* Input:
* Error: Something went wrong
at funcName (/path/to/file.js:10:5)
at anotherFunc (/path/to/file.js:15:10)
* Output:
at funcName (/path/to/file.js:10:5)
at anotherFunc (/path/to/file.js:15:10)
*/
export function getStackWithoutErrorMessage(error: Error): string {
const stack = error.stack
if (!stack) return ''
return stack.replace(/^[^\n]*\n/, '')
}
export function formatServerError(error: Error): void { export function formatServerError(error: Error): void {
if (typeof error?.message !== 'string') return if (typeof error?.message !== 'string') return
......
...@@ -80,6 +80,7 @@ import { DetachedPromise } from '../../lib/detached-promise' ...@@ -80,6 +80,7 @@ import { DetachedPromise } from '../../lib/detached-promise'
import { isDynamicServerError } from '../../client/components/hooks-server-context' import { isDynamicServerError } from '../../client/components/hooks-server-context'
import { useFlightResponse } from './use-flight-response' import { useFlightResponse } from './use-flight-response'
import { isStaticGenBailoutError } from '../../client/components/static-generation-bailout' import { isStaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { getStackWithoutErrorMessage } from '../../lib/format-server-error'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -1008,8 +1009,9 @@ async function renderToHTMLOrFlightImpl( ...@@ -1008,8 +1009,9 @@ async function renderToHTMLOrFlightImpl(
console.log() console.log()
if (renderOpts.experimental.missingSuspenseWithCSRBailout) { if (renderOpts.experimental.missingSuspenseWithCSRBailout) {
const stack = getStackWithoutErrorMessage(err)
error( error(
`${err.reason} should be wrapped in a suspense boundary at page "${pagePath}". Read more: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout` `${err.reason} should be wrapped in a suspense boundary at page "${pagePath}". Read more: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout\n${stack}`
) )
throw err throw err
......
...@@ -77,8 +77,7 @@ export function createErrorHandler({ ...@@ -77,8 +77,7 @@ export function createErrorHandler({
const { logAppDirError } = const { logAppDirError } =
require('../dev/log-app-dir-error') as typeof import('../dev/log-app-dir-error') require('../dev/log-app-dir-error') as typeof import('../dev/log-app-dir-error')
logAppDirError(err) logAppDirError(err)
} } else {
if (process.env.NODE_ENV === 'production') {
console.error(err) console.error(err)
} }
} }
......
import { Suspense } from 'react'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html> <html>
<head /> <head />
<body>{children}</body> <body>
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense>
</body>
</html> </html>
) )
} }
import { Suspense } from 'react'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html> <html>
<head /> <head />
<body> <body>{children}</body>
<Suspense fallback={<div>Loading...</div>}>{children}</Suspense>
</body>
</html> </html>
) )
} }
...@@ -2,7 +2,16 @@ ...@@ -2,7 +2,16 @@
import { useSearchParams } from 'next/navigation' import { useSearchParams } from 'next/navigation'
export default function Page() { function SearchParams() {
useSearchParams() useSearchParams()
return <div>Page</div> return null
}
export default function Page() {
return (
<>
<SearchParams />
<div>Page</div>
</>
)
} }
...@@ -19,31 +19,36 @@ createNextDescribe( ...@@ -19,31 +19,36 @@ createNextDescribe(
describe('useSearchParams', () => { describe('useSearchParams', () => {
const message = `useSearchParams() should be wrapped in a suspense boundary at page "/".` const message = `useSearchParams() should be wrapped in a suspense boundary at page "/".`
it('should fail build if useSearchParams is not wrapped in a suspense boundary', async () => {
const { exitCode } = await next.build()
expect(exitCode).toBe(1)
expect(next.cliOutput).toContain(message)
// Can show the trace where the searchParams hook is used
expect(next.cliOutput).toMatch(/at.*server[\\/]app[\\/]page.js/)
})
it('should pass build if useSearchParams is wrapped in a suspense boundary', async () => { it('should pass build if useSearchParams is wrapped in a suspense boundary', async () => {
await next.renameFile('app/layout.js', 'app/layout-no-suspense.js')
await next.renameFile('app/layout-suspense.js', 'app/layout.js')
await expect(next.build()).resolves.toEqual({ await expect(next.build()).resolves.toEqual({
exitCode: 0, exitCode: 0,
cliOutput: expect.not.stringContaining(message), cliOutput: expect.not.stringContaining(message),
}) })
})
it('should fail build if useSearchParams is not wrapped in a suspense boundary', async () => {
await next.renameFile('app/layout.js', 'app/layout-suspense.js') await next.renameFile('app/layout.js', 'app/layout-suspense.js')
await next.renameFile('app/layout-no-suspense.js', 'app/layout.js') await next.renameFile('app/layout-no-suspense.js', 'app/layout.js')
await expect(next.build()).resolves.toEqual({
exitCode: 1,
cliOutput: expect.stringContaining(message),
})
await next.renameFile('app/layout.js', 'app/layout-no-suspense.js')
await next.renameFile('app/layout-suspense.js', 'app/layout.js')
}) })
}) })
describe('next/dynamic', () => { describe('next/dynamic', () => {
beforeEach(async () => { beforeEach(async () => {
await next.renameFile('app/page.js', 'app/_page.js')
await next.start() await next.start()
}) })
afterEach(async () => {
await next.renameFile('app/_page.js', 'app/page.js')
})
it('does not emit errors related to bailing out of client side rendering', async () => { it('does not emit errors related to bailing out of client side rendering', async () => {
const browser = await next.browser('/dynamic', { const browser = await next.browser('/dynamic', {
...@@ -53,8 +58,6 @@ createNextDescribe( ...@@ -53,8 +58,6 @@ createNextDescribe(
try { try {
await browser.waitForElementByCss('#dynamic') await browser.waitForElementByCss('#dynamic')
// await new Promise((resolve) => setTimeout(resolve, 1000))
expect(await browser.log()).not.toContainEqual( expect(await browser.log()).not.toContainEqual(
expect.objectContaining({ expect.objectContaining({
source: 'error', source: 'error',
......
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