8000 Collections reference() API docs do not match current behavior · Issue #11303 · withastro/docs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Collections reference() API docs do not match current behavior #11303

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

Closed
kfranqueiro opened this issue Mar 25, 2025 · 6 comments · Fixed by #11333
Closed

Collections reference() API docs do not match current behavior #11303

kfranqueiro opened this issue Mar 25, 2025 · 6 comments · Fixed by #11333
Labels
good first issue Good for newcomers help wanted Issues look 8000 ing for someone to run with them! improve or update documentation Enhance / update existing documentation (e.g. add example, improve description, update for changes)

Comments

@kfranqueiro
Copy link
kfranqueiro commented Mar 25, 2025

📚 Subject area/topic

Content Collections API Reference

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/reference/modules/astro-content/#reference

📋 Description of content that is out-of-date or incorrect

This accepts a collection name and validates the entry identifier(s) specified in your content frontmatter or data file before transforming the reference into an object containing the collection name and the reference id.

No validation of entries occurs in v5, as originally reported in withastro/astro#13268. Moreover, without some alternative explanation, this may leave developers wondering what reason there is to use the reference() API at all in v5.

From discussion in the original reported issue, this is apparently a regression to be tackled in v6, though I do not know where that is tracked since the issue in question has been closed (as "not planned", which sends the opposite message) and locked.

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@sarah11918
Copy link
Member

Thanks for filing this on the docs repo, as it appears that yes, the behaviour has changed!

It seems @KianNH (in the original issue filed) described when and how validation occurs, and how it differs from when it did in v4:

In Astro 4, when provided with an identifier that does not exist in the referenced collection, an error would be thrown:

In Astro 5, the entries are accepted and would only fail at the time when getEntry()/getEntries() is used and undefined is returned.

And @florian-lefebvre stated:

It still makes sense for reference() to exist since it works in many cases still!

Florian, can you provide some context/examples of when and how it does work so that we can make sure in docs we are saying when/why to use reference() properly?

This should be updated in docs to properly describe the intended/recommended use case of reference(), and I'll throw a help wanted on this for anyone who would like to work on this and make a PR! 🙌

@sarah11918 sarah11918 added improve or update documentation Enhance / update existing documentation (e.g. add example, improve description, update for changes) help wanted Issues looking for someone to run with them! good first issue Good for newcomers labels Mar 25, 2025
@florian-lefebvre
Copy link
Member

Indeed getEntry() and getEntries() only validate at runtime. So on pre-rendered pages, the behavior is similar to v4 because they run at build time. However if they're used on a a page rendered on demand, they will not throw during the build which is unfortunate. Does that help?

@kfranqueiro
Copy link
Author

So on pre-rendered pages, the behavior is similar to v4 because they run at build time.

The point I have been attempting to make is that this behavior would still be just as similar even if you didn't use reference at all, right? You could store an id in a field defined as z.string() and it would still validate at runtime through getEntry. reference does nothing special in v5.

So as a developer, what reason do I have to use reference? What should the docs tell me about it?

@sarah11918
Copy link
Member

Noting that you didn't really answer my question, @florian-lefebvre ! You didn't give me an example of when or now to use reference() that I can put in docs! 😅

@ArmandPhilippot
Copy link
Member

For a single reference, I'd say it's just a handy helper to avoid writing the queried collection explicitly:

// Using `author: reference("authors")`, we can use this:
const author = await getEntry(blogPost.data.author);

// Using `author: z.string()`, we would need to write:
const author = await getEntry("authors", blogPost.data.author);

So, yes, without validation, I don't think there's a real reason to favor one over the other. It may just seem more convenient to some or if wrapped in a generic helper function to process collection entries (ie. when we don't know in advance the collection name to use).

I guess this is more useful with complex types. I don't know if an union of references is possible... But, for example, when using an array of references:

// Using `authors: z.array(reference("authors"))`, we can use this:
const authors = await getEntries(blogPost.data.authors);

// Using `authors: z.array(z.string())`, we would need to write:
const authors = await getEntries(blogPost.data.authors.map((author) => ({collection: "authors", id: author}));

I could be wrong but I think, with the .map version, Typescript will complain because of the getEntries overloads (slug/id for backward compability) and an explicit type will probably be necessary here. So, the reference() version is shorter, we don't have to replicate the same mapping each time we use references, and this is Typescript-friendly.

@florian-lefebvre
Copy link
Member

Yeah i think Armand is quite right there. Btw I realized I thought all this time we were talking about getEntry, not reference 🤦. They are somewhat related ofc but yeah I think it's mainly for convenience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Issues looking for someone to run with them! improve or update documentation Enhance / update existing documentation (e.g. add example, improve description, update for changes)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0