Skip to content

Commit

Permalink
Avoid dupes of original image
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Nov 7, 2024
1 parent 5ddf701 commit 5c3a130
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ export async function getImage(
};

let originalWidth: number | undefined;
let originalHeight: number | undefined;
let originalFormat: string | undefined;
let originalFilePath: string | undefined;

if (isESMImportedImage(resolvedOptions.src)) {
originalFilePath = resolvedOptions.src.fsPath;
originalWidth = resolvedOptions.src.width;
originalHeight = resolvedOptions.src.height;
originalFormat = resolvedOptions.src.format;
}

// Infer size for remote images if inferSize is true
if (
Expand All @@ -82,13 +92,11 @@ export async function getImage(
resolvedOptions.width ??= result.width;
resolvedOptions.height ??= result.height;
originalWidth = result.width;
originalHeight = result.height;
originalFormat = result.format;
delete resolvedOptions.inferSize; // Delete so it doesn't end up in the attributes
}

const originalFilePath = isESMImportedImage(resolvedOptions.src)
? resolvedOptions.src.fsPath
: undefined; // Only set for ESM imports, where we do have a file path

// Clone the `src` object if it's an ESM import so that we don't refer to any properties of the original object
// Causing our generate step to think the image is used outside of the image optimization pipeline
const clonedSrc = isESMImportedImage(resolvedOptions.src)
Expand Down Expand Up @@ -132,13 +140,23 @@ export async function getImage(
: [];

let imageURL = await service.getURL(validatedOptions, imageConfig);

const matchesOriginal = (transform: ImageTransform) =>
transform.width === originalWidth &&
transform.height === originalHeight &&
transform.format === originalFormat;

let srcSets: SrcSetValue[] = await Promise.all(
srcSetTransforms.map(async (srcSet) => ({
transform: srcSet.transform,
url: await service.getURL(srcSet.transform, imageConfig),
descriptor: srcSet.descriptor,
attributes: srcSet.attributes,
})),
srcSetTransforms.map(async (srcSet) => {
return {
transform: srcSet.transform,
url: matchesOriginal(srcSet.transform)
? imageURL
: await service.getURL(srcSet.transform, imageConfig),
descriptor: srcSet.descriptor,
attributes: srcSet.attributes,
};
}),
);

if (
Expand All @@ -152,12 +170,16 @@ export async function getImage(
propsToHash,
originalFilePath,
);
srcSets = srcSetTransforms.map((srcSet) => ({
transform: srcSet.transform,
url: globalThis.astroAsset.addStaticImage!(srcSet.transform, propsToHash, originalFilePath),
descriptor: srcSet.descriptor,
attributes: srcSet.attributes,
}));
srcSets = srcSetTransforms.map((srcSet) => {
return {
transform: srcSet.transform,
url: matchesOriginal(srcSet.transform)
? imageURL
: globalThis.astroAsset.addStaticImage!(srcSet.transform, propsToHash, originalFilePath),
descriptor: srcSet.descriptor,
attributes: srcSet.attributes,
};
});
}

return {
Expand Down

0 comments on commit 5c3a130

Please sign in to comment.