Skip to content

[recovery] Disable Next image on-disk cache on read-only Netlify Lambda#18781

Open
pettinarip wants to merge 1 commit into
devfrom
recovery/fix/image-disk-cache-readonly-lambda
Open

[recovery] Disable Next image on-disk cache on read-only Netlify Lambda#18781
pettinarip wants to merge 1 commit into
devfrom
recovery/fix/image-disk-cache-readonly-lambda

Conversation

@pettinarip

Copy link
Copy Markdown
Member

Error

Recurring unhandled promise rejection in the ___netlify-server-handler function (11 hits, first seen 2026-05-25):

[ERROR] Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection",
"errorMessage":"Error: ENOENT: no such file or directory, mkdir '/var/task/.next/cache'",
... at .../next/dist/server/lib/disk-lru-cache.external.js:36:17}

Root cause

Next.js 16's image optimizer (ImageOptimizerCache, next/dist/server/image-optimizer.js:686-694) eagerly initializes an on-disk LRU cache in its constructor whenever:

  • no custom cacheHandler is set, and
  • images.maximumDiskCacheSize !== 0, and
  • experimental.isrFlushToDisk is truthy

All three hold by default in our config. That eager getOrInitDiskLRU(...) promise (disk-lru-cache.external.js:36) calls fs.promises.mkdir(cacheDir, { recursive: true }) where cacheDir = .next/cache/images. On Netlify's read-only Lambda filesystem (/var/task), creating .next/cache fails with ENOENT.

Because the promise is created eagerly in the constructor and not awaited or caught at that point (only later inside get()/set()), the rejection escapes as an unhandled promise rejection and crashes/pollutes the server handler.

Fix

Set images.maximumDiskCacheSize: 0 in next.config.js. Per the constructor guard, this leaves isDiskCacheEnabled false so getOrInitDiskLRU is never called — no mkdir, no rejection.

maximumDiskCacheSize is a documented public image option (z.number().int().min(0).optional() in Next's config schema). The local disk image cache is useless on ephemeral, read-only Lambda anyway — optimized images are served through Netlify's image CDN (NEXT_FORCE_EDGE_IMAGES=true), so there is no functional regression.

Affected files

  • next.config.js

Verification

  • pnpm type-check
  • pnpm exec eslint next.config.js ✅ (clean)

🤖 Generated with Claude Code

@pettinarip pettinarip added the recovery-agent Created by the Recovery Agent label Jul 13, 2026
@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for ethereumorg ready!

Name Link
🔨 Latest commit e6ba8cc
🔍 Latest deploy log https://app.netlify.com/projects/ethereumorg/deploys/6a57ad7f1f3d740007aa8dac
😎 Deploy Preview https://deploy-preview-18781.ethereum.it
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
7 paths audited
Performance: 61 (🟢 up 5 from production)
Accessibility: 95 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (no change from production)
PWA: 59 (🔴 down 1 from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the config ⚙️ Changes to configuration files label Jul 13, 2026
Next 16's image optimizer eagerly initializes a disk LRU cache in its
constructor, calling mkdir('.next/cache/images') at runtime. On Netlify's
read-only Lambda filesystem (/var/task) this mkdir fails with ENOENT, and
because the init promise is created eagerly and not awaited/caught at that
point, it surfaces as an unhandled promise rejection in the
___netlify-server-handler function.

Set images.maximumDiskCacheSize to 0 so the disk cache is never
initialized (isDiskCacheEnabled stays false, getOrInitDiskLRU is never
called). The local disk cache is useless on ephemeral read-only functions
anyway -- optimized images are served via Netlify's image CDN
(NEXT_FORCE_EDGE_IMAGES).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@pettinarip pettinarip marked this pull request as ready for review July 15, 2026 15:55
@pettinarip pettinarip requested a review from wackerow as a code owner July 15, 2026 15:55
@pettinarip pettinarip force-pushed the recovery/fix/image-disk-cache-readonly-lambda branch from 8bc41ae to e6ba8cc Compare July 15, 2026 15:55
@github-actions

Copy link
Copy Markdown
Contributor

🔎 First-pass review — ✅ Looks mergeable

Sets images.maximumDiskCacheSize: 0 in next.config.js to stop Next 16's image optimizer from eagerly calling mkdir('.next/cache/images') on Netlify's read-only Lambda FS, which was surfacing as a recurring unhandled promise rejection. The diagnosis is accurate and the fix is minimal (4 lines, comment + one option). Verified NEXT_FORCE_EDGE_IMAGES="true" is set in netlify.toml, so optimized images are already served via Netlify's image CDN — disabling the local disk LRU is a no-op functionally on Lambda. No convention issues.

Analysis
  • Lane: tooling (single config file, next.config.js)
  • Checked: option placement within images config ✅; maximumDiskCacheSize is a documented public image option; CDN claim verified against netlify.toml:18 (NEXT_FORCE_EDGE_IMAGES="true") ✅; no functional regression on the edge-image path.
  • Non-blocking: none. CI may still be running — no failing checks observed.

Generated by PR Reviewer (team) for #18781 · 55.2 AIC · ⌖ 27.4 AIC · ⊞ 5.2K ·

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch recovery/fix/image-disk-cache-readonly-lambda

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

config ⚙️ Changes to configuration files needs review 👀 recovery-agent Created by the Recovery Agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant