Unverified Commit accec7b3 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Allow silencing direct access of dynamic APIs to unblock internal sync (#70705)

parent b95f0129
...@@ -266,6 +266,10 @@ export function getDefineEnv({ ...@@ -266,6 +266,10 @@ export function getDefineEnv({
'process.env.__NEXT_LINK_NO_TOUCH_START': 'process.env.__NEXT_LINK_NO_TOUCH_START':
config.experimental.linkNoTouchStart ?? false, config.experimental.linkNoTouchStart ?? false,
'process.env.__NEXT_ASSET_PREFIX': config.assetPrefix, 'process.env.__NEXT_ASSET_PREFIX': config.assetPrefix,
'process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS':
// Internal only so untyped to avoid discovery
(config.experimental as any).internal_disableSyncDynamicAPIWarnings ??
false,
...(isNodeOrEdgeCompilation ...(isNodeOrEdgeCompilation
? { ? {
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`) // Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
......
...@@ -303,6 +303,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -303,6 +303,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
forceSwcTransforms: z.boolean().optional(), forceSwcTransforms: z.boolean().optional(),
fullySpecified: z.boolean().optional(), fullySpecified: z.boolean().optional(),
gzipSize: z.boolean().optional(), gzipSize: z.boolean().optional(),
internal_disableSyncDynamicAPIWarnings: z.boolean().optional(),
isrFlushToDisk: z.boolean().optional(), isrFlushToDisk: z.boolean().optional(),
largePageDataBytes: z.number().optional(), largePageDataBytes: z.number().optional(),
linkNoTouchStart: z.boolean().optional(), linkNoTouchStart: z.boolean().optional(),
......
...@@ -510,23 +510,30 @@ function describeNameArg(arg: unknown) { ...@@ -510,23 +510,30 @@ function describeNameArg(arg: unknown) {
: '...' : '...'
} }
function warnForSyncIteration(route?: string) { const noop = () => {}
const prefix = route ? ` In route ${route} ` : ''
console.error(
`${prefix}cookies were iterated over. ` +
`\`cookies()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
function warnForSyncAccess(route: undefined | string, expression: string) { const warnForSyncIteration = process.env
const prefix = route ? ` In route ${route} a ` : 'A ' .__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
console.error( ? noop
`${prefix}cookie property was accessed directly with \`${expression}\`. ` + : function warnForSyncIteration(route?: string) {
`\`cookies()\` should be awaited before using its value. ` + const prefix = route ? ` In route ${route} ` : ''
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` console.error(
) `${prefix}cookies were iterated over. ` +
} `\`cookies()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
? noop
: function warnForSyncAccess(route: undefined | string, expression: string) {
const prefix = route ? ` In route ${route} a ` : 'A '
console.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`
)
}
function polyfilledResponseCookiesIterator( function polyfilledResponseCookiesIterator(
this: ResponseCookies this: ResponseCookies
......
...@@ -163,11 +163,15 @@ class DraftMode { ...@@ -163,11 +163,15 @@ class DraftMode {
} }
} }
function warnForSyncAccess(route: undefined | string, expression: string) { const noop = () => {}
const prefix = route ? ` In route ${route} a ` : 'A '
console.error( const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
`${prefix}\`draftMode()\` property was accessed directly with \`${expression}\`. ` + ? noop
`\`draftMode()\` should be awaited before using its value. ` + : function warnForSyncAccess(route: undefined | string, expression: string) {
`Learn more: https://nextjs.org/docs/messages/draft-mode-sync-access` const prefix = route ? ` In route ${route} a ` : 'A '
) console.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`
)
}
...@@ -425,23 +425,30 @@ function describeNameArg(arg: unknown) { ...@@ -425,23 +425,30 @@ function describeNameArg(arg: unknown) {
return typeof arg === 'string' ? `'${arg}'` : '...' return typeof arg === 'string' ? `'${arg}'` : '...'
} }
function warnForSyncIteration(route?: string) { const noop = () => {}
const prefix = route ? ` In route ${route} ` : ''
console.error(
`${prefix}headers were iterated over. ` +
`\`headers()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
function warnForSyncAccess(route: undefined | string, expression: string) { const warnForSyncIteration = process.env
const prefix = route ? ` In route ${route} a ` : 'A ' .__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
console.error( ? noop
`${prefix}header property was accessed directly with \`${expression}\`. ` + : function warnForSyncIteration(route?: string) {
`\`headers()\` should be awaited before using its value. ` + const prefix = route ? ` In route ${route} ` : ''
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` console.error(
) `${prefix}headers were iterated over. ` +
} `\`headers()\` should be awaited before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
? noop
: function warnForSyncAccess(route: undefined | string, expression: string) {
const prefix = route ? ` In route ${route} a ` : 'A '
console.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`
)
}
type HeadersExtensions = { type HeadersExtensions = {
[K in keyof ReadonlyHeaders]: unknown [K in keyof ReadonlyHeaders]: unknown
......
...@@ -119,29 +119,43 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings( ...@@ -119,29 +119,43 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings(
return proxiedPromise return proxiedPromise
} }
function warnForSyncAccess(expression: string) { const noop = () => {}
console.error(
`A param property was accessed directly with ${expression}. \`params\` is now a Promise and should be unwrapped with \`React.use()\` before accessing properties of the underlying params object. In this version of Next.js direct access to param properties is still supported to facilitate migration but in a future version you will be required to unwrap \`params\` with \`React.use()\`.`
)
}
function warnForEnumeration(missingProperties: Array<string>) { const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
if (missingProperties.length) { ? noop
const describedMissingProperties = : function warnForSyncAccess(expression: string) {
describeListOfPropertyNames(missingProperties) if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
console.error( return
`params are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` + }
`\`params\` should be unwrapped with \`React.use()\` before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` console.error(
) `A param property was accessed directly with ${expression}. \`params\` is now a Promise and should be unwrapped with \`React.use()\` before accessing properties of the underlying params object. In this version of Next.js direct access to param properties is still supported to facilitate migration but in a future version you will be required to unwrap \`params\` with \`React.use()\`.`
} else { )
console.error( }
`params are being enumerated. ` +
`\`params\` should be unwrapped with \`React.use()\` before using its value. ` + const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` ? noop
) : function warnForEnumeration(missingProperties: Array<string>) {
} if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
} return
}
if (missingProperties.length) {
const describedMissingProperties =
describeListOfPropertyNames(missingProperties)
console.error(
`params are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` +
`\`params\` should be unwrapped with \`React.use()\` before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
} else {
console.error(
`params are being enumerated. ` +
`\`params\` should be unwrapped with \`React.use()\` before using its value. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
}
function describeListOfPropertyNames(properties: Array<string>) { function describeListOfPropertyNames(properties: Array<string>) {
switch (properties.length) { switch (properties.length) {
......
...@@ -495,36 +495,50 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings( ...@@ -495,36 +495,50 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings(
return proxiedPromise return proxiedPromise
} }
function warnForSyncAccess(route: undefined | string, expression: string) { const noop = () => {}
const prefix = route ? ` In route ${route} a ` : 'A '
console.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`
)
}
function warnForEnumeration( const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
route: undefined | string, ? noop
missingProperties: Array<string> : function warnForSyncAccess(route: undefined | string, expression: string) {
) { if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
const prefix = route ? ` In route ${route} ` : '' return
if (missingProperties.length) { }
const describedMissingProperties =
describeListOfPropertyNames(missingProperties) const prefix = route ? ` In route ${route} a ` : 'A '
console.error( console.error(
`${prefix}params are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` + `${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`
) )
} else { }
console.error(
`${prefix}params are being enumerated. ` + const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
`\`params\` should be awaited before accessing its properties. ` + ? noop
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` : function warnForEnumeration(
) route: undefined | string,
} missingProperties: Array<string>
} ) {
if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
return
}
const prefix = route ? ` In route ${route} ` : ''
if (missingProperties.length) {
const describedMissingProperties =
describeListOfPropertyNames(missingProperties)
console.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`
)
} else {
console.error(
`${prefix}params are being enumerated. ` +
`\`params\` should be awaited before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
}
function describeListOfPropertyNames(properties: Array<string>) { function describeListOfPropertyNames(properties: Array<string>) {
switch (properties.length) { switch (properties.length) {
......
...@@ -119,18 +119,32 @@ function makeUntrackedExoticSearchParams( ...@@ -119,18 +119,32 @@ function makeUntrackedExoticSearchParams(
return promise return promise
} }
function warnForSyncAccess(expression: string) { const noop = () => {}
console.error(
`A searchParam property was accessed directly with ${expression}. ` +
`\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
function warnForSyncSpread() { const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
console.error( ? noop
`The keys of \`searchParams\` were accessed directly. ` + : function warnForSyncAccess(expression: string) {
`\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` + if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` return
) }
}
console.error(
`A searchParam property was accessed directly with ${expression}. ` +
`\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
const warnForSyncSpread = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
? noop
: function warnForSyncSpread() {
if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) {
return
}
console.error(
`The keys of \`searchParams\` were accessed directly. ` +
`\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` +
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
)
}
...@@ -657,36 +657,42 @@ function makeDynamicallyTrackedExoticSearchParamsWithDevWarnings( ...@@ -657,36 +657,42 @@ function makeDynamicallyTrackedExoticSearchParamsWithDevWarnings(
return proxiedPromise return proxiedPromise
} }
function warnForSyncAccess(route: undefined | string, expression: string) { const noop = () => {}
const prefix = route ? ` In route ${route} a ` : 'A '
console.error( const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
`${prefix}searchParam property was accessed directly with ${expression}. ` + ? noop
`\`searchParams\` should be awaited before accessing properties. ` + : function warnForSyncAccess(route: undefined | string, expression: string) {
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` const prefix = route ? ` In route ${route} a ` : 'A '
) console.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`
)
}
function warnForEnumeration( const warnForEnumeration = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS
route: undefined | string, ? noop
missingProperties: Array<string> : function warnForEnumeration(
) { route: undefined | string,
const prefix = route ? ` In route ${route} ` : '' missingProperties: Array<string>
if (missingProperties.length) { ) {
const describedMissingProperties = const prefix = route ? ` In route ${route} ` : ''
describeListOfPropertyNames(missingProperties) if (missingProperties.length) {
console.error( const describedMissingProperties =
`${prefix}searchParams are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` + describeListOfPropertyNames(missingProperties)
`\`searchParams\` should be awaited before accessing its properties. ` + console.error(
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `${prefix}searchParams are being enumerated incompletely missing these properties: ${describedMissingProperties}. ` +
) `\`searchParams\` should be awaited before accessing its properties. ` +
} else { `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
console.error( )
`${prefix}searchParams are being enumerated. ` + } else {
`\`searchParams\` should be awaited before accessing its properties. ` + console.error(
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` `${prefix}searchParams are being enumerated. ` +
) `\`searchParams\` should be awaited before accessing its properties. ` +
} `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`
} )
}
}
function describeListOfPropertyNames(properties: Array<string>) { function describeListOfPropertyNames(properties: Array<string>) {
switch (properties.length) { switch (properties.length) {
......
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