Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/astro/src/content/mutable-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 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 contentAfter = await fs.readFile(assetsFilePath, 'utf-8');
assert.ok(
!contentAfter.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 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 contentAfter = await fs.readFile(assetsFilePath, 'utf-8');
assert.ok(
!contentAfter.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);
Expand Down
Loading