Skip to content

image optimization using cloudflare-binding does not optimize during build time #16035

@alexanderniebuhr

Description

@alexanderniebuhr

Astro Info

Astro                    v6.0.8
Node                     v25.8.1
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             none

Describe the Bug

When using image optimization with the Cloudflare adapter and the cloudflare-binding option, images on prerendered pages are not optimized during build time. Instead of emitting transformed image variants into dist, Astro outputs image URLs that point to the runtime _image endpoint backed by the Cloudflare Images binding.

This is the current intended behavior, but it makes cloudflare-binding effectively runtime-only for prerendered output.

Today, users can configure:

{ build: 'compile', runtime: 'cloudflare-binding' }

This enables image optimization during both build and runtime, but the two stages use different implementations:

  • build: 'compile' uses local sharp
  • runtime: 'cloudflare-binding' uses Cloudflare Images infrastructure

Because of that, there is no way to use the Cloudflare binding for both build-time and runtime optimization with the same behavior and feature set.

For prerendered pages specifically, this means:

  • transformed image assets are not written to dist
  • output is not fully static for images
  • build-time and runtime image behavior can differ

What's the expected result?

cloudflare-binding should also be usable as a build-time image service for prerendered pages.

That would allow Astro to generate optimized image assets during build and emit them into dist, while still using the same Cloudflare-based image pipeline that is used at runtime.

This is especially important when Wrangler is configured like this:

"images": {
  "binding": "IMAGES",
  "remote": true
}

With remote: true, Cloudflare can use its remote Images API instead of falling back to local sharp. That makes it possible to use the higher-fidelity Cloudflare image pipeline and features consistently. Right now, there is no way to get that same behavior during build for prerendered pages.

Expected behavior for these configurations:

{ build: 'compile', runtime: 'cloudflare-binding' }

Keep the current behavior:

  • optimize at build time with local sharp
  • optimize at runtime with the Cloudflare binding
{ build: 'cloudflare-binding', runtime: 'cloudflare-binding' }

Use the Cloudflare binding for both:

  • build-time optimization of prerendered images
  • runtime optimization of on-demand images

For prerendered pages, transformed assets should be emitted into dist.

{ build: 'runtime-endpoint', runtime: 'cloudflare-binding' }

Keep the current runtime-only behavior:

  • no build-time transforms for prerendered images
  • all optimization happens through the runtime endpoint

Another option is to use a transformAtBuild flag as shown in this draft PR.

Link to Minimal Reproducible Example

https://github.com/alexanderniebuhr/astro-v6-repro

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)pkg: cloudflareRelated to the Cloudflare adapter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions