Unverified Commit dab4d00e authored by Josh Story's avatar Josh Story Committed by GitHub

sync access warnings for Request APIs should include a stack (#71519)

This updates our logging to use Error objects to provide stacks to
better locate where sync access of Request APIs is happening
parent d62627ce
import * as React from 'react' import * as React from 'react'
const errorRef: { current: null | string } = { current: null } const errorRef: { current: null | Error } = { current: null }
// React.cache is currently only available in canary/experimental React channels. // React.cache is currently only available in canary/experimental React channels.
const cache = const cache =
...@@ -33,7 +33,7 @@ const flushCurrentErrorIfNew = cache( ...@@ -33,7 +33,7 @@ const flushCurrentErrorIfNew = cache(
* @returns * @returns
*/ */
export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>( export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>(
getMessage: (...args: Args) => string getMessage: (...args: Args) => Error
) { ) {
return function logDedupedError(...args: Args) { return function logDedupedError(...args: Args) {
const message = getMessage(...args) const message = getMessage(...args)
......
...@@ -538,10 +538,10 @@ const warnForSyncIteration = process.env ...@@ -538,10 +538,10 @@ const warnForSyncIteration = process.env
: createDedupedByCallsiteServerErrorLoggerDev( : createDedupedByCallsiteServerErrorLoggerDev(
function getSyncIterationMessage(route?: string) { function getSyncIterationMessage(route?: string) {
const prefix = route ? ` In route ${route} ` : '' const prefix = route ? ` In route ${route} ` : ''
return ( return new Error(
`${prefix}cookies were iterated over. ` + `${prefix}cookies were iterated over. ` +
`\`cookies()\` should be awaited before using its value. ` + `\`cookies()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} }
) )
...@@ -553,10 +553,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -553,10 +553,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
expression: string expression: string
) { ) {
const prefix = route ? ` In route ${route} a ` : 'A ' const prefix = route ? ` In route ${route} a ` : 'A '
return ( return new Error(
`${prefix}cookie property was accessed directly with \`${expression}\`. ` + `${prefix}cookie property was accessed directly with \`${expression}\`. ` +
`\`cookies()\` should be awaited before using its value. ` + `\`cookies()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
}) })
......
...@@ -199,10 +199,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -199,10 +199,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
expression: string expression: string
) { ) {
const prefix = route ? ` In route ${route} a ` : 'A ' const prefix = route ? ` In route ${route} a ` : 'A '
return ( return new Error(
`${prefix}\`draftMode()\` property was accessed directly with \`${expression}\`. ` + `${prefix}\`draftMode()\` property was accessed directly with \`${expression}\`. ` +
`\`draftMode()\` should be awaited before using its value. ` + `\`draftMode()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/draft-mode-sync-access` `Learn more: https://nextjs.org/docs/messages/draft-mode-sync-access`
) )
}) })
......
...@@ -460,10 +460,10 @@ const warnForSyncIteration = process.env ...@@ -460,10 +460,10 @@ const warnForSyncIteration = process.env
: createDedupedByCallsiteServerErrorLoggerDev( : createDedupedByCallsiteServerErrorLoggerDev(
function getSyncIterationMessage(route?: string) { function getSyncIterationMessage(route?: string) {
const prefix = route ? ` In route ${route} ` : '' const prefix = route ? ` In route ${route} ` : ''
return ( return new Error(
`${prefix}headers were iterated over. ` + `${prefix}headers were iterated over. ` +
`\`headers()\` should be awaited before using its value. ` + `\`headers()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} }
) )
...@@ -475,10 +475,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -475,10 +475,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
expression: string expression: string
) { ) {
const prefix = route ? ` In route ${route} a ` : 'A ' const prefix = route ? ` In route ${route} a ` : 'A '
return ( return new Error(
`${prefix}header property was accessed directly with \`${expression}\`. ` + `${prefix}header property was accessed directly with \`${expression}\`. ` +
`\`headers()\` should be awaited before using its value. ` + `\`headers()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
}) })
......
...@@ -420,10 +420,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -420,10 +420,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
expression: string expression: string
) { ) {
const prefix = route ? ` In route ${route} a ` : 'A ' const prefix = route ? ` In route ${route} a ` : 'A '
return ( return new Error(
`${prefix}param property was accessed directly with ${expression}. ` + `${prefix}param property was accessed directly with ${expression}. ` +
`\`params\` should be awaited before accessing its properties. ` + `\`params\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
}) })
...@@ -437,16 +437,16 @@ const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -437,16 +437,16 @@ const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
if (missingProperties.length) { if (missingProperties.length) {
const describedMissingProperties = const describedMissingProperties =
describeListOfPropertyNames(missingProperties) describeListOfPropertyNames(missingProperties)
return ( return new Error(
`${prefix}params are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` + `${prefix}params are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` +
`\`params\` should be awaited before accessing its properties. ` + `\`params\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} else { } else {
return ( return new Error(
`${prefix}params are being enumerated. ` + `${prefix}params are being enumerated. ` +
`\`params\` should be awaited before accessing its properties. ` + `\`params\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} }
}) })
......
...@@ -680,10 +680,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -680,10 +680,10 @@ const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
expression: string expression: string
) { ) {
const prefix = route ? ` In route ${route} a ` : 'A ' const prefix = route ? ` In route ${route} a ` : 'A '
return ( return new Error(
`${prefix}searchParam property was accessed directly with ${expression}. ` + `${prefix}searchParam property was accessed directly with ${expression}. ` +
`\`searchParams\` should be awaited before accessing properties. ` + `\`searchParams\` should be awaited before accessing properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
}) })
...@@ -697,16 +697,16 @@ const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ...@@ -697,16 +697,16 @@ const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
if (missingProperties.length) { if (missingProperties.length) {
const describedMissingProperties = const describedMissingProperties =
describeListOfPropertyNames(missingProperties) describeListOfPropertyNames(missingProperties)
return ( return new Error(
`${prefix}searchParams are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` + `${prefix}searchParams are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` +
`\`searchParams\` should be awaited before accessing its properties. ` + `\`searchParams\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} else { } else {
return ( return new Error(
`${prefix}searchParams are being enumerated. ` + `${prefix}searchParams are being enumerated. ` +
`\`searchParams\` should be awaited before accessing its properties. ` + `\`searchParams\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
) )
} }
}) })
......
...@@ -10,8 +10,8 @@ describe('dynamic-requests warnings', () => { ...@@ -10,8 +10,8 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/cookies') const browser = await next.browser('/request/cookies')
const browserLogsserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogsserLogs const browserConsoleErrors = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'error')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
......
export default function Page({ params }) { export default async function Page({
return <h1>Page {params.slug}</h1> params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
return <h1>Page {slug}</h1>
} }
export default function Page({ params }) { export default async function Page({
return <h1>Blog Sub Page {params.slug}</h1> params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
return <h1>Blog Sub Page {slug}</h1>
} }
...@@ -7,6 +7,10 @@ export function generateStaticParams() { ...@@ -7,6 +7,10 @@ export function generateStaticParams() {
return [{ slug: 'first' }, { slug: 'second' }] return [{ slug: 'first' }, { slug: 'second' }]
} }
export const GET = (req: NextRequest, { params }) => { export const GET = async (
return NextResponse.json({ params, now: Date.now() }) req: NextRequest,
{ params }: { params: Promise<{ slug: string }> }
) => {
const resolvedParams = await params
return NextResponse.json({ params: resolvedParams, now: Date.now() })
} }
...@@ -5,6 +5,10 @@ export function generateStaticParams() { ...@@ -5,6 +5,10 @@ export function generateStaticParams() {
return [{ slug: 'first' }, { slug: 'second' }] return [{ slug: 'first' }, { slug: 'second' }]
} }
export const GET = (req: NextRequest, { params }) => { export const GET = async (
return NextResponse.json({ params, now: Date.now() }) req: NextRequest,
{ params }: { params: Promise<{ slug: string }> }
) => {
const resolvedParams = await params
return NextResponse.json({ params: resolvedParams, now: Date.now() })
} }
...@@ -3,7 +3,7 @@ import { withRequestMeta } from '../helpers' ...@@ -3,7 +3,7 @@ import { withRequestMeta } from '../helpers'
const helloHandler = async ( const helloHandler = async (
request: NextRequest, request: NextRequest,
{ params }: { params?: Record<string, string | string[]> } { params }: { params?: Promise<Record<string, string | string[]>> }
): Promise<Response> => { ): Promise<Response> => {
const { pathname } = request.nextUrl const { pathname } = request.nextUrl
...@@ -11,10 +11,12 @@ const helloHandler = async ( ...@@ -11,10 +11,12 @@ const helloHandler = async (
throw new Error('missing WebSocket constructor!!') throw new Error('missing WebSocket constructor!!')
} }
const resolvedParams = params ? await params : null
return new Response('hello, world', { return new Response('hello, world', {
headers: withRequestMeta({ headers: withRequestMeta({
method: request.method, method: request.method,
params: params ?? null, params: resolvedParams,
pathname, pathname,
}), }),
}) })
......
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