From f87148e55d44813b12c557589e1b220c63ce59b1 Mon Sep 17 00:00:00 2001 From: Gabriel Pereira Woitechen Date: Mon, 2 Sep 2024 15:11:39 -0300 Subject: [PATCH] perf(astro/assets): avoid downloading original image when using cache --- .changeset/purple-ears-sneeze.md | 5 +++++ packages/astro/src/assets/build/generate.ts | 14 ++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) create mode 100644 .changeset/purple-ears-sneeze.md diff --git a/.changeset/purple-ears-sneeze.md b/.changeset/purple-ears-sneeze.md new file mode 100644 index 0000000000000..b79cc2e68b0d9 --- /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 fcc19f4f53a98..1573dfbcb9f6c 100644 --- a/packages/astro/src/assets/build/generate.ts +++ b/packages/astro/src/assets/build/generate.ts @@ -98,11 +98,9 @@ export async function generateImagesForPath( env: AssetEnv, queue: PQueue, ) { - const originalImageData = await loadImage(originalFilePath, env); - 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 +126,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 +145,6 @@ export async function generateImagesForPath( } async function generateImageInternal( - originalImage: ImageData, filepath: string, options: ImageTransform, ): Promise { @@ -207,6 +200,7 @@ export async function generateImagesForPath( ? (options.src as ImageMetadata).src : (options.src as string); + const originalImage = await loadImage(originalFilePath, env); let resultData: Partial = { data: undefined, expires: originalImage.expires,