mirror of
https://github.com/withastro/astro.git
synced 2025-01-22 18:41:55 -05:00
fix: make reference()
a sync transform (#12781)
This commit is contained in:
parent
f7068995aa
commit
96c4b92533
3 changed files with 20 additions and 4 deletions
5
.changeset/fluffy-ants-reflect.md
Normal file
5
.changeset/fluffy-ants-reflect.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes a regression that caused `default()` to not work with `reference()`
|
|
@ -18,7 +18,7 @@ import {
|
||||||
unescapeHTML,
|
unescapeHTML,
|
||||||
} from '../runtime/server/index.js';
|
} from '../runtime/server/index.js';
|
||||||
import { CONTENT_LAYER_TYPE, IMAGE_IMPORT_PREFIX } from './consts.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';
|
import type { ContentLookupMap } from './utils.js';
|
||||||
|
|
||||||
type LazyImport = () => Promise<any>;
|
type LazyImport = () => Promise<any>;
|
||||||
|
@ -604,6 +604,10 @@ async function render({
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createReference({ lookupMap }: { lookupMap: ContentLookupMap }) {
|
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 function reference(collection: string) {
|
||||||
return z
|
return z
|
||||||
.union([
|
.union([
|
||||||
|
@ -618,15 +622,22 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap })
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
.transform(
|
.transform(
|
||||||
async (
|
(
|
||||||
lookup:
|
lookup:
|
||||||
| string
|
| string
|
||||||
| { id: string; collection: string }
|
| { id: string; collection: string }
|
||||||
| { slug: string; collection: string },
|
| { slug: string; collection: string },
|
||||||
ctx,
|
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 flattenedErrorPath = ctx.path.join('.');
|
||||||
const store = await globalDataStore.get();
|
|
||||||
const collectionIsInStore = store.hasCollection(collection);
|
const collectionIsInStore = store.hasCollection(collection);
|
||||||
|
|
||||||
if (typeof lookup === 'object') {
|
if (typeof lookup === 'object') {
|
||||||
|
|
|
@ -152,7 +152,7 @@ const spacecraft = defineCollection({
|
||||||
publishedDate: z.coerce.date(),
|
publishedDate: z.coerce.date(),
|
||||||
tags: z.array(z.string()),
|
tags: z.array(z.string()),
|
||||||
heroImage: image().optional(),
|
heroImage: image().optional(),
|
||||||
cat: reference('cats').optional(),
|
cat: reference('cats').default('siamese'),
|
||||||
something: z
|
something: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
|
|
Loading…
Reference in a new issue