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