nextjs - 💡(How to fix) Fix /_next/image returns 504 / infinite loading for external images when using bun install on Linux server with PM2 [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#91712Fetched 2026-04-08 01:03:50
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Author
Timeline (top)
labeled ×2closed ×1commented ×1issue_type_added ×1

Error Message

⨯ Error: The router state header was sent but could not be parsed. Images load correctly, same as in 16.1.6. If sharp is unavailable or incompatible at runtime, Next.js should fail fast with a clear error — not silently hang the /_next/image handler until timeout.

Fix Action

Fix / Workaround

Workaround in use: images: { unoptimized: true } — functional but loses all image optimization benefits

Code Example

Operating System:
  Platform: linux
  Arch: x64
  Version: #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025
  Available memory (MB): 3820
  Available CPU cores: 3
Binaries:
  Node: 24.12.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.0 // Latest available version is detected (16.2.0).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

---

images: {
  remotePatterns: [
    {
      protocol: "https",
      hostname: "*.s3.eu-north-1.amazonaws.com",
      pathname: "/**",
    },
  ],
}
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://nextjs.org/docs/app/getting-started/images

To Reproduce

  1. Build project using bun install && bun run build Ubuntu 24 Linux server
  2. Run bun install --production on the server (without rebuilding sharp)
  3. Start with PM2 — pm2 start ecosystem.config.js
  4. Open any page with <Image /> component pointing to external URL (S3)
  5. Observe infinite loading → 504

Current vs. Expected behavior

Current behavior

After upgrading from 16.1.6 → 16.2.0, all <Image /> components hang indefinitely on production server (Ubuntu 24, PM2, deployed with bun) and eventually return 504. This affects both external images (AWS S3 via remotePatterns) and local static assets. The /_next/image optimization endpoint never responds.

Server logs:

⨯ The requested resource isn't a valid image for https://....s3.amazonaws.com/user-media/....png received null ⨯ Error: The router state header was sent but could not be parsed.

Expected behavior

Images load correctly, same as in 16.1.6. If sharp is unavailable or incompatible at runtime, Next.js should fail fast with a clear error — not silently hang the /_next/image handler until timeout.

Notes

Works perfectly in local development on both versions

Adding images: { unoptimized: true } to next.config.js immediately fixes the issue, confirming the problem is isolated to the image optimization pipeline

Suspected cause: sharp native binary compiled on the build machine (macOS/Windows) is not recompiled for Linux x64 when running bun install --production on the server. 16.2.0 appears to handle this failure differently than 16.1.6

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025
  Available memory (MB): 3820
  Available CPU cores: 3
Binaries:
  Node: 24.12.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.0 // Latest available version is detected (16.2.0).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Image (next/image)

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

Deploying to: Self-hosted Ubuntu 24 LTS VPS, managed with PM2 (cluster mode)

Reproducible: Production only. Local development (macOS) works fine on both 16.1.6 and 16.2.0

Build & deploy flow:

  • bun install && bun run build on the server (same machine)
  • Built artifacts synced via rsync (excluding node_modules/)
  • bun install --production in production directory
  • pm2 reload

sharp is the likely culprit — after running npm install --platform=linux --arch=x64 --legacy-peer-deps sharp manually on the server and removing unoptimized: true, images still failed. The bun package manager appears to not correctly handle sharp's native binary compilation for the target platform regardless of flags.

Browser: Reproduced in all browsers (Chrome, Firefox, Safari) — rules out browser-specific issue

next.config.js images config:

images: {
  remotePatterns: [
    {
      protocol: "https",
      hostname: "*.s3.eu-north-1.amazonaws.com",
      pathname: "/**",
    },
  ],
}

Workaround in use: images: { unoptimized: true } — functional but loses all image optimization benefits

extent analysis

Fix Plan

To resolve the issue with Next.js image optimization, follow these steps:

  • Recompile sharp for Linux x64:
    1. Run npm install --platform=linux --arch=x64 sharp on the server to ensure sharp is compiled for the correct platform.
    2. Remove unoptimized: true from next.config.js to enable image optimization.
  • Update bun install command:
    1. Use bun install --platform=linux --arch=x64 to install dependencies, including sharp, for the correct platform.
  • Verify sharp version:
    1. Check the sharp version installed on the server using npm ls sharp to ensure it matches the expected version.

Example next.config.js:

module.exports = {
  // ... other configurations ...
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "*.s3.eu-north-1.amazonaws.com",
        pathname: "/**",
      },
    ],
  },
}

Example package.json script:

"scripts": {
  "build": "bun run build",
  "install": "bun install --platform=linux --arch=x64"
}

Verification

After applying the fix, verify that images load correctly by:

  1. Restarting the PM2 process using pm2 reload.
  2. Accessing a page with an <Image /> component pointing to an external URL (S3).
  3. Checking the server logs for any errors related to image optimization.

Extra Tips

  • Ensure that the sharp package is installed and compiled for the correct platform (Linux x64) to avoid image optimization issues.
  • Use the --platform and --arch flags with bun install to specify the target platform and architecture.
  • Monitor server logs for any errors related to image optimization and adjust the next.config.js and package.json configurations as needed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Images load correctly, same as in 16.1.6. If sharp is unavailable or incompatible at runtime, Next.js should fail fast with a clear error — not silently hang the /_next/image handler until timeout.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING