-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: update Cloudflare adapter's default image service #15435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Princesseuh
merged 21 commits into
withastro:main
from
rururux:cloudflare-image-component
Feb 24, 2026
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b305c97
fix: use `@astrojs/cloudflare/image-endpoint` for Cloudflare dev server
rururux 015b212
Change the entrypoint configuration method
rururux 26a8abd
Merge branch 'main' into cloudflare-image-component
rururux aabfd32
Merge branch 'fix/hahahah-picomatch-cjs' into cloudflare-image-component
Princesseuh 31e58e9
fix(assets): precompile dev deny glob pattern to avoid CJS deps
Princesseuh 2d9a5ec
feat: change default to bindings
Princesseuh 9152bfd
fix: explain comment
Princesseuh 24d2e58
Merge branch 'main' into cloudflare-image-component
Princesseuh 04380dc
fix: use generic endpoint in passthrough
Princesseuh f518c40
chore: lockfile
Princesseuh b4dd89c
Merge branch 'main' into cloudflare-image-component
Princesseuh 442d013
chore: changeset
Princesseuh 943c250
Merge remote-tracking branch 'origin/main' into cloudflare-image-comp…
OliverSpeir 260c122
feat: cache cloudflare-binding transforms
OliverSpeir 66a44d7
feat: fix/improve compile
OliverSpeir a829eb6
Merge branch 'main' into cloudflare-image-component
Princesseuh f0a8d3f
remove `debug` package
rururux 6d1c5bd
fix changesets
OliverSpeir d62a625
Update cloudflare-image-service-object.md
OliverSpeir ee449bd
Update cloudflare-image-component.md
OliverSpeir 233774b
Apply suggestion from @sarah11918
Princesseuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@astrojs/cloudflare": major | ||
| --- | ||
|
|
||
| Changes the default image service from `compile` to `cloudflare-binding`. Image services options that resulted in broken images in development due to Node JS incompatiblities have now been updated to use the noop passthrough image service in dev mode. - ([Cloudflare v13 and Astro6 upgrade guidance](https://v6.docs.astro.build/en/guides/integrations-guide/cloudflare/#changed-imageservice-default)) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "astro": patch | ||
| --- | ||
|
|
||
| Improves compatibility of the built-in image endpoint with runtimes that don't support CJS dependencies correctly |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@astrojs/cloudflare": minor | ||
| --- | ||
|
|
||
| Adds support for configuring the image service as an object with separate `build` and `runtime` options | ||
|
|
||
| It is now possible to set both a build time and runtime service independently. Currently, `'compile'` is the only available build time option. The supported runtime options are `'passthrough'` (default) and `'cloudflare-binding'`: | ||
|
|
||
| ```js title="astro.config.mjs" ins={6} | ||
| import { defineConfig } from 'astro/config'; | ||
| import cloudflare from '@astrojs/cloudflare'; | ||
|
|
||
| export default defineConfig({ | ||
| adapter: cloudflare({ | ||
| imageService: { build: 'compile', runtime: 'cloudflare-binding' } | ||
| }), | ||
| }); | ||
|
|
||
| See the [Cloudflare adapter `imageService` docs](/en/guides/integrations-guide/cloudflare/#imageservice) for more information about configuring your image service. | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { getConfiguredImageService, getImage } from './internal.js'; | ||
| export { baseService, isLocalService } from './services/service.js'; | ||
| export { hashTransform, propsToFilename } from './utils/hash.js'; | ||
| export type { LocalImageProps, RemoteImageProps } from './types.js'; |
149 changes: 149 additions & 0 deletions
149
packages/astro/src/assets/utils/deterministic-string.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /** | ||
| * Vendored from deterministic-object-hash@2.0.2 (MIT) | ||
| * https://github.com/nicholasgasior/deterministic-object-hash | ||
| * | ||
| * Only `deterministicString` is needed - the async `deterministicHash` (which | ||
| * pulls in `node:crypto`) is intentionally excluded so this module stays | ||
| * runtime-agnostic (works in Node, workerd, browsers, etc.). | ||
| */ | ||
|
|
||
| const objConstructorString = Function.prototype.toString.call(Object); | ||
|
|
||
| function isPlainObject(value: unknown): value is Record<string | symbol, unknown> { | ||
| if ( | ||
| typeof value !== 'object' || | ||
| value === null || | ||
| Object.prototype.toString.call(value) !== '[object Object]' | ||
| ) { | ||
| return false; | ||
| } | ||
| const proto = Object.getPrototypeOf(value); | ||
| if (proto === null) { | ||
| return true; | ||
| } | ||
| if (!Object.prototype.hasOwnProperty.call(proto, 'constructor')) { | ||
| return false; | ||
| } | ||
| return ( | ||
| typeof proto.constructor === 'function' && | ||
| proto.constructor instanceof proto.constructor && | ||
| Function.prototype.toString.call(proto.constructor) === objConstructorString | ||
| ); | ||
| } | ||
|
|
||
| /** Recursively serializes any JS value into a deterministic string. */ | ||
| export function deterministicString(input: unknown): string { | ||
| if (typeof input === 'string') { | ||
| return JSON.stringify(input); | ||
| } else if (typeof input === 'symbol' || typeof input === 'function') { | ||
| return input.toString(); | ||
| } else if (typeof input === 'bigint') { | ||
| return `${input}n`; | ||
| } else if ( | ||
| input === globalThis || | ||
| input === undefined || | ||
| input === null || | ||
| typeof input === 'boolean' || | ||
| typeof input === 'number' || | ||
| typeof input !== 'object' | ||
| ) { | ||
| return `${input}`; | ||
| } else if (input instanceof Date) { | ||
| return `(${input.constructor.name}:${input.getTime()})`; | ||
| } else if ( | ||
| input instanceof RegExp || | ||
| input instanceof Error || | ||
| input instanceof WeakMap || | ||
| input instanceof WeakSet | ||
| ) { | ||
| return `(${input.constructor.name}:${input.toString()})`; | ||
| } else if (input instanceof Set) { | ||
| let ret = `(${input.constructor.name}:[`; | ||
| for (const val of input.values()) { | ||
| ret += `${deterministicString(val)},`; | ||
| } | ||
| ret += '])'; | ||
| return ret; | ||
| } else if ( | ||
| Array.isArray(input) || | ||
| input instanceof Int8Array || | ||
| input instanceof Uint8Array || | ||
| input instanceof Uint8ClampedArray || | ||
| input instanceof Int16Array || | ||
| input instanceof Uint16Array || | ||
| input instanceof Int32Array || | ||
| input instanceof Uint32Array || | ||
| input instanceof Float32Array || | ||
| input instanceof Float64Array || | ||
| input instanceof BigInt64Array || | ||
| input instanceof BigUint64Array | ||
| ) { | ||
| let ret = `(${input.constructor.name}:[`; | ||
| for (const [k, v] of input.entries()) { | ||
| ret += `(${k}:${deterministicString(v)}),`; | ||
| } | ||
| ret += '])'; | ||
| return ret; | ||
| } else if (input instanceof ArrayBuffer || input instanceof SharedArrayBuffer) { | ||
| if (input.byteLength % 8 === 0) { | ||
| return deterministicString(new BigUint64Array(input)); | ||
| } else if (input.byteLength % 4 === 0) { | ||
| return deterministicString(new Uint32Array(input)); | ||
| } else if (input.byteLength % 2 === 0) { | ||
| return deterministicString(new Uint16Array(input)); | ||
| } else { | ||
| let ret = '('; | ||
| for (let i = 0; i < input.byteLength; i++) { | ||
| ret += `${deterministicString(new Uint8Array(input.slice(i, i + 1)))},`; | ||
| } | ||
| ret += ')'; | ||
| return ret; | ||
| } | ||
| } else if (input instanceof Map || isPlainObject(input)) { | ||
| const sortable: [string, string][] = []; | ||
| const entries = input instanceof Map ? input.entries() : Object.entries(input); | ||
| for (const [k, v] of entries) { | ||
| sortable.push([deterministicString(k), deterministicString(v)]); | ||
| } | ||
| if (!(input instanceof Map)) { | ||
| const symbolKeys = Object.getOwnPropertySymbols(input); | ||
| // eslint-disable-next-line @typescript-eslint/prefer-for-of | ||
| for (let i = 0; i < symbolKeys.length; i++) { | ||
| sortable.push([ | ||
| deterministicString(symbolKeys[i]!), | ||
| deterministicString((input as Record<symbol, unknown>)[symbolKeys[i]!]), | ||
| ]); | ||
| } | ||
| } | ||
| sortable.sort(([a], [b]) => a.localeCompare(b)); | ||
| let ret = `(${input.constructor.name}:[`; | ||
| for (const [k, v] of sortable) { | ||
| ret += `(${k}:${v}),`; | ||
| } | ||
| ret += '])'; | ||
| return ret; | ||
| } | ||
|
|
||
| const allEntries: [string, string][] = []; | ||
| for (const k in input) { | ||
| allEntries.push([ | ||
| deterministicString(k), | ||
| deterministicString((input as Record<string, unknown>)[k]), | ||
| ]); | ||
| } | ||
| const symbolKeys = Object.getOwnPropertySymbols(input); | ||
| // eslint-disable-next-line @typescript-eslint/prefer-for-of | ||
| for (let i = 0; i < symbolKeys.length; i++) { | ||
| allEntries.push([ | ||
| deterministicString(symbolKeys[i]!), | ||
| deterministicString((input as Record<symbol, unknown>)[symbolKeys[i]!]), | ||
| ]); | ||
| } | ||
| allEntries.sort(([a], [b]) => a.localeCompare(b)); | ||
| let ret = `(${input.constructor.name}:[`; | ||
| for (const [k, v] of allEntries) { | ||
| ret += `(${k}:${v}),`; | ||
| } | ||
| ret += '])'; | ||
| return ret; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { deterministicString } from './deterministic-string.js'; | ||
| import { removeQueryString } from '@astrojs/internal-helpers/path'; | ||
| import { shorthash } from '../../runtime/server/shorthash.js'; | ||
| import type { ImageTransform } from '../types.js'; | ||
| import { isESMImportedImage } from './imageKind.js'; | ||
|
|
||
| // Taken from https://github.com/rollup/rollup/blob/a8647dac0fe46c86183be8596ef7de25bc5b4e4b/src/utils/sanitizeFileName.ts | ||
| // eslint-disable-next-line no-control-regex | ||
| const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$%&*+,:;<=>?[\]^`{|}\u007F]/g; | ||
|
|
||
| /** Pure-string replacement for `path.posix.basename` (no node: import). */ | ||
| function basename(filePath: string, ext?: string): string { | ||
| let end = filePath.length; | ||
| while (end > 0 && filePath[end - 1] === '/') end--; | ||
| const stripped = filePath.slice(0, end); | ||
|
|
||
| const lastSlash = stripped.lastIndexOf('/'); | ||
| const base = lastSlash === -1 ? stripped : stripped.slice(lastSlash + 1); | ||
|
|
||
| if (ext && base.endsWith(ext)) { | ||
| return base.slice(0, base.length - ext.length); | ||
| } | ||
| return base; | ||
| } | ||
|
|
||
| /** Pure-string replacement for `path.posix.dirname` (no node: import). */ | ||
| function dirname(filePath: string): string { | ||
| const lastSlash = filePath.lastIndexOf('/'); | ||
| if (lastSlash === -1) return '.'; | ||
| if (lastSlash === 0) return '/'; | ||
| return filePath.slice(0, lastSlash); | ||
| } | ||
|
|
||
| /** Pure-string replacement for `path.posix.extname` (no node: import). */ | ||
| function extname(filePath: string): string { | ||
| const base = basename(filePath); | ||
| const dotIndex = base.lastIndexOf('.'); | ||
| if (dotIndex <= 0) return ''; | ||
| return base.slice(dotIndex); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a file path and transformation properties into a formatted filename. | ||
| * | ||
| * `<prefixDirname>/<baseFilename>_<hash><outputExtension>` | ||
| */ | ||
| export function propsToFilename(filePath: string, transform: ImageTransform, hash: string): string { | ||
| let filename = decodeURIComponent(removeQueryString(filePath)); | ||
| const ext = extname(filename); | ||
| if (filePath.startsWith('data:')) { | ||
| filename = shorthash(filePath); | ||
| } else { | ||
| filename = basename(filename, ext).replace(INVALID_CHAR_REGEX, '_'); | ||
| } | ||
| const prefixDirname = isESMImportedImage(transform.src) ? dirname(filePath) : ''; | ||
|
|
||
| let outputExt = transform.format ? `.${transform.format}` : ext; | ||
| return `${prefixDirname}/${filename}_${hash}${outputExt}`; | ||
| } | ||
|
|
||
| /** Hashes the subset of transform properties that affect the output image. */ | ||
| export function hashTransform( | ||
| transform: ImageTransform, | ||
| imageService: string, | ||
| propertiesToHash: string[], | ||
| ): string { | ||
| const hashFields = propertiesToHash.reduce( | ||
| (acc, prop) => { | ||
| acc[prop] = transform[prop]; | ||
| return acc; | ||
| }, | ||
| { imageService } as Record<string, unknown>, | ||
| ); | ||
| return shorthash(deterministicString(hashFields)); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.