From e87f5866c125d88e832e4fb37a6618f457bd767c Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 19 Dec 2024 09:35:03 +0000 Subject: [PATCH 1/3] fix: make `reference()` a sync transform --- .changeset/fluffy-ants-reflect.md | 5 +++++ packages/astro/src/content/runtime.ts | 18 +++++++++++++++--- .../content-layer/src/content.config.ts | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/fluffy-ants-reflect.md diff --git a/.changeset/fluffy-ants-reflect.md b/.changeset/fluffy-ants-reflect.md new file mode 100644 index 000000000000..924c7991049c --- /dev/null +++ b/.changeset/fluffy-ants-reflect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a regression that caused `default()` to not work with `reference()` diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 81590da8b325..dc48b321f97d 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -18,7 +18,7 @@ import { unescapeHTML, } from '../runtime/server/index.js'; import { CONTENT_LAYER_TYPE, IMAGE_IMPORT_PREFIX } from './consts.js'; -import { type DataEntry, globalDataStore } from './data-store.js'; +import { type DataEntry, globalDataStore, ImmutableDataStore } from './data-store.js'; import type { ContentLookupMap } from './utils.js'; type LazyImport = () => Promise; @@ -604,6 +604,10 @@ async function render({ } export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) { + // We're handling it like this to avoid needing an async schema. Not ideal, but should + // be safe because the store will already have been loaded by the time this is called. + let store: ImmutableDataStore | null = null; + globalDataStore.get().then((s) => (store = s)); return function reference(collection: string) { return z .union([ @@ -618,15 +622,23 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) }), ]) .transform( - async ( + ( lookup: | string | { id: string; collection: string } | { slug: string; collection: string }, ctx, ) => { + if(!store) { + ctx.addIssue({ + code: ZodIssueCode.custom, + message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved. Store not available.`, + }); + return + } + + const flattenedErrorPath = ctx.path.join('.'); - const store = await globalDataStore.get(); const collectionIsInStore = store.hasCollection(collection); if (typeof lookup === 'object') { diff --git a/packages/astro/test/fixtures/content-layer/src/content.config.ts b/packages/astro/test/fixtures/content-layer/src/content.config.ts index 82228f61daa0..93f741c60fad 100644 --- a/packages/astro/test/fixtures/content-layer/src/content.config.ts +++ b/packages/astro/test/fixtures/content-layer/src/content.config.ts @@ -152,7 +152,7 @@ const spacecraft = defineCollection({ publishedDate: z.coerce.date(), tags: z.array(z.string()), heroImage: image().optional(), - cat: reference('cats').optional(), + cat: reference('cats').default('siamese'), something: z .string() .optional() From e824b1a7b95a1048ccd9537a7d5c6c820da728fd Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 19 Dec 2024 09:52:05 +0000 Subject: [PATCH 2/3] Format --- packages/astro/src/content/runtime.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index dc48b321f97d..ae20cb90d365 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -18,7 +18,7 @@ import { unescapeHTML, } from '../runtime/server/index.js'; import { CONTENT_LAYER_TYPE, IMAGE_IMPORT_PREFIX } from './consts.js'; -import { type DataEntry, globalDataStore, ImmutableDataStore } from './data-store.js'; +import { type DataEntry, type ImmutableDataStore, globalDataStore } from './data-store.js'; import type { ContentLookupMap } from './utils.js'; type LazyImport = () => Promise; @@ -604,7 +604,7 @@ async function render({ } export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) { - // We're handling it like this to avoid needing an async schema. Not ideal, but should + // We're handling it like this to avoid needing an async schema. Not ideal, but should // be safe because the store will already have been loaded by the time this is called. let store: ImmutableDataStore | null = null; globalDataStore.get().then((s) => (store = s)); @@ -622,22 +622,21 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) }), ]) .transform( - ( + ( lookup: | string | { id: string; collection: string } | { slug: string; collection: string }, ctx, ) => { - if(!store) { + if (!store) { ctx.addIssue({ code: ZodIssueCode.custom, message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved. Store not available.`, }); - return + return; } - const flattenedErrorPath = ctx.path.join('.'); const collectionIsInStore = store.hasCollection(collection); From 0ba701e8490e45fa833bec2d3512c710b2715ed8 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 19 Dec 2024 14:46:51 +0000 Subject: [PATCH 3/3] Better error --- packages/astro/src/content/runtime.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index ae20cb90d365..e1ef6adb9bf8 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -632,7 +632,7 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) if (!store) { ctx.addIssue({ code: ZodIssueCode.custom, - message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved. Store not available.`, + message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved: store not available.\nThis is an Astro bug, so please file an issue at https://github.com/withastro/astro/issues.`, }); return; }