summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2025-01-02 12:16:57 +0000
committerGravatar GitHub <noreply@github.com> 2025-01-02 12:16:57 +0000
commite109002c3d5980362788360211e61f11f4394837 (patch)
tree36d68ec45e61e9bb2fbdccf1f3663d0b3ee49074
parentac00c64ee769e0d54eb090c78eba2ce13cdb638f (diff)
downloadastro-e109002c3d5980362788360211e61f11f4394837.tar.gz
astro-e109002c3d5980362788360211e61f11f4394837.tar.zst
astro-e109002c3d5980362788360211e61f11f4394837.zip
fix: pass emulated entry to getCollection filter function (#12875)
* fix: pass emulated entry to getCollection filter function * Add test
-rw-r--r--.changeset/rotten-baboons-roll.md5
-rw-r--r--packages/astro/src/content/runtime.ts18
-rw-r--r--packages/astro/test/fixtures/legacy-content-collections/src/pages/collections.json.js11
-rw-r--r--packages/astro/test/legacy-content-collections.test.js6
4 files changed, 32 insertions, 8 deletions
diff --git a/.changeset/rotten-baboons-roll.md b/.changeset/rotten-baboons-roll.md
new file mode 100644
index 000000000..e7e142259
--- /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 ea1527106..aa6b63be3 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<DataEntry>(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 67bafdb93..16350c5ad 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 613d76e1e..e4327ceb3 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);