summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2025-01-02 09:49:20 +0000
committerGravatar GitHub <noreply@github.com> 2025-01-02 09:49:20 +0000
commit96c4b925333fede1a53d19657d15e0052da90780 (patch)
treedd36984d839b04656e4201af502723b5c5a10c46
parentf7068995aa451dced13853789b0d51433c2373b5 (diff)
downloadastro-96c4b925333fede1a53d19657d15e0052da90780.tar.gz
astro-96c4b925333fede1a53d19657d15e0052da90780.tar.zst
astro-96c4b925333fede1a53d19657d15e0052da90780.zip
fix: make `reference()` a sync transform (#12781)
-rw-r--r--.changeset/fluffy-ants-reflect.md5
-rw-r--r--packages/astro/src/content/runtime.ts17
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content.config.ts2
3 files changed, 20 insertions, 4 deletions
diff --git a/.changeset/fluffy-ants-reflect.md b/.changeset/fluffy-ants-reflect.md
new file mode 100644
index 000000000..924c79910
--- /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 cf6537609..ea1527106 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, type ImmutableDataStore, globalDataStore } from './data-store.js';
import type { ContentLookupMap } from './utils.js';
type LazyImport = () => Promise<any>;
@@ -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,22 @@ 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.\nThis is an Astro bug, so please file an issue at https://github.com/withastro/astro/issues.`,
+ });
+ 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 b0e972256..484d14d52 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()