fix(cloudflare): enable Sharp image optimization for default cloudflare-binding mode#16194
fix(cloudflare): enable Sharp image optimization for default cloudflare-binding mode#16194Desel72 wants to merge 2 commits intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: e3b92c6 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
I'm not sure if this is correct. In theory the image binding should have sharp for prerender optimization. And the binding should be available when workerd is used for prerendering.
cc @OliverSpeir @Princesseuh who might have more context to share
|
Cloudflare-binding intentionally does not transform images on pre-rendered routes locally during build. The intention is they go to the image endpoint at request time as well, one benefit of this is faster build times |
|
It should use the cloudflare binding itself to optimize them during build time though, not sharp. So like during |
|
Thank you for the context, @alexanderniebuhr! I think I misunderstood the design intent behind |
@Desel72 yeah after discussion we think this the the better way. I'll update the original issue so it makes more sense. Would you like to still work on that? If so, do you want to use this PR or should we close it and you can create a new one? |
|
I'd love to use this PR @alexanderniebuhr I will solve perfectly. |
… in cloudflare-binding mode (withastro#16035) When using the default `cloudflare-binding` image service, prerendered pages had un-optimized images because the IMAGES binding was only used at runtime. Now during the build, `handleStaticImagesRequest` uses the IMAGES binding in workerd to transform images, and the optimized bytes are written directly to the output directory. Falls back to Sharp if the binding is unavailable.
|
Hi @matthewp @alexanderniebuhr I've done. Welcome to your feedback. Thanks |
|
@Desel72 we are going to review the PR as soon as we have bandwidth, but please fix the failing lint check :) |
|
@alexanderniebuhr Thanks for your reviewing. I've solved. |
The default
cloudflare-bindingimage service relied on the IMAGES binding for transforms, but this binding is unavailable during the Node-side build pipeline. Prerendered pages were left with un-optimized images. Expand the Sharp fallback to cover bothcompileandcloudflare-bindingbuild services so that static images are optimized at build time.Fixes #16035
Changes
needsBuildTimeOptimizationflag that covers bothcompileandcloudflare-bindingbuild servicescompileImageConfigis now passed to the workerd prerender handler forcloudflare-binding, enablinginstallAddStaticImageto track images during prerenderingcollectStaticImagesis now defined forcloudflare-binding, allowing Sharp to process tracked images on the Node side during build@astrojs/cloudflarepatchBefore: Default
cloudflare-bindingmode skipped build-time image optimization entirely — prerendered/static pages contained un-optimized images.After: Sharp processes images at build time for both
compileandcloudflare-bindingmodes, producing optimized output.Testing
All 136 existing Cloudflare adapter tests pass with 0 failures, including:
compile-image-service.test.js(5/5 pass)binding-image-service.test.js(6/6 pass)external-image-service.test.js(2/2 pass)dev-image-endpoint.test.js(6/6 pass)prerenderer-errors.test.js(1/1 pass)static.test.js(1/1 pass)Reproduction repo: https://github.com/alexanderniebuhr/astro-v6-repro
Docs
No docs changes needed. This fix aligns the default
cloudflare-bindingbehavior with what users already expect — image optimization should work out of the box. The existing Cloudflare adapter docs describecloudflare-bindingas the default image service; this change ensures it actually optimizes images at build time.