Singletons in the Content Collections API #449
Replies: 2 comments 8 replies
-
|
It's been quite a while now since this has proposed, and I think there is some real value in having singleton entries in Astro. As OP has described, it's quite common to use content collections with a single entry for displaying content on a singular page alone. As an example, I do this on my own page for the topmost "about" section, but something like a privacy policy page would also benefit from this. To build on the proposed idea, I think there should be a dedicated "singletons directory" of some kind for better organization: The main benefit here, in my opinion, is on the sides of DX and directory structure complexity. The current setup with an "index" file works, but it leads to lots of folders with very little subcontent. |
Beta Was this translation helpful? Give feedback.
-
|
2 questions:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm proposing adding a simple way of indicating to the Content Collections API that a collection is a singleton and should have exactly one entry. These could be used to more intuitively describe single pages, footer/header content, common settings, and more.
Background & Motivation
I love Astro, and I especially love the new Content Collections API. I think it is such a clean and easy way of separating editable content from page structure, styling, and other concerns — especially when you want your content to be editable by less technical users. I have been building a new website using the API, and I had an idea for a small improvement that could improve it.
I want to load the content of my about page from the Content Collections API so that it can have a type-safe schema and intellisense. Right now, I am doing this as follows:
This is not bad, but I think it could be even better if there we a more explicit way of defining singletons. This would improve code readability, simplify the directory structure, and make singletons more enforceable (instead of feeling like a workaround). It would especially improve the experience of less technical users who wish to understand and update the
contentdirectory, while also adding more context for more advanced use cases (like creating a visual CMS based on the schema that writes to thecontentdirectory).This is a very small change (mostly just syntactic sugar). I imagine it would not be too difficult to implement and would add very little complexity.
Goals
contentdirectorycontentdirectoryconfig.ts, for example) to know which entries are singletons and which are part of a collection.Example
One way to do this would be to create new function called
defineSingletonto be used in theconfig.tsfile:Singletons could be exported along with normal collections:
Or they could be exported separately (i.e.
export const singletons = {...}), mitigating the need for a newdefineSingletonfunction.Then, the content directory would look something like this:
Finally, to use the singleton, there would be a getSingleton function (similar to getEntryBySlug, but with only one parameter):
I feel like this would be a more explicit, clean, and enforceable way of providing type-safe markdown content for special pages like an about page or a homepage.
Beta Was this translation helpful? Give feedback.
All reactions