-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into add-statsig-app-router-example
- Loading branch information
Showing
31 changed files
with
765 additions
and
144 deletions.
There are no files selected for viewing
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
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.