diff --git a/.changeset/purple-ears-sneeze.md b/.changeset/purple-ears-sneeze.md new file mode 100644 index 0000000000..b79cc2e68b --- /dev/null +++ b/.changeset/purple-ears-sneeze.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +perf(assets): avoid downloading original image when using cache diff --git a/packages/astro/src/assets/build/generate.ts b/packages/astro/src/assets/build/generate.ts index fcc19f4f53..645a0acdcb 100644 --- a/packages/astro/src/assets/build/generate.ts +++ b/packages/astro/src/assets/build/generate.ts @@ -98,11 +98,11 @@ export async function generateImagesForPath( env: AssetEnv, queue: PQueue, ) { - const originalImageData = await loadImage(originalFilePath, env); + let originalImage: ImageData; for (const [_, transform] of transformsAndPath.transforms) { await queue - .add(async () => generateImage(originalImageData, transform.finalPath, transform.transform)) + .add(async () => generateImage(transform.finalPath, transform.transform)) .catch((e) => { throw e; }); @@ -128,13 +128,9 @@ export async function generateImagesForPath( } } - async function generateImage( - originalImage: ImageData, - filepath: string, - options: ImageTransform, - ) { + async function generateImage(filepath: string, options: ImageTransform) { const timeStart = performance.now(); - const generationData = await generateImageInternal(originalImage, filepath, options); + const generationData = await generateImageInternal(filepath, options); const timeEnd = performance.now(); const timeChange = getTimeStat(timeStart, timeEnd); @@ -151,7 +147,6 @@ export async function generateImagesForPath( } async function generateImageInternal( - originalImage: ImageData, filepath: string, options: ImageTransform, ): Promise { @@ -207,6 +202,10 @@ export async function generateImagesForPath( ? (options.src as ImageMetadata).src : (options.src as string); + if (!originalImage) { + originalImage = await loadImage(originalFilePath, env); + } + let resultData: Partial = { data: undefined, expires: originalImage.expires,