Skip to content

fix(media): make ipx optional to support Cloudflare Workers#404

Merged
larbish merged 3 commits intonuxt-content:mainfrom
CalmProton:fix/ipx-optional-cloudflare-workers
Apr 8, 2026
Merged

fix(media): make ipx optional to support Cloudflare Workers#404
larbish merged 3 commits intonuxt-content:mainfrom
CalmProton:fix/ipx-optional-cloudflare-workers

Conversation

@CalmProton
Copy link
Copy Markdown
Contributor

Problem

Builds with the cloudflare-worker Nitro preset fail since v1.5.0 (#397):

Error: Cannot resolve "@img/sharp-wasm32/versions" from ".../node_modules/sharp/lib/utility.js" and externals are not allowed!

ipx depends on sharp which requires native binaries that are unavailable on platforms like Cloudflare Workers. Since ipx was added as a hard dependency in #369, Rollup resolves the entire sharp dependency tree during bundling and fails.

Solution

Make ipx an optional, gracefully-degrading dependency:

  1. package.json — Move ipx from dependencies to optionalDependencies
  2. ipx.ts — Use a variable-based dynamic import (const id = 'ipx'; await import(id)) so Rollup cannot statically follow the import chain into sharp. Falls back to null when ipx is unavailable.
  3. [...path].ts — When ipx is unavailable at runtime, serve original (unoptimized) images via the existing getOriginalImage() fallback path
  4. module.ts — Log a build-time warning when ipx is not installed

Behavior

Platform ipx installed Result
Node.js (Vercel, Netlify, etc.) Yes Full IPX thumbnail optimization (no change)
Cloudflare Workers Yes (but sharp unusable) Graceful fallback to original images at runtime
Any platform No Build-time warning + original images served

The degraded experience is: the media picker shows full-size images instead of 200×200 thumbnails. Fully functional, slightly more bandwidth.

Testing

  • NITRO_PRESET=cloudflare-worker npx nuxi build — builds successfully (was failing before)
  • Warning message confirmed when ipx is absent
  • Existing tests pass (178/178 assertions)

Closes #397

ipx depends on sharp which requires native binaries that are
unavailable on platforms like Cloudflare Workers, causing builds
to fail with `Cannot resolve @img/sharp-wasm32/versions`.

- Move ipx from dependencies to optionalDependencies
- Use variable-based dynamic import so Rollup/Nitro does not
  statically follow the import chain into sharp
- Gracefully fall back to serving original (unoptimized) images
  when ipx is unavailable at runtime
- Log a build-time warning when ipx is not installed

Closes nuxt-content#397
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Mar 27, 2026

@CalmProton is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

@larbish larbish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! A few changes I made on top:

  • removed ipx from deps (keeping only optionalDependencies), are you sure the graceful install failure on Cloudflare happened with it? optionalDeps still installs automatically on platforms that support sharp

  • throw a proper 404 instead of falling with an empty 200 when getOriginalImage returns null

  • removed the createRequire since ipx is still installed by default (and gracefully failing on cloudflare)

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nuxt.studio Ready Ready Preview Apr 7, 2026 4:45pm

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 7, 2026

npm i https://pkg.pr.new/nuxt-studio@404

commit: c27631e

@proudguy69
Copy link
Copy Markdown
Contributor

Preview works on cloudflare-pages preset!

@larbish larbish merged commit e3db8ea into nuxt-content:main Apr 8, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v1.5 fails to build on the cloudflare-worker nitro preset

3 participants