diff options
author | 2024-11-19 16:56:28 +0100 | |
---|---|---|
committer | 2024-11-19 16:56:28 +0100 | |
commit | 80a9a5299a9d51f2b09900d3200976d687feae8f (patch) | |
tree | fdac67fa8eb70da343e0e9461bd5a748efbb7e9b | |
parent | f64934086e84966bcfd02b699d64a80cb8392fa2 (diff) | |
download | astro-80a9a5299a9d51f2b09900d3200976d687feae8f.tar.gz astro-80a9a5299a9d51f2b09900d3200976d687feae8f.tar.zst astro-80a9a5299a9d51f2b09900d3200976d687feae8f.zip |
fix: content layer glob deletion (#12476)
-rw-r--r-- | .changeset/six-pianos-draw.md | 5 | ||||
-rw-r--r-- | packages/astro/src/content/loaders/glob.ts | 4 | ||||
-rw-r--r-- | packages/astro/test/content-layer.test.js | 21 | ||||
-rw-r--r-- | packages/astro/test/fixtures/content-layer/src/pages/collections.json.js | 6 |
4 files changed, 33 insertions, 3 deletions
diff --git a/.changeset/six-pianos-draw.md b/.changeset/six-pianos-draw.md new file mode 100644 index 000000000..61a6d07eb --- /dev/null +++ b/.changeset/six-pianos-draw.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a case where the Content Layer `glob()` loader would not update when renaming or deleting an entry diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index e0b1d79c1..4a515a92b 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -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) { @@ -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({ diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index d8bd7bd2d..1f2ceb7d8 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -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); + } + }); }); }); diff --git a/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js b/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js index 5bce6ed54..d218613fa 100644 --- a/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js +++ b/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js @@ -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, @@ -29,7 +32,8 @@ export async function GET() { entryWithImagePath, referencedEntry, increment, - images + images, + numbers, }) ); } |