-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Improving support for third-party hosted image services #3957
Changes from 5 commits
bbf3f19
dcf56e4
7c9ebfd
e336fba
ef6bf69
582ea9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/image': patch | ||
--- | ||
|
||
Improves the `astro dev` experience when using a third-party hosted image service |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import { | |
OutputFormat, | ||
TransformOptions, | ||
} from './types.js'; | ||
import { parseAspectRatio } from './utils.js'; | ||
import { isRemoteImage, parseAspectRatio } from './utils.js'; | ||
|
||
export interface GetImageTransform extends Omit<TransformOptions, 'src'> { | ||
src: string | ImageMetadata | Promise<{ default: ImageMetadata }>; | ||
|
@@ -105,23 +105,32 @@ export async function getImage( | |
loader: ImageService, | ||
transform: GetImageTransform | ||
): Promise<ImageAttributes> { | ||
(globalThis as any).loader = loader; | ||
globalThis.astroImage.loader = loader; | ||
|
||
const resolved = await resolveTransform(transform); | ||
|
||
const attributes = await loader.getImageAttributes(resolved); | ||
|
||
const isDev = globalThis.astroImage.command === 'dev'; | ||
const isLocalImage = !isRemoteImage(resolved.src); | ||
|
||
const _loader = isDev && isLocalImage ? globalThis.astroImage.ssrLoader : loader; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix pt. 2 - This makes sure that we don't build a |
||
|
||
if (!_loader) { | ||
throw new Error('@astrojs/image: loader not found!'); | ||
} | ||
|
||
// For SSR services, build URLs for the injected route | ||
if (isSSRService(loader)) { | ||
const { searchParams } = loader.serializeTransform(resolved); | ||
if (isSSRService(_loader)) { | ||
const { searchParams } = _loader.serializeTransform(resolved); | ||
|
||
// cache all images rendered to HTML | ||
if (globalThis && (globalThis as any).addStaticImage) { | ||
(globalThis as any)?.addStaticImage(resolved); | ||
if (globalThis?.astroImage) { | ||
globalThis.astroImage.addStaticImage(resolved); | ||
} | ||
|
||
const src = | ||
globalThis && (globalThis as any).filenameFormat | ||
? (globalThis as any).filenameFormat(resolved, searchParams) | ||
const src = globalThis?.astroImage | ||
? globalThis.astroImage.filenameFormat(resolved, searchParams) | ||
: `${ROUTE_PATTERN}?${searchParams.toString()}`; | ||
|
||
return { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,18 @@ | |
export type { Image, Picture } from '../components/index.js'; | ||
export * from './index.js'; | ||
|
||
interface ImageIntegration { | ||
loader?: ImageService; | ||
ssrLoader: SSRImageService; | ||
command: 'dev' | 'build'; | ||
addStaticImage: (transform: TransformOptions) => void; | ||
filenameFormat: (transform: TransformOptions, searchParams: URLSearchParams) => string; | ||
} | ||
|
||
declare global { | ||
var astroImage: ImageIntegration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making typescript happy with a |
||
} | ||
|
||
export type InputFormat = | ||
| 'heic' | ||
| 'heif' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix pt. 1 -
getImage()
in dev will now only build a_image?
src for local images. The endpoint can always use thessrLoader
global (currently defaults to the built-in sharp service)