Replies: 2 comments
-
|
Hi! 👋 Thanks for the detailed investigation — this is really helpful. From what you described, the root cause seems to be corrupted cache entries (zero-sized files) inside the Next.js image cache directory. When the cache is initialized (getOrInitDiskLRU), those invalid files are still being read and inserted into the LRU, which causes the initialization to fail repeatedly for every /_next/image request. A practical workaround that usually resolves this:
This forces Next.js to rebuild the cache from scratch and avoids loading corrupted entries. Regarding your suggestion — adding a guard in
This would prevent invalid cache entries from ever reaching Even though the root cause of the zero-sized files is still unclear, adding this validation would improve resilience and avoid repeated failures. Thanks again for sharing this — super useful insight 🙌 |
Beta Was this translation helpful? Give feedback.
-
|
This is a known bug in Next.js 16.x. Here's a workaround and explanation: Root CauseThe LRU cache initialization fails when it encounters a cache file with
Immediate Workaround# Clean the corrupted image cache
rm -rf .next/cache/images/*
# Or for standalone builds
rm -rf /var/cache/nextjs/images/*Programmatic FixCreate a pre-start script to clean zero-byte cache files: // scripts/clean-image-cache.js
const fs = require('fs');
const path = require('path');
const cacheDir = path.join(process.cwd(), '.next/cache/images');
if (fs.existsSync(cacheDir)) {
const files = fs.readdirSync(cacheDir);
let cleaned = 0;
for (const file of files) {
const filePath = path.join(cacheDir, file);
const stats = fs.statSync(filePath);
if (stats.size === 0) {
fs.unlinkSync(filePath);
cleaned++;
}
}
console.log(`Cleaned ${cleaned} zero-byte cache files`);
}Add to your {
"scripts": {
"predev": "node scripts/clean-image-cache.js",
"prebuild": "node scripts/clean-image-cache.js"
}
}Prevent Recurrence// next.config.js
module.exports = {
images: {
// Limit cache size to prevent disk issues
minimumCacheTTL: 60 * 60 * 24, // 24 hours
deviceSizes: [640, 750, 828, 1080, 1200], // Fewer sizes = smaller cache
},
};Report to Next.js TeamThis should be handled gracefully in the LRU cache initialization. Consider opening an issue at https://github.com/vercel/next.js/issues with:
The fix should be: skip zero-byte files during cache initialization instead of throwing. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
We discovered this on observing a large amount of log entries that looked like the below following an upgrade to Next 16.2.1:
The error message was introduced in PR 89040 to address Issue 89033. The message was being logged for every
/_next/imagerequest, and on investigation we determined that it was happening in the cache initialisation rather than for the requested image itself. It turns out we had two image cache entries where zero-sized files had been written to disk, and whengetOrInitDiskLRUattempted to load one of those files set them to the lru cache, it would break, resulting in the cache initialization failing on every image request, and no images being cached. After completely deleting the zero-sized files and their folders and restarting the app the problem was resolved.I have no idea how we acquired the zero-sized files in the first place so I can't really open an issue for that, however it might be beneficial to add a sanity check to the image optimizer
readFromCacheDirfunction to throw if the file being read tobufferis zero-sized so the entry is skipped byinitCacheEntries, preventing it from being passed togetOrInitDiskLRU.Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions