Unverified Commit bbad635a authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

warn on sync access if dynamicIO is not enabled (#71696)

Since `console.error` is intercepted, this can be frustrating when a 3p
library that hasn't updated to use the async APIs triggers a warning.

This keeps the error behavior when `dynamicIO` is enabled and warns in
other cases.
parent 741f8300
...@@ -8,13 +8,20 @@ const cache = ...@@ -8,13 +8,20 @@ const cache =
? React.cache ? React.cache
: (fn: (key: unknown) => void) => fn : (fn: (key: unknown) => void) => fn
// When Dynamic IO is enabled, we record these as errors so that they
// are captured by the dev overlay as it's more critical to fix these
// when enabled.
const logErrorOrWarn = process.env.__NEXT_DYNAMIC_IO
? console.error
: console.warn
// We don't want to dedupe across requests. // We don't want to dedupe across requests.
// The developer might've just attempted to fix the warning so we should warn again if it still happens. // The developer might've just attempted to fix the warning so we should warn again if it still happens.
const flushCurrentErrorIfNew = cache( const flushCurrentErrorIfNew = cache(
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- cache key // eslint-disable-next-line @typescript-eslint/no-unused-vars -- cache key
(key: unknown) => { (key: unknown) => {
try { try {
console.error(errorRef.current) logErrorOrWarn(errorRef.current)
} finally { } finally {
errorRef.current = null errorRef.current = null
} }
...@@ -41,7 +48,7 @@ export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>( ...@@ -41,7 +48,7 @@ export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>(
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const callStackFrames = new Error().stack?.split('\n') const callStackFrames = new Error().stack?.split('\n')
if (callStackFrames === undefined || callStackFrames.length < 4) { if (callStackFrames === undefined || callStackFrames.length < 4) {
console.error(message) logErrorOrWarn(message)
} else { } else {
// Error: // Error:
// logDedupedError // logDedupedError
...@@ -53,7 +60,7 @@ export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>( ...@@ -53,7 +60,7 @@ export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>(
flushCurrentErrorIfNew(key) flushCurrentErrorIfNew(key)
} }
} else { } else {
console.error(message) logErrorOrWarn(message)
} }
} }
} }
...@@ -20,7 +20,7 @@ import { ...@@ -20,7 +20,7 @@ import {
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external' import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger' import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { scheduleImmediate } from '../../lib/scheduler' import { scheduleImmediate } from '../../lib/scheduler'
/** /**
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
postponeWithTracking, postponeWithTracking,
trackSynchronousRequestDataAccessInDev, trackSynchronousRequestDataAccessInDev,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger' import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { DynamicServerError } from '../../client/components/hooks-server-context' import { DynamicServerError } from '../../client/components/hooks-server-context'
......
...@@ -17,7 +17,7 @@ import { ...@@ -17,7 +17,7 @@ import {
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger' import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { scheduleImmediate } from '../../lib/scheduler' import { scheduleImmediate } from '../../lib/scheduler'
/** /**
......
...@@ -19,7 +19,7 @@ import { ...@@ -19,7 +19,7 @@ import {
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { describeStringPropertyAccess, wellKnownProperties } from './utils' import { describeStringPropertyAccess, wellKnownProperties } from './utils'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger' import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { scheduleImmediate } from '../../lib/scheduler' import { scheduleImmediate } from '../../lib/scheduler'
export type Params = Record<string, string | Array<string> | undefined> export type Params = Record<string, string | Array<string> | undefined>
......
...@@ -19,7 +19,7 @@ import { ...@@ -19,7 +19,7 @@ import {
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { makeHangingPromise } from '../dynamic-rendering-utils' import { makeHangingPromise } from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger' import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { import {
describeStringPropertyAccess, describeStringPropertyAccess,
describeHasCheckingStringProperty, describeHasCheckingStringProperty,
......
...@@ -11,15 +11,15 @@ describe('dynamic-requests warnings', () => { ...@@ -11,15 +11,15 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/cookies') const browser = await next.browser('/request/cookies')
const browserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogs const browserConsoleWarnings = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
const terminalCookieErrors = terminalOutput.split('\n').filter((line) => { const terminalCookieErrors = terminalOutput.split('\n').filter((line) => {
return line.includes('Route "/request/cookies') return line.includes('Route "/request/cookies')
}) })
expect({ browserConsoleErrors, terminalCookieErrors }).toEqual({ expect({ browserConsoleWarnings, terminalCookieErrors }).toEqual({
browserConsoleErrors: [ browserConsoleWarnings: [
expect.stringContaining("`cookies().get('page')`."), expect.stringContaining("`cookies().get('page')`."),
expect.stringContaining("`cookies().get('component')`."), expect.stringContaining("`cookies().get('component')`."),
expect.stringContaining("`cookies().has('component')`."), expect.stringContaining("`cookies().has('component')`."),
...@@ -39,16 +39,16 @@ describe('dynamic-requests warnings', () => { ...@@ -39,16 +39,16 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/draftMode') const browser = await next.browser('/request/draftMode')
const browserLogsserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogsserLogs const browserConsoleWarnings = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
const terminalCookieErrors = terminalOutput.split('\n').filter((line) => { const terminalCookieErrors = terminalOutput.split('\n').filter((line) => {
return line.includes('Route "/request/draftMode') return line.includes('Route "/request/draftMode')
}) })
expect({ browserConsoleErrors, terminalCookieErrors }).toEqual({ expect({ browserConsoleWarnings, terminalCookieErrors }).toEqual({
browserConsoleErrors: [ browserConsoleWarnings: [
expect.stringContaining('`draftMode().isEnabled`.'), expect.stringContaining('`draftMode().isEnabled`.'),
expect.stringContaining('`draftMode().isEnabled`.'), expect.stringContaining('`draftMode().isEnabled`.'),
expect.stringContaining('`draftMode().enable()`.'), expect.stringContaining('`draftMode().enable()`.'),
...@@ -68,16 +68,16 @@ describe('dynamic-requests warnings', () => { ...@@ -68,16 +68,16 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/headers') const browser = await next.browser('/request/headers')
const browserLogsserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogsserLogs const browserConsoleWarnings = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
const terminalCookieErrors = terminalOutput.split('\n').filter((line) => { const terminalCookieErrors = terminalOutput.split('\n').filter((line) => {
return line.includes('Route "/request/headers') return line.includes('Route "/request/headers')
}) })
expect({ browserConsoleErrors, terminalCookieErrors }).toEqual({ expect({ browserConsoleWarnings, terminalCookieErrors }).toEqual({
browserConsoleErrors: [ browserConsoleWarnings: [
expect.stringContaining("`headers().get('page')`."), expect.stringContaining("`headers().get('page')`."),
expect.stringContaining("`headers().get('component')`."), expect.stringContaining("`headers().get('component')`."),
expect.stringContaining("`headers().has('component')`."), expect.stringContaining("`headers().has('component')`."),
...@@ -97,16 +97,16 @@ describe('dynamic-requests warnings', () => { ...@@ -97,16 +97,16 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/params/[slug]') const browser = await next.browser('/request/params/[slug]')
const browserLogsserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogsserLogs const browserConsoleWarnings = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
const terminalCookieErrors = terminalOutput.split('\n').filter((line) => { const terminalCookieErrors = terminalOutput.split('\n').filter((line) => {
return line.includes('Route "/request/params/[slug]') return line.includes('Route "/request/params/[slug]')
}) })
expect({ browserConsoleErrors, terminalCookieErrors }).toEqual({ expect({ browserConsoleWarnings, terminalCookieErrors }).toEqual({
browserConsoleErrors: [ browserConsoleWarnings: [
expect.stringContaining('`params.slug`.'), expect.stringContaining('`params.slug`.'),
expect.stringContaining('`params.slug`.'), expect.stringContaining('`params.slug`.'),
expect.stringContaining('`params.slug`.'), expect.stringContaining('`params.slug`.'),
...@@ -126,16 +126,16 @@ describe('dynamic-requests warnings', () => { ...@@ -126,16 +126,16 @@ describe('dynamic-requests warnings', () => {
const browser = await next.browser('/request/searchParams') const browser = await next.browser('/request/searchParams')
const browserLogsserLogs = await browser.log() const browserLogs = await browser.log()
const browserConsoleErrors = browserLogsserLogs const browserConsoleWarnings = browserLogs
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex) const terminalOutput = next.cliOutput.slice(nextDevBootstrapOutputIndex)
const terminalCookieErrors = terminalOutput.split('\n').filter((line) => { const terminalCookieErrors = terminalOutput.split('\n').filter((line) => {
return line.includes('Route "/request/searchParams') return line.includes('Route "/request/searchParams')
}) })
expect({ browserConsoleErrors, terminalCookieErrors }).toEqual({ expect({ browserConsoleWarnings, terminalCookieErrors }).toEqual({
browserConsoleErrors: [ browserConsoleWarnings: [
expect.stringContaining('`searchParams.slug`.'), expect.stringContaining('`searchParams.slug`.'),
expect.stringContaining('`searchParams.slug`.'), expect.stringContaining('`searchParams.slug`.'),
expect.stringContaining('`searchParams.slug`.'), expect.stringContaining('`searchParams.slug`.'),
...@@ -154,24 +154,24 @@ describe('dynamic-requests warnings', () => { ...@@ -154,24 +154,24 @@ describe('dynamic-requests warnings', () => {
it('should have no warnings on normal rsc page without accessing params', async () => { it('should have no warnings on normal rsc page without accessing params', async () => {
const browser = await next.browser('/no-access/normal') const browser = await next.browser('/no-access/normal')
const browserLogItems = await browser.log() const browserLogItems = await browser.log()
const browserConsoleErrors = browserLogItems const browserConsoleWarnings = browserLogItems
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
expect(browserConsoleErrors.length).toBe(0) expect(browserConsoleWarnings.length).toBe(0)
}) })
it('should only have hydration warnings on hydration mismatch page without accessing params', async () => { it('should only have hydration warnings on hydration mismatch page without accessing params', async () => {
const browser = await next.browser('/no-access/mismatch') const browser = await next.browser('/no-access/mismatch')
const browserLogItems = await browser.log() const browserLogItems = await browser.log()
console.log('browserLogItems', browserLogItems) console.log('browserLogItems', browserLogItems)
const browserConsoleErrors = browserLogItems const browserConsoleWarnings = browserLogItems
.filter((log) => log.source === 'error') .filter((log) => log.source === 'warning')
.map((log) => log.message) .map((log) => log.message)
// We assert there are zero logged errors but first we assert specific strings b/c we want a better // We assert there are zero logged errors but first we assert specific strings b/c we want a better
// failure message if these do appear // failure message if these do appear
expect(browserConsoleErrors).toEqual( expect(browserConsoleWarnings).toEqual(
expect.not.arrayContaining([ expect.not.arrayContaining([
expect.stringContaining('param property was accessed directly with'), expect.stringContaining('param property was accessed directly with'),
expect.stringContaining( expect.stringContaining(
...@@ -182,7 +182,7 @@ describe('dynamic-requests warnings', () => { ...@@ -182,7 +182,7 @@ describe('dynamic-requests warnings', () => {
// Even though there is a hydration error it does show up in the logs list b/c it is an // Even though there is a hydration error it does show up in the logs list b/c it is an
// uncaught Error not a console.error. We expect there to be no logged errors // uncaught Error not a console.error. We expect there to be no logged errors
expect(browserConsoleErrors.length).toBe(0) expect(browserConsoleWarnings.length).toBe(0)
}) })
}) })
}) })
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