Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make reference() a sync transform #12781

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: make reference() a sync transform
  • Loading branch information
ascorbic committed Dec 19, 2024
commit e87f5866c125d88e832e4fb37a6618f457bd767c
5 changes: 5 additions & 0 deletions .changeset/fluffy-ants-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a regression that caused `default()` to not work with `reference()`
18 changes: 15 additions & 3 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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';

Check failure on line 21 in packages/astro/src/content/runtime.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/style/useImportType

Some named imports are only used as types.
import type { ContentLookupMap } from './utils.js';

type LazyImport = () => Promise<any>;
Expand Down Expand Up @@ -604,6 +604,10 @@
}

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([
Expand All @@ -618,15 +622,23 @@
}),
])
.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.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a more actionable message. If the user encounters this error, what should they do? File an issue? Purge the store? We should provide an escape hatch to our users

});
return
}


const flattenedErrorPath = ctx.path.join('.');
const store = await globalDataStore.get();
const collectionIsInStore = store.hasCollection(collection);

if (typeof lookup === 'object') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading