Skip to content

Commit

Permalink
Ensure final asset directory exists before writing cached files (#12418)
Browse files Browse the repository at this point in the history
Co-authored-by: Erika <[email protected]>
  • Loading branch information
oliverlynch and Princesseuh authored Nov 15, 2024
1 parent cec4af8 commit 25baa4e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-shrimps-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix cached image redownloading if it is the first asset
6 changes: 3 additions & 3 deletions packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 25baa4e

Please sign in to comment.