From 58e71c8549507fcf2c734151b1fd522d9ec7bc27 Mon Sep 17 00:00:00 2001 From: dataCenter430 Date: Fri, 27 Mar 2026 15:17:35 +0100 Subject: [PATCH 1/2] fix(content): clear stale asset imports on content collection entry update --- .../astro/src/content/mutable-data-store.ts | 27 +++++ .../mutable-data-store.test.js | 101 ++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/packages/astro/src/content/mutable-data-store.ts b/packages/astro/src/content/mutable-data-store.ts index 794a35ce7a62..9459645c29a8 100644 --- a/packages/astro/src/content/mutable-data-store.ts +++ b/packages/astro/src/content/mutable-data-store.ts @@ -50,17 +50,20 @@ export class MutableDataStore extends ImmutableDataStore { if (collection) { collection.delete(String(key)); this.#saveToDiskDebounced(); + this.#writeAssetsImportsDebounced(); } } clear(collectionName: string) { this._collections.delete(collectionName); this.#saveToDiskDebounced(); + this.#writeAssetsImportsDebounced(); } clearAll() { this._collections.clear(); this.#saveToDiskDebounced(); + this.#writeAssetsImportsDebounced(); } addAssetImport(assetImport: string, filePath?: string) { @@ -89,8 +92,32 @@ export class MutableDataStore extends ImmutableDataStore { } } + /** + * Rebuilds #assetImports from the current entries in _collections. + * This ensures stale import IDs are removed when entries are updated or deleted, + * preventing unrecoverable ImageNotFound errors in astro dev after a content entry's + * image path is temporarily set to an invalid value and then restored. + */ + #rebuildAssetImports() { + this.#assetImports.clear(); + for (const collection of this._collections.values()) { + for (const entry of collection.values()) { + const typedEntry = entry as DataEntry; + if (typedEntry.assetImports?.length) { + for (const assetImport of typedEntry.assetImports) { + const id = imageSrcToImportId(assetImport, typedEntry.filePath); + if (id) { + this.#assetImports.add(id); + } + } + } + } + } + } + async writeAssetImports(filePath: PathLike) { this.#assetsFile = filePath; + this.#rebuildAssetImports(); if (this.#assetImports.size === 0) { try { diff --git a/packages/astro/test/units/content-collections/mutable-data-store.test.js b/packages/astro/test/units/content-collections/mutable-data-store.test.js index 692a74852600..9e6488c4339e 100644 --- a/packages/astro/test/units/content-collections/mutable-data-store.test.js +++ b/packages/astro/test/units/content-collections/mutable-data-store.test.js @@ -7,6 +7,7 @@ import path from 'node:path'; import { pathToFileURL } from 'node:url'; import * as devalue from 'devalue'; import { MutableDataStore } from '../../../dist/content/mutable-data-store.js'; +import { imageSrcToImportId } from '../../../dist/assets/utils/resolveImports.js'; describe('MutableDataStore', () => { let tmpDir; @@ -23,6 +24,106 @@ describe('MutableDataStore', () => { } }); + it('removes stale image asset import after entry image path is updated (issue #16097)', async () => { + const assetsFilePath = path.join(tmpDir, 'content-assets.mjs'); + const entryFilePath = 'src/content/categories/example.json'; + const store = new MutableDataStore(); + const scoped = store.scopedStore('categories'); + + scoped.set({ + id: 'example', + data: {}, + filePath: entryFilePath, + assetImports: ['./images/seed.webp'], + }); + + scoped.set({ + id: 'example', + data: {}, + filePath: entryFilePath, + assetImports: ['./images/non-existing.jpg'], + }); + + scoped.set({ + id: 'example', + data: {}, + filePath: entryFilePath, + assetImports: ['./images/seed.webp'], + }); + + await store.writeAssetImports(assetsFilePath); + + const content = await fs.readFile(assetsFilePath, 'utf-8'); + + const validId = imageSrcToImportId('./images/seed.webp', entryFilePath); + const staleId = imageSrcToImportId('./images/non-existing.jpg', entryFilePath); + + assert.ok( + content.includes(validId), + `content-assets.mjs should reference the valid image import "${validId}"`, + ); + assert.ok( + !content.includes('non-existing.jpg'), + `content-assets.mjs must NOT reference the stale invalid import "${staleId}" after the path is restored`, + ); + }); + + it('removes asset imports when an entry is deleted', async () => { + const assetsFilePath = path.join(tmpDir, 'content-assets-delete.mjs'); + const entryFilePath = 'src/content/categories/deleted.json'; + const store = new MutableDataStore(); + const scoped = store.scopedStore('categories'); + + scoped.set({ + id: 'deleted-entry', + data: {}, + filePath: entryFilePath, + assetImports: ['./images/to-be-removed.webp'], + }); + + await store.writeAssetImports(assetsFilePath); + const before = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok(before.includes('to-be-removed.webp'), 'should contain the image before deletion'); + + scoped.delete('deleted-entry'); + await store.writeAssetImports(assetsFilePath); + await store.waitUntilSaveComplete(); + + const after = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok( + !after.includes('to-be-removed.webp'), + 'should NOT contain the image after the entry is deleted', + ); + }); + + it('removes asset imports when a collection is cleared', async () => { + const assetsFilePath = path.join(tmpDir, 'content-assets-clear.mjs'); + const entryFilePath = 'src/content/blog/post.json'; + const store = new MutableDataStore(); + const scoped = store.scopedStore('blog'); + + scoped.set({ + id: 'post-1', + data: {}, + filePath: entryFilePath, + assetImports: ['./images/cover.webp'], + }); + + await store.writeAssetImports(assetsFilePath); + const before = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok(before.includes('cover.webp'), 'should contain the image before clear'); + + scoped.clear(); + await store.writeAssetImports(assetsFilePath); + await store.waitUntilSaveComplete(); + + const after = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok( + !after.includes('cover.webp'), + 'should NOT contain the image after the collection is cleared', + ); + }); + it('reproduces race condition: concurrent writeToDisk() calls lose data', async () => { const filePath = pathToFileURL(path.join(tmpDir, 'data-store.json')); const store = await MutableDataStore.fromFile(filePath); From 892d9a27d56f0ee4029e879cbb949e87b5914dd9 Mon Sep 17 00:00:00 2001 From: dataCenter430 Date: Fri, 27 Mar 2026 15:26:54 +0100 Subject: [PATCH 2/2] fix lint errors --- .../mutable-data-store.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/astro/test/units/content-collections/mutable-data-store.test.js b/packages/astro/test/units/content-collections/mutable-data-store.test.js index 9e6488c4339e..8a8025e4fe3a 100644 --- a/packages/astro/test/units/content-collections/mutable-data-store.test.js +++ b/packages/astro/test/units/content-collections/mutable-data-store.test.js @@ -82,16 +82,16 @@ describe('MutableDataStore', () => { }); await store.writeAssetImports(assetsFilePath); - const before = await fs.readFile(assetsFilePath, 'utf-8'); - assert.ok(before.includes('to-be-removed.webp'), 'should contain the image before deletion'); + const contentBefore = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok(contentBefore.includes('to-be-removed.webp'), 'should contain the image before deletion'); scoped.delete('deleted-entry'); await store.writeAssetImports(assetsFilePath); await store.waitUntilSaveComplete(); - const after = await fs.readFile(assetsFilePath, 'utf-8'); + const contentAfter = await fs.readFile(assetsFilePath, 'utf-8'); assert.ok( - !after.includes('to-be-removed.webp'), + !contentAfter.includes('to-be-removed.webp'), 'should NOT contain the image after the entry is deleted', ); }); @@ -110,16 +110,16 @@ describe('MutableDataStore', () => { }); await store.writeAssetImports(assetsFilePath); - const before = await fs.readFile(assetsFilePath, 'utf-8'); - assert.ok(before.includes('cover.webp'), 'should contain the image before clear'); + const contentBefore = await fs.readFile(assetsFilePath, 'utf-8'); + assert.ok(contentBefore.includes('cover.webp'), 'should contain the image before clear'); scoped.clear(); await store.writeAssetImports(assetsFilePath); await store.waitUntilSaveComplete(); - const after = await fs.readFile(assetsFilePath, 'utf-8'); + const contentAfter = await fs.readFile(assetsFilePath, 'utf-8'); assert.ok( - !after.includes('cover.webp'), + !contentAfter.includes('cover.webp'), 'should NOT contain the image after the collection is cleared', ); });