fix(@astrojs/cloudflare): fix imageService: 'compile' silent noop with prerenderEnvironment: 'node'#17347
Open
astrobot-houston wants to merge 2 commits into
Open
fix(@astrojs/cloudflare): fix imageService: 'compile' silent noop with prerenderEnvironment: 'node'#17347astrobot-houston wants to merge 2 commits into
imageService: 'compile' silent noop with prerenderEnvironment: 'node'#17347astrobot-houston wants to merge 2 commits into
Conversation
… node (#17346) When `imageService: 'compile'` was used with `prerenderEnvironment: 'node'`, images were silently copied without optimization (PNG bytes in .webp files). The root cause was that `collectStaticImages` — which installs sharp for build-time transforms — only ran in the workerd prerenderer path. The node prerender path used the workerd passthrough stub instead. Fix: wrap the default prerenderer with a `collectStaticImages` method that installs sharp (or the user's custom service) before image generation runs, and restore the default prerender entrypoint that gets skipped when `settings.prerenderer` is set.
1 task
🦋 Changeset detectedLatest commit: fffb454 The changes in this PR will be included in the next version bump. This PR includes changesets to release 40 packages
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 |
adamchal
reviewed
Jul 10, 2026
adamchal
left a comment
Contributor
There was a problem hiding this comment.
@ematipico embarrassed I missed this one. Got a little fixated on the workerd prerenderer!
Read through the fix and it seems logical and the tests look good.
I also tested this fix locally to make sure that it does not regress anything in #17099 or #17285 on the workerd side.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where
imageService: 'compile'produced unoptimized images (byte-for-byte source copies with incorrect extensions, e.g. PNG bytes in a.webpfile) whenprerenderEnvironmentwas set to'node'. No warning or error was emitted — the build completed silently.Closes #17346
Root Cause
Two interacting issues:
collectStaticImagesonly existed on the workerd prerenderer. This method installs sharp (or a user-configured image service) intoglobalThis.astroAsset.imageServicebefore the image generation pipeline runs. WhenprerenderEnvironment: 'node', the default Node prerenderer was used instead — which had nocollectStaticImages, so image transforms fell back to the workerd passthrough stub, returning input buffers unchanged.The default prerender entrypoint was skipped when
setPrerendererwas called. Astro skips setting the prerender entrypoint whensettings.prerendereris truthy, so restoring it manually was also required.Fix
In
packages/integrations/cloudflare/src/index.ts:astro:build:starthook — Added anelse if (hasBuildImageService)branch forprerenderEnvironment: 'node'. Wraps the default prerenderer with acollectStaticImagesmethod that installs sharp (or the user's custom service) before image generation runs, mirroring the existing workerd prerenderer behavior.astro:build:setuphook — Restores the default prerender entrypoint whenprerenderEnvironment: 'node'andhasBuildImageServiceare both true, since it gets skipped whensetPrerendereris called.Before:
(before: 12kB, after: 12kB)— output.webpis actually PNG bytesAfter:
(before: 12kB, after: 0kB)— proper WebP output (~494 bytes)Testing
Added
packages/integrations/cloudflare/test/compile-image-service.test.tswith tests covering:imageService: 'compile'withprerenderEnvironment: 'node'(the bug scenario)prerenderEnvironment: 'node'All existing tests continue to pass.