From 25baa4ed0c5f55fa85c2c7e2c15848937ed1dc9b Mon Sep 17 00:00:00 2001 From: oliverlynch <59594611+oliverlynch@users.noreply.github.com> Date: Sat, 16 Nov 2024 01:25:42 +0800 Subject: [PATCH] Ensure final asset directory exists before writing cached files (#12418) Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> --- .changeset/thick-shrimps-hammer.md | 5 +++++ packages/astro/src/assets/build/generate.ts | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 .changeset/thick-shrimps-hammer.md diff --git a/.changeset/thick-shrimps-hammer.md b/.changeset/thick-shrimps-hammer.md new file mode 100644 index 0000000000..b46b9ad6c2 --- /dev/null +++ b/.changeset/thick-shrimps-hammer.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix cached image redownloading if it is the first asset diff --git a/packages/astro/src/assets/build/generate.ts b/packages/astro/src/assets/build/generate.ts index 645a0acdcb..da76cafa38 100644 --- a/packages/astro/src/assets/build/generate.ts +++ b/packages/astro/src/assets/build/generate.ts @@ -153,6 +153,9 @@ export async function generateImagesForPath( const isLocalImage = isESMImportedImage(options.src); const finalFileURL = new URL('.' + filepath, env.clientRoot); + const finalFolderURL = new URL('./', finalFileURL); + await fs.promises.mkdir(finalFolderURL, { recursive: true }); + // For remote images, instead of saving the image directly, we save a JSON file with the image data and expiration date from the server const cacheFile = basename(filepath) + (isLocalImage ? '' : '.json'); const cachedFileURL = new URL(cacheFile, env.assetsCacheDir); @@ -194,9 +197,6 @@ export async function generateImagesForPath( // If the cache file doesn't exist, just move on, and we'll generate it } - const finalFolderURL = new URL('./', finalFileURL); - await fs.promises.mkdir(finalFolderURL, { recursive: true }); - // The original filepath or URL from the image transform const originalImagePath = isLocalImage ? (options.src as ImageMetadata).src