mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
fix: memory leak from warnOnce() (#73483)
The `warnOnce()` function is used to warn only once time, however this means we might be storing every possible message in memory forever (aka memory leak) since it can grow unbounded. This PR changes the behavior to only store only 10KB in a Least Recently Used (LRU) cache. The tradeoff here is that you might see the same warning twice if you have a lot of unique warnings.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { bold, green, magenta, red, yellow, white } from '../../lib/picocolors'
|
||||
import { LRUCache } from '../../server/lib/lru-cache'
|
||||
|
||||
export const prefixes = {
|
||||
wait: white(bold('○')),
|
||||
@@ -76,11 +77,12 @@ export function trace(...message: any[]) {
|
||||
prefixedLog('trace', ...message)
|
||||
}
|
||||
|
||||
const warnOnceMessages = new Set()
|
||||
export function warnOnce(...message: any[]) {
|
||||
if (!warnOnceMessages.has(message[0])) {
|
||||
warnOnceMessages.add(message.join(' '))
|
||||
const warnOnceCache = new LRUCache<string>(10_000, (message) => message.length)
|
||||
|
||||
export function warnOnce(...message: any[]) {
|
||||
const key = message.join(' ')
|
||||
if (!warnOnceCache.has(key)) {
|
||||
warnOnceCache.set(key)
|
||||
warn(...message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user