fix(cloudflare): resolve image format from the source when f is absent - #17677
Open
andreialba wants to merge 1 commit into
Open
fix(cloudflare): resolve image format from the source when f is absent#17677andreialba wants to merge 1 commit into
f is absent#17677andreialba wants to merge 1 commit into
Conversation
Astro's base image service only sets the `f` (format) parameter on `/_image` URLs when it can infer a format from the source, and deliberately leaves it off otherwise so the image service resolves the format from the source itself. Extensionless remote images take that path, GitHub avatars such as `https://avatars.githubusercontent.com/u/1234` being the common case, and the Cloudflare image transform endpoint rejected them with `400 Unsupported format: null`. Fall back to the source's media type when `f` is absent: SVG sources are passed through unchanged and everything else is encoded as WebP, matching core's `resolveDefaultOutputFormat`. Requests that explicitly ask for a format the IMAGES binding cannot produce still return a 400. This also fixes SVG images, which core requests as `f=svg` and which previously failed the same way. Fixes withastro/astro.build#2610
🦋 Changeset detectedLatest commit: 6c1f435 The changes in this PR will be included in the next version bump. This PR includes changesets to release 43 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 |
There was a problem hiding this comment.
Findings
No findings.
Review Status
Scope: PR #17677 @astrojs/cloudflare image binding fix (base 09f0dc7 through head 6c1f4352).
Changeset: present and covers @astrojs/cloudflare.
Validation: Static review only; no project code, tests, builds, or checks were run.
Fetch: not needed for the supplied PR scope.
This review was made by an LLM. The analysis may be wrong, and reports might be incorrect.
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.
Changes
Remote images whose URL has no file extension fail with
400 Unsupported format: nullunder thecloudflare-bindingimage service (the adapter default).Astro's
baseService.validateOptionsonly setsoptions.formatwhen it can infer a source format from the URL, and since #16665 deliberately leaves itundefinedotherwise so the image service resolves the format from the source bytes instead — which is what the Sharp service does atsharp.ts#L155-L156. Withformatundefined, nofparameter is emitted onto the/_imageURL, andtransformStreamtreated a missingfas a client error.Extensionless remote images are the common case here — GitHub avatars like
https://avatars.githubusercontent.com/u/192622539?s=200&v=4have no extension to infer from, which is why every theme author avatar on astro.build is currently broken:transformStreamnow takes the source's media type and uses it whenfis absent: SVG sources pass through unchanged, everything else is encoded as WebP. This mirrors core'sresolveDefaultOutputFormat, which webp-encodes every non-SVG source.f=tiff) still return400.f=svgand which previously failed the same way — the IMAGES binding cannot emit SVG, so those bytes are served as-is.Fixes withastro/astro.build#2610
Testing
Added four cases to
test/binding-image-service.test.ts(build + preview, exercising the IMAGES binding for real) covering remote/local sources with nof, and SVG passthrough. A local HTTP server serves images from extensionless paths to reproduce the GitHub avatar shape. Also added the no-fcase totest/dev-image-endpoint.test.ts.All four new binding tests fail on
mainwith400and pass with this change; the seven pre-existing tests in that file are unaffected.Note
Two things worth flagging for maintainers, both pre-existing and left untouched here:
caches.defaultis persisted undertest/fixtures/binding-image-service/.wrangler/state/v3/cache, so a previously-cached200can mask a genuine failure for any stable/_imageURL. I had to clear that directory to see the new tests fail onmain.createPreviewServerreturns the requested port rather than the bound one (preview.ts#L97), so the whole suite 404s if port 4321 is already taken.Docs
No docs changes needed. This restores the documented behavior of
image.remotePatterns/image.domainsfor remote images; there's no API or configuration surface change.