Skip to content

Commit

Permalink
fix: content layer glob deletion (#12476)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre authored Nov 19, 2024
1 parent f649340 commit 80a9a52
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-pianos-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where the Content Layer `glob()` loader would not update when renaming or deleting an entry
4 changes: 2 additions & 2 deletions packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function glob(globOptions: GlobOptions): Loader {
const existingEntry = store.get(id);

const digest = generateDigest(contents);
const filePath = fileURLToPath(fileUrl);

if (existingEntry && existingEntry.digest === digest && existingEntry.filePath) {
if (existingEntry.deferredRender) {
Expand All @@ -112,11 +113,10 @@ export function glob(globOptions: GlobOptions): Loader {
store.addAssetImports(existingEntry.assetImports, existingEntry.filePath);
}

fileToIdMap.set(filePath, id);
return;
}

const filePath = fileURLToPath(fileUrl);

const relativePath = posixRelative(fileURLToPath(config.root), filePath);

const parsedData = await parseData({
Expand Down
21 changes: 21 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,26 @@ describe('Content Layer', () => {
assert.equal(res.status, 500);
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);
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export async function GET() {
const increment = await getEntry('increment', 'value');

const images = await getCollection('images');

const numbers = await getCollection('numbers');

return new Response(
devalue.stringify({
customLoader,
Expand All @@ -29,7 +32,8 @@ export async function GET() {
entryWithImagePath,
referencedEntry,
increment,
images
images,
numbers,
})
);
}

0 comments on commit 80a9a52

Please sign in to comment.