next.js
9e5759de - fix: ensure LRU cache items have minimum size of 1 to prevent unbounded growth (#89040)

Commit
279 days ago
fix: ensure LRU cache items have minimum size of 1 to prevent unbounded growth (#89040) ### What? Prevent LRU cache unbounded growth by requiring `calculateSize` to return values > 0. ### Why? When `calculateSize` returned 0 (e.g., for `null` values in the filesystem route cache), items were never evicted because they didn't contribute to `totalSize`. This caused unbounded memory growth when handling dynamic routes like `/api/logs/[id]` where each unique ID created a new cache entry with size 0. ### How? 1. **LRUCache now throws an error if size ≤ 0** - This catches bugs at development time rather than silently masking them: ```typescript if (size <= 0) { throw new Error( \`LRUCache: calculateSize returned ${size}, but size must be > 0. \` + \`Items with size 0 would never be evicted, causing unbounded cache growth.\` ) } ``` 2. **Fixed filesystem.ts** - Null entries (negative cache) now return size 1 instead of 0: ```typescript if (!value) { // Null entries (negative cache) still need a non-zero size for LRU eviction return 1 } ``` 3. **Fixed next-dev-server.ts** - Static paths cache now uses `|| 1` instead of `?? 0`: ```typescript return JSON.stringify(value.staticPaths)?.length || 1 ```
Author
Parents
Loading