Skip to content

Commit eebebeb

Browse files
committed
Fix issue #515: Image optimization crash
1 parent c8bc46d commit eebebeb

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@onwidget/astrowind",
3-
"version": "1.0.0-beta.47",
3+
"version": "1.0.0-beta.48",
44
"description": "AstroWind: A free template using Astro 4.0 and Tailwind CSS. Astro starter theme.",
55
"type": "module",
66
"private": true,

src/utils/images-optimization.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ export interface ImageProps extends Omit<HTMLAttributes<'img'>, 'src'> {
2222
widths?: number[] | null;
2323
aspectRatio?: string | number | null;
2424
objectPosition?: string;
25+
26+
format?: string;
2527
}
2628

2729
export type ImagesOptimizer = (
2830
image: ImageMetadata | string,
2931
breakpoints: number[],
3032
width?: number,
31-
height?: number
33+
height?: number,
34+
format?: string
3235
) => Promise<Array<{ src: string; width: number }>>;
3336

3437
/* ******* */
@@ -209,17 +212,25 @@ const getBreakpoints = ({
209212
};
210213

211214
/* ** */
212-
export const astroAsseetsOptimizer: ImagesOptimizer = async (image, breakpoints, _width, _height) => {
215+
export const astroAsseetsOptimizer: ImagesOptimizer = async (
216+
image,
217+
breakpoints,
218+
_width,
219+
_height,
220+
format = undefined
221+
) => {
213222
if (!image) {
214223
return [];
215224
}
216225

217226
return Promise.all(
218227
breakpoints.map(async (w: number) => {
219-
const url = (await getImage({ src: image, width: w, inferSize: true })).src;
228+
const result = (await getImage({ src: image, width: w, inferSize: true, ...(format ? { format: format } : {}) }));
229+
220230
return {
221-
src: url,
222-
width: w,
231+
src: result?.src,
232+
width: result?.attributes?.width ?? w,
233+
height: result?.attributes?.height
223234
};
224235
})
225236
);
@@ -230,7 +241,7 @@ export const isUnpicCompatible = (image: string) => {
230241
};
231242

232243
/* ** */
233-
export const unpicOptimizer: ImagesOptimizer = async (image, breakpoints, width, height) => {
244+
export const unpicOptimizer: ImagesOptimizer = async (image, breakpoints, width, height, format = undefined) => {
234245
if (!image || typeof image !== 'string') {
235246
return [];
236247
}
@@ -242,16 +253,19 @@ export const unpicOptimizer: ImagesOptimizer = async (image, breakpoints, width,
242253

243254
return Promise.all(
244255
breakpoints.map(async (w: number) => {
256+
const _height = width && height ? computeHeight(w, width / height) : height;
245257
const url =
246258
transformUrl({
247259
url: image,
248260
width: w,
249-
height: width && height ? computeHeight(w, width / height) : height,
261+
height: _height,
250262
cdn: urlParsed.cdn,
263+
...(format ? { format: format } : {}),
251264
}) || image;
252265
return {
253266
src: String(url),
254267
width: w,
268+
height: _height,
255269
};
256270
})
257271
);
@@ -270,6 +284,7 @@ export async function getImagesOptimized(
270284
widths,
271285
layout = 'constrained',
272286
style = '',
287+
format,
273288
...rest
274289
}: ImageProps,
275290
transform: ImagesOptimizer = () => Promise.resolve([])
@@ -312,7 +327,7 @@ export async function getImagesOptimized(
312327
let breakpoints = getBreakpoints({ width: width, breakpoints: widths, layout: layout });
313328
breakpoints = [...new Set(breakpoints)].sort((a, b) => a - b);
314329

315-
const srcset = (await transform(image, breakpoints, Number(width) || undefined, Number(height) || undefined))
330+
const srcset = (await transform(image, breakpoints, Number(width) || undefined, Number(height) || undefined, format))
316331
.map(({ src, width }) => `${src} ${width}w`)
317332
.join(', ');
318333

src/utils/images.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getImage } from 'astro:assets';
1+
import { isUnpicCompatible, unpicOptimizer, astroAsseetsOptimizer } from './images-optimization';
22
import type { ImageMetadata } from 'astro';
33
import type { OpenGraph } from '@astrolib/seo';
44

@@ -64,23 +64,32 @@ export const adaptOpenGraphImages = async (
6464
const adaptedImages = await Promise.all(
6565
images.map(async (image) => {
6666
if (image?.url) {
67-
const resolvedImage = (await findImage(image.url)) as ImageMetadata | undefined;
67+
const resolvedImage = (await findImage(image.url)) as ImageMetadata | string | undefined;
6868
if (!resolvedImage) {
6969
return {
7070
url: '',
7171
};
7272
}
7373

74-
const _image = await getImage({
75-
src: resolvedImage,
76-
alt: 'Placeholder alt',
77-
width: image?.width || defaultWidth,
78-
height: image?.height || defaultHeight,
79-
});
74+
let _image;
75+
76+
if (
77+
typeof resolvedImage === 'string' &&
78+
(resolvedImage.startsWith('http://') || resolvedImage.startsWith('https://')) &&
79+
isUnpicCompatible(resolvedImage)
80+
) {
81+
_image = (await unpicOptimizer(resolvedImage, [defaultWidth], defaultWidth, defaultHeight, 'jpg'))[0];
82+
} else if (resolvedImage) {
83+
const dimensions =
84+
typeof resolvedImage !== 'string' && resolvedImage?.width <= defaultWidth
85+
? [resolvedImage?.width, resolvedImage?.height]
86+
: [defaultWidth, defaultHeight];
87+
_image = (await astroAsseetsOptimizer(resolvedImage, [dimensions[0]], dimensions[0], dimensions[1], 'jpg'))[0];
88+
}
8089

8190
if (typeof _image === 'object') {
8291
return {
83-
url: 'src' in _image && typeof _image.src === 'string' ? String(new URL(_image.src, astroSite)) : 'pepe',
92+
url: 'src' in _image && typeof _image.src === 'string' ? String(new URL(_image.src, astroSite)) : '',
8493
width: 'width' in _image && typeof _image.width === 'number' ? _image.width : undefined,
8594
height: 'height' in _image && typeof _image.height === 'number' ? _image.height : undefined,
8695
};

0 commit comments

Comments
 (0)