-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Dedupe sync access warning on the Server by callsite" (#70672)…
… (#70738) Relands #70672 with fixes that were failing in CI. [x-ref](https://github.com/vercel/next.js/actions/runs/11153352206/job/31002090099)
- Loading branch information
Showing
17 changed files
with
484 additions
and
103 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/next/src/server/create-deduped-by-callsite-server-error-loger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react' | ||
|
||
const errorRef: { current: null | string } = { current: null } | ||
|
||
// React.cache is currently only available in canary/experimental React channels. | ||
const cache = | ||
typeof React.cache === 'function' | ||
? React.cache | ||
: (fn: (key: unknown) => void) => fn | ||
|
||
// 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. | ||
const flushCurrentErrorIfNew = cache( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- cache key | ||
(key: unknown) => { | ||
try { | ||
console.error(errorRef.current) | ||
} finally { | ||
errorRef.current = null | ||
} | ||
} | ||
) | ||
|
||
/** | ||
* Creates a function that logs an error message that is deduped by the userland | ||
* callsite. | ||
* This requires no indirection between the call of this function and the userland | ||
* callsite i.e. there's only a single library frame above this. | ||
* Do not use on the Client where sourcemaps and ignore listing might be enabled. | ||
* Only use that for warnings need a fix independent of the callstack. | ||
* | ||
* @param getMessage | ||
* @returns | ||
*/ | ||
export function createDedupedByCallsiteServerErrorLoggerDev<Args extends any[]>( | ||
getMessage: (...args: Args) => string | ||
) { | ||
return function logDedupedError(...args: Args) { | ||
const message = getMessage(...args) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
const callStackFrames = new Error().stack?.split('\n') | ||
if (callStackFrames === undefined || callStackFrames.length < 4) { | ||
console.error(message) | ||
} else { | ||
// Error: | ||
// logDedupedError | ||
// asyncApiBeingAccessedSynchronously | ||
// <userland callsite> | ||
// TODO: This breaks if sourcemaps with ignore lists are enabled. | ||
const key = callStackFrames[3] | ||
errorRef.current = message | ||
flushCurrentErrorIfNew(key) | ||
} | ||
} else { | ||
console.error(message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
test/development/app-dir/dynamic-io-warnings/app/pages/cookies/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { cookies, type UnsafeUnwrappedCookies } from 'next/headers' | ||
|
||
function Component() { | ||
;(cookies() as unknown as UnsafeUnwrappedCookies).get('component') | ||
;(cookies() as unknown as UnsafeUnwrappedCookies).has('component') | ||
|
||
const allCookies = [...(cookies() as unknown as UnsafeUnwrappedCookies)] | ||
return <pre>{JSON.stringify(allCookies, null, 2)}</pre> | ||
} | ||
|
||
export default function Page() { | ||
;(cookies() as unknown as UnsafeUnwrappedCookies).get('page') | ||
return ( | ||
<> | ||
<Component /> | ||
<Component /> | ||
</> | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
test/development/app-dir/dynamic-io-warnings/app/pages/draftMode/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { draftMode, type UnsafeUnwrappedDraftMode } from 'next/headers' | ||
|
||
function Component() { | ||
const isEnabled = (draftMode() as unknown as UnsafeUnwrappedDraftMode) | ||
.isEnabled | ||
;(draftMode() as unknown as UnsafeUnwrappedDraftMode).enable() | ||
|
||
const clonedDraftMode = { | ||
...(draftMode() as unknown as UnsafeUnwrappedDraftMode), | ||
} | ||
return <pre>{JSON.stringify({ clonedDraftMode, isEnabled }, null, 2)}</pre> | ||
} | ||
|
||
export default function Page() { | ||
const isEnabled = (draftMode() as unknown as UnsafeUnwrappedDraftMode) | ||
.isEnabled | ||
return ( | ||
<> | ||
<pre>{JSON.stringify({ isEnabled }, null, 2)}</pre> | ||
<Component /> | ||
<Component /> | ||
</> | ||
) | ||
} |
Oops, something went wrong.