Fix issue #515: Image optimization crash
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { getImage } from 'astro:assets';
|
||||
import { isUnpicCompatible, unpicOptimizer, astroAsseetsOptimizer } from './images-optimization';
|
||||
import type { ImageMetadata } from 'astro';
|
||||
import type { OpenGraph } from '@astrolib/seo';
|
||||
|
||||
@@ -64,23 +64,32 @@ export const adaptOpenGraphImages = async (
|
||||
const adaptedImages = await Promise.all(
|
||||
images.map(async (image) => {
|
||||
if (image?.url) {
|
||||
const resolvedImage = (await findImage(image.url)) as ImageMetadata | undefined;
|
||||
const resolvedImage = (await findImage(image.url)) as ImageMetadata | string | undefined;
|
||||
if (!resolvedImage) {
|
||||
return {
|
||||
url: '',
|
||||
};
|
||||
}
|
||||
|
||||
const _image = await getImage({
|
||||
src: resolvedImage,
|
||||
alt: 'Placeholder alt',
|
||||
width: image?.width || defaultWidth,
|
||||
height: image?.height || defaultHeight,
|
||||
});
|
||||
let _image;
|
||||
|
||||
if (
|
||||
typeof resolvedImage === 'string' &&
|
||||
(resolvedImage.startsWith('http://') || resolvedImage.startsWith('https://')) &&
|
||||
isUnpicCompatible(resolvedImage)
|
||||
) {
|
||||
_image = (await unpicOptimizer(resolvedImage, [defaultWidth], defaultWidth, defaultHeight, 'jpg'))[0];
|
||||
} else if (resolvedImage) {
|
||||
const dimensions =
|
||||
typeof resolvedImage !== 'string' && resolvedImage?.width <= defaultWidth
|
||||
? [resolvedImage?.width, resolvedImage?.height]
|
||||
: [defaultWidth, defaultHeight];
|
||||
_image = (await astroAsseetsOptimizer(resolvedImage, [dimensions[0]], dimensions[0], dimensions[1], 'jpg'))[0];
|
||||
}
|
||||
|
||||
if (typeof _image === 'object') {
|
||||
return {
|
||||
url: 'src' in _image && typeof _image.src === 'string' ? String(new URL(_image.src, astroSite)) : 'pepe',
|
||||
url: 'src' in _image && typeof _image.src === 'string' ? String(new URL(_image.src, astroSite)) : '',
|
||||
width: 'width' in _image && typeof _image.width === 'number' ? _image.width : undefined,
|
||||
height: 'height' in _image && typeof _image.height === 'number' ? _image.height : undefined,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user