Unverified Commit 3008af6b authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

DX: add route context to the dynamic errors (#61332)

### What 

Given user infomation when the dynamic errors are thrown, e.g. bad
`cookies` or `headers` usages. Now users can tell through the error
information to see which pathname is broken, and trace down the usage.

#### before

```
Page couldn't be rendered statically because ...
This page needs to bail out of prerendering at this point because ...
```

#### after

```
Route /cookies couldn't be rendered statically because ...
Route /server needs to bail out of prerendering at this point because ...
```

### Why

When you have multi pages in your app, such as 100+, and many page might
uses these. This is hard to trace down where exactly the error is from

Closes NEXT-2283
Cloese NEXT-2265
parent fa935e5a
function getUrlWithoutHost(url: string) {
return new URL(url, 'http://n')
}
export function getPathname(url: string) {
return getUrlWithoutHost(url).pathname
}
......@@ -25,8 +25,8 @@ import React from 'react'
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { getPathname } from '../../lib/url'
const hasPostpone = typeof React.unstable_postpone === 'function'
......@@ -40,6 +40,7 @@ export function markCurrentScopeAsDynamic(
store: StaticGenerationStore,
expression: string
): void {
const pathname = getPathname(store.urlPathname)
if (store.isUnstableCacheCallback) {
// inside cache scopes marking a scope as dynamic has no effect because the outer cache scope
// creates a cache boundary. This is subtly different from reading a dynamic data source which is
......@@ -47,7 +48,7 @@ export function markCurrentScopeAsDynamic(
return
} else if (store.dynamicShouldError) {
throw new StaticGenBailoutError(
`Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
`Route ${pathname} with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
)
} else if (
// We are in a prerender (PPR enabled, during build)
......@@ -58,14 +59,14 @@ export function markCurrentScopeAsDynamic(
// This will be used by the renderer to decide whether
// the prerender requires a resume
store.prerenderState.hasDynamic = true
React.unstable_postpone(createPostponeReason(expression))
React.unstable_postpone(createPostponeReason(expression, pathname))
} else {
store.revalidate = 0
if (store.isStaticGeneration) {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Page couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
`Route ${pathname} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
store.dynamicUsageDescription = expression
store.dynamicUsageStack = err.stack
......@@ -88,13 +89,14 @@ export function trackDynamicDataAccessed(
store: StaticGenerationStore,
expression: string
): void {
const pathname = getPathname(store.urlPathname)
if (store.isUnstableCacheCallback) {
throw new Error(
`used "${expression}" inside a function cached with "unstable_cache(...)". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "${expression}" oustide of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/app/api-reference/functions/unstable_cache`
`Route ${pathname} used "${expression}" inside a function cached with "unstable_cache(...)". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "${expression}" oustide of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/app/api-reference/functions/unstable_cache`
)
} else if (store.dynamicShouldError) {
throw new StaticGenBailoutError(
`Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
`Route ${pathname} with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
)
} else if (
// We are in a prerender (PPR enabled, during build)
......@@ -105,14 +107,14 @@ export function trackDynamicDataAccessed(
// This will be used by the renderer to decide whether
// the prerender requires a resume
store.prerenderState.hasDynamic = true
React.unstable_postpone(createPostponeReason(expression))
React.unstable_postpone(createPostponeReason(expression, pathname))
} else {
store.revalidate = 0
if (store.isStaticGeneration) {
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Page couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
`Route ${pathname} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
store.dynamicUsageDescription = expression
store.dynamicUsageStack = err.stack
......@@ -144,13 +146,14 @@ export function trackDynamicFetch(
if (store.prerenderState) {
assertPostpone()
store.prerenderState.hasDynamic = true
React.unstable_postpone(createPostponeReason(expression))
React.unstable_postpone(createPostponeReason(expression, store.urlPathname))
}
}
function createPostponeReason(expression: string) {
function createPostponeReason(expression: string, urlPathname: string) {
const pathname = getPathname(urlPathname) // remove queries such like `_rsc` for flight
return (
`This page needs to bail out of prerendering at this point because it used ${expression}. ` +
`Route ${pathname} needs to bail out of prerendering at this point because it used ${expression}. ` +
`React throws this special object to indicate where. It should not be caught by ` +
`your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`
)
......
......@@ -8,6 +8,7 @@ import {
NEXT_CACHE_IMPLICIT_TAG_ID,
NEXT_CACHE_SOFT_TAG_MAX_LENGTH,
} from '../../../lib/constants'
import { getPathname } from '../../../lib/url'
export function revalidateTag(tag: string) {
return revalidate(tag, `revalidateTag ${tag}`)
......@@ -49,7 +50,9 @@ function revalidate(tag: string, expression: string) {
if (store.isUnstableCacheCallback) {
throw new Error(
`used "${expression}" inside a function cached with "unstable_cache(...)" which is unsupported. To ensure revalidation is performed consistently it must always happen outside of renders and cached functions. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
`Route ${getPathname(
store.urlPathname
)} used "${expression}" inside a function cached with "unstable_cache(...)" which is unsupported. To ensure revalidation is performed consistently it must always happen outside of renders and cached functions. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
)
}
......
......@@ -33,7 +33,7 @@ describe('dynamic = "error" in devmode', () => {
await session.hasRedbox()
console.log(await session.getRedboxDescription())
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
`"Error: Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering"`
`"Error: Route /server with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering"`
)
await cleanup()
......
......@@ -184,7 +184,7 @@ createNextDescribe(
try {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
'Error: Route /cookies with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
)
} finally {
await browser.close()
......@@ -194,7 +194,7 @@ createNextDescribe(
try {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
'Error: Route /headers with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
)
} finally {
await browser.close()
......@@ -204,7 +204,7 @@ createNextDescribe(
try {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams`'
'Error: Route /search with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams`'
)
} finally {
await browser.close()
......@@ -219,13 +219,13 @@ createNextDescribe(
}
// Error: Page with `dynamic = "error"` couldn't be rendered statically because it used `headers`
expect(next.cliOutput).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
'Error: Route /cookies with `dynamic = "error"` couldn\'t be rendered statically because it used `cookies`'
)
expect(next.cliOutput).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
'Error: Route /headers with `dynamic = "error"` couldn\'t be rendered statically because it used `headers`'
)
expect(next.cliOutput).toMatch(
'Error: Page with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams`.'
'Error: Route /search with `dynamic = "error"` couldn\'t be rendered statically because it used `searchParams`.'
)
})
}
......@@ -254,7 +254,7 @@ createNextDescribe(
try {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
'Error: used "cookies" inside a function cached with "unstable_cache(...)".'
'Error: Route /cookies used "cookies" inside a function cached with "unstable_cache(...)".'
)
} finally {
await browser.close()
......@@ -264,7 +264,7 @@ createNextDescribe(
try {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(
'Error: used "headers" inside a function cached with "unstable_cache(...)".'
'Error: Route /headers used "headers" inside a function cached with "unstable_cache(...)".'
)
} finally {
await browser.close()
......@@ -278,10 +278,10 @@ createNextDescribe(
// We expect this to fail
}
expect(next.cliOutput).toMatch(
'Error: used "cookies" inside a function cached with "unstable_cache(...)".'
'Error: Route /cookies used "cookies" inside a function cached with "unstable_cache(...)".'
)
expect(next.cliOutput).toMatch(
'Error: used "headers" inside a function cached with "unstable_cache(...)".'
'Error: Route /headers used "headers" inside a function cached with "unstable_cache(...)".'
)
})
}
......
import React, { Suspense } from 'react'
import { Suspense } from 'react'
import { cookies } from 'next/headers'
export default async function Page() {
......@@ -13,7 +13,7 @@ async function Foobar() {
try {
cookies()
} catch (err) {
console.log('Logged error: ' + err.message)
console.log('User land logged error: ' + err.message)
}
cookies() // still postpones so doesn't fail build
return null
......
......@@ -90,7 +90,7 @@ describe('ppr build errors', () => {
describe('when a postpone call is caught and logged it should', () => {
it('should include a message telling why', async () => {
expect(stdout).toContain(
'Logged error: This page needs to bail out of prerendering at this point because it used cookies.'
'User land logged error: Route /logging-error needs to bail out of prerendering at this point because it used cookies.'
)
})
})
......
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