Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: content layer glob deletion #12476

Merged
merged 2 commits into from
Nov 19, 2024
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
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,
})
);
}
Loading