From e109002c3d5980362788360211e61f11f4394837 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 2 Jan 2025 12:16:57 +0000 Subject: [PATCH] fix: pass emulated entry to getCollection filter function (#12875) * fix: pass emulated entry to getCollection filter function * Add test --- .changeset/rotten-baboons-roll.md | 5 +++++ packages/astro/src/content/runtime.ts | 18 +++++++++++------- .../src/pages/collections.json.js | 11 ++++++++++- .../test/legacy-content-collections.test.js | 6 ++++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 .changeset/rotten-baboons-roll.md diff --git a/.changeset/rotten-baboons-roll.md b/.changeset/rotten-baboons-roll.md new file mode 100644 index 0000000000..e7e1422597 --- /dev/null +++ b/.changeset/rotten-baboons-roll.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug in emulated legacy collections where the entry passed to the getCollection filter function did not include the legacy entry fields. diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index ea1527106c..aa6b63be3e 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -96,15 +96,20 @@ export function createGetCollection({ for (const rawEntry of store.values(collection)) { const data = updateImageReferencesInData(rawEntry.data, rawEntry.filePath, imageAssetMap); - const entry = { + let entry = { ...rawEntry, data, collection, }; + + if (entry.legacyId) { + entry = emulateLegacyEntry(entry); + } + if (hasFilter && !filter(entry)) { continue; } - result.push(entry.legacyId ? emulateLegacyEntry(entry) : entry); + result.push(entry); } return result; } else { @@ -272,19 +277,18 @@ type DataEntryResult = { type EntryLookupObject = { collection: string; id: string } | { collection: string; slug: string }; -function emulateLegacyEntry(entry: DataEntry) { +function emulateLegacyEntry({ legacyId, ...entry }: DataEntry & { collection: string }) { // Define this first so it's in scope for the render function const legacyEntry = { ...entry, - id: entry.legacyId!, + id: legacyId!, slug: entry.id, }; - delete legacyEntry.legacyId; return { ...legacyEntry, // Define separately so the render function isn't included in the object passed to `renderEntry()` render: () => renderEntry(legacyEntry), - }; + } as ContentEntryResult; } export function createGetEntry({ @@ -334,7 +338,7 @@ export function createGetEntry({ const { default: imageAssetMap } = await import('astro:asset-imports'); entry.data = updateImageReferencesInData(entry.data, entry.filePath, imageAssetMap); if (entry.legacyId) { - return { ...emulateLegacyEntry(entry), collection } as ContentEntryResult; + return emulateLegacyEntry({ ...entry, collection }); } return { ...entry, diff --git a/packages/astro/test/fixtures/legacy-content-collections/src/pages/collections.json.js b/packages/astro/test/fixtures/legacy-content-collections/src/pages/collections.json.js index 67bafdb93e..16350c5ad3 100644 --- a/packages/astro/test/fixtures/legacy-content-collections/src/pages/collections.json.js +++ b/packages/astro/test/fixtures/legacy-content-collections/src/pages/collections.json.js @@ -9,8 +9,17 @@ export async function GET() { const withUnionSchema = stripAllRenderFn(await getCollection('with-union-schema')); const withSymlinkedContent = stripAllRenderFn(await getCollection('with-symlinked-content')); const withSymlinkedData = stripAllRenderFn(await getCollection('with-symlinked-data')); + const filtered = stripAllRenderFn(await getCollection('without-config', (entry) => entry.slug)); return new Response( - devalue.stringify({ withoutConfig, withSchemaConfig, withSlugConfig, withUnionSchema, withSymlinkedContent, withSymlinkedData }), + devalue.stringify({ + withoutConfig, + withSchemaConfig, + withSlugConfig, + withUnionSchema, + withSymlinkedContent, + withSymlinkedData, + filtered, + }), ); } diff --git a/packages/astro/test/legacy-content-collections.test.js b/packages/astro/test/legacy-content-collections.test.js index 613d76e1ec..e4327ceb3d 100644 --- a/packages/astro/test/legacy-content-collections.test.js +++ b/packages/astro/test/legacy-content-collections.test.js @@ -55,6 +55,12 @@ describe('Legacy Content Collections', () => { ); }); + it('Passes legacy entry to filter function', async () => { + assert.ok(json.hasOwnProperty('filtered')); + assert.ok(Array.isArray(json.filtered)); + assert.ok(json.filtered.length > 0); + }); + it('Returns `with schema` collection', async () => { assert.ok(json.hasOwnProperty('withSchemaConfig')); assert.equal(Array.isArray(json.withSchemaConfig), true);