0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: content layer glob deletion (#12476)

This commit is contained in:
Florian Lefebvre 2024-11-19 16:56:28 +01:00 committed by GitHub
parent f64934086e
commit 80a9a5299a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a case where the Content Layer `glob()` loader would not update when renaming or deleting an entry

View file

@ -101,6 +101,7 @@ export function glob(globOptions: GlobOptions): Loader {
const existingEntry = store.get(id); const existingEntry = store.get(id);
const digest = generateDigest(contents); const digest = generateDigest(contents);
const filePath = fileURLToPath(fileUrl);
if (existingEntry && existingEntry.digest === digest && existingEntry.filePath) { if (existingEntry && existingEntry.digest === digest && existingEntry.filePath) {
if (existingEntry.deferredRender) { if (existingEntry.deferredRender) {
@ -112,11 +113,10 @@ export function glob(globOptions: GlobOptions): Loader {
store.addAssetImports(existingEntry.assetImports, existingEntry.filePath); store.addAssetImports(existingEntry.assetImports, existingEntry.filePath);
} }
fileToIdMap.set(filePath, id);
return; return;
} }
const filePath = fileURLToPath(fileUrl);
const relativePath = posixRelative(fileURLToPath(config.root), filePath); const relativePath = posixRelative(fileURLToPath(config.root), filePath);
const parsedData = await parseData({ const parsedData = await parseData({

View file

@ -323,5 +323,26 @@ describe('Content Layer', () => {
assert.equal(res.status, 500); assert.equal(res.status, 500);
assert.ok(text.includes('RenderUndefinedEntryError')); assert.ok(text.includes('RenderUndefinedEntryError'));
}); });
it('update the store when a file is renamed', async () => {
const rawJsonResponse = await fixture.fetch('/collections.json');
const initialJson = devalue.parse(await rawJsonResponse.text());
assert.equal(initialJson.numbers.map((e) => e.id).includes('src/data/glob-data/three'), true);
const oldPath = new URL('./data/glob-data/three.json', fixture.config.srcDir);
const newPath = new URL('./data/glob-data/four.json', fixture.config.srcDir);
await fs.rename(oldPath, newPath);
await fixture.onNextDataStoreChange();
try {
const updatedJsonResponse = await fixture.fetch('/collections.json');
const updated = devalue.parse(await updatedJsonResponse.text());
assert.equal(updated.numbers.map((e) => e.id).includes('src/data/glob-data/three'), false);
assert.equal(updated.numbers.map((e) => e.id).includes('src/data/glob-data/four'), true);
} finally {
await fs.rename(newPath, oldPath);
}
});
}); });
}); });

View file

@ -19,6 +19,9 @@ export async function GET() {
const increment = await getEntry('increment', 'value'); const increment = await getEntry('increment', 'value');
const images = await getCollection('images'); const images = await getCollection('images');
const numbers = await getCollection('numbers');
return new Response( return new Response(
devalue.stringify({ devalue.stringify({
customLoader, customLoader,
@ -29,7 +32,8 @@ export async function GET() {
entryWithImagePath, entryWithImagePath,
referencedEntry, referencedEntry,
increment, increment,
images images,
numbers,
}) })
); );
} }