-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[v6] New content collections guide includes live collections #12604
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
Open
sarah11918
wants to merge
43
commits into
v6
Choose a base branch
from
stable-live-collections
base: v6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,113
−211
Open
Changes from 41 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
530c68f
remove legacy note, start to rewrite introduction
sarah11918 7a36689
what are content collections?
sarah11918 a0940ae
types of collections section
sarah11918 0e5b449
more about collections
sarah11918 cd362bd
attempt at redoing the loader section
sarah11918 cc8cd1b
more section heading work
sarah11918 20150cf
more sections added to incorporate and organize live collections
sarah11918 706d7d1
more attention to "quick start" of easy case, streamlining Accessing …
sarah11918 e3a03df
incorporate current updates to content collections guide page
sarah11918 ff9926f
way more links to make happy paths, and attempts to make loader conte…
sarah11918 1b192b1
some polish to generating routes section
sarah11918 7167df0
talk about generating pages for each type of collection to get links …
sarah11918 d8be8e7
placeholder content for astro:content reference items (and so links w…
sarah11918 25e1fff
Merge branch 'v6' into stable-live-collections
sarah11918 53d6f32
update `/en/` broken links
sarah11918 8fb34fd
fix one more en link
sarah11918 2738a82
actcually fix en link
sarah11918 f4e8d2a
fix international links, mostly by deleting German pages slated for d…
sarah11918 0e1cd7a
astro:module nits/easy commits
sarah11918 d6ea42f
Merge branch 'v6' into stable-live-collections
sarah11918 b9e06cf
Merge branch 'v6' into stable-live-collections
sarah11918 2d40073
update chnaged anchor links
sarah11918 aa5f25d
[v6] Update `astro:content` docs (#12620)
ArmandPhilippot 7b1dd81
revised What are Collections and When (not) to Use sections
sarah11918 928cbaa
actual working example for build-time collections
sarah11918 55edf6a
Armand typo fix
sarah11918 b0e59d0
remove duplicate sentence
sarah11918 422a94d
lower case npm
sarah11918 a3e2d50
Merge branch 'stable-live-collections' of https://github.com/withastr…
sarah11918 02e9773
errant section heading levels fixed
sarah11918 b659cd9
define entry, update example to use getCollection
sarah11918 ebe1f8f
reorganization into build time and live main sections
sarah11918 1dd7a5c
missing preposition
sarah11918 d93b60b
updated holdover legacy references
sarah11918 bc8622d
when one or more of these is true...
sarah11918 33c76fb
build-time loaders polish
sarah11918 aebfd5c
build-time schema polish
sarah11918 617659a
live collections polish
sarah11918 ef68d7d
attempt to better organize and handle "pile of sections" at the bottom
sarah11918 b479c55
remove live collection limitations heading and work into section dire…
sarah11918 1f648da
Merge branch 'v6' into stable-live-collections
sarah11918 ed51725
heading weirdness
sarah11918 d7acbe4
[V6]: Various fixes in content collections pages (#12685)
ArmandPhilippot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,14 +5,15 @@ sidebar: | |||||
| i18nReady: true | ||||||
| --- | ||||||
| import Since from '~/components/Since.astro'; | ||||||
| import ReadMore from '~/components/ReadMore.astro'; | ||||||
|
|
||||||
| Astro's Content Loader API allows you to load your data from any source, local or remote, and interact with Astro's content layer to manage your [content collections](/en/guides/content-collections/). | ||||||
|
|
||||||
| ## What is a loader? | ||||||
|
|
||||||
| Astro loaders allow you to load data into [content collections](/en/guides/content-collections/), which can then be used in pages and components. The [built-in `glob()` and `file()` loaders](/en/guides/content-collections/#built-in-loaders) are used to load content from the file system, and you can create your own loaders to load content from other sources. | ||||||
| Astro loaders allow you to load data into [content collections](/en/guides/content-collections/), which can then be used in pages and components. The [built-in `glob()` and `file()` loaders](/en/guides/content-collections/#build-time-collection-loaders) are used to load content from the file system, and you can create your own loaders to load content from other sources. | ||||||
|
|
||||||
| Each collection needs [a loader defined in its schema](/en/guides/content-collections/#defining-the-collection-loader). You can define a loader inline in your project's `src/content.config.ts` file, share one loader between multiple collections, or even [publish your loader to NPM as a package](/en/reference/publish-to-npm/) to share with others and be included in our integrations library. | ||||||
| Each collection needs a loader defined in its schema. You can define a loader inline in your project's `src/content.config.ts` file, share one loader between multiple collections, or even [publish your loader to NPM as a package](/en/reference/publish-to-npm/) to share with others and be included in our integrations library. | ||||||
|
|
||||||
| ## Built-in loaders | ||||||
|
|
||||||
|
|
@@ -176,7 +177,7 @@ const countries = defineCollection({ | |||||
|
|
||||||
| ### Object loaders | ||||||
|
|
||||||
| A loader is an object with a `load()` method that is called at build time to fetch data and update the data store. It allows entries to be updated incrementally, or for the store to be cleared only when necessary. It can also define a schema for the entries, which can be used to validate the data and generate static types. | ||||||
| A loader is an object with a [`load()` method](#load) that is called at build time to fetch data and update the data store. It allows [entries](#dataentry) to be updated incrementally, or for the store to be cleared only when necessary. It can also define a schema for the entries, which can be used to validate the data and generate static types. | ||||||
|
|
||||||
| The recommended pattern is to define a function that accepts configuration options and returns the loader object, in the same way that you would normally define an Astro integration or Vite plugin. | ||||||
|
|
||||||
|
|
@@ -222,9 +223,78 @@ const blog = defineCollection({ | |||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| ### Live loaders | ||||||
|
|
||||||
| A live loader is an object with two methods: `loadCollection()` and `loadEntry()` that should handle errors gracefully and return either data or an `Error` object. | ||||||
|
|
||||||
| ```ts title="src/article-loader.ts" | ||||||
| import type { LiveLoader } from 'astro/loaders'; | ||||||
| import { fetchFromCMS } from './cms-client.js'; | ||||||
|
|
||||||
| interface Article { | ||||||
| id: string; | ||||||
| title: string; | ||||||
| content: string; | ||||||
| author: string; | ||||||
| } | ||||||
|
|
||||||
| export function articleLoader(config: { apiKey: string }): LiveLoader<Article> { | ||||||
| return { | ||||||
| name: 'article-loader', | ||||||
| loadCollection: async ({ filter }) => { | ||||||
| try { | ||||||
| const articles = await fetchFromCMS({ | ||||||
| apiKey: config.apiKey, | ||||||
| type: 'article', | ||||||
| filter, | ||||||
| }); | ||||||
|
|
||||||
| return { | ||||||
| entries: articles.map((article) => ({ | ||||||
| id: article.id, | ||||||
| data: article, | ||||||
| })), | ||||||
| }; | ||||||
| } catch (error) { | ||||||
| return { | ||||||
| error: new Error(`Failed to load articles: ${error.message}`), | ||||||
| }; | ||||||
| } | ||||||
| }, | ||||||
| loadEntry: async ({ filter }) => { | ||||||
| try { | ||||||
| // filter will be { id: "some-id" } when called with a string | ||||||
| const article = await fetchFromCMS({ | ||||||
| apiKey: config.apiKey, | ||||||
| type: 'article', | ||||||
| id: filter.id, | ||||||
| }); | ||||||
|
|
||||||
| if (!article) { | ||||||
| return { | ||||||
| error: new Error('Article not found'), | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| return { | ||||||
| id: article.id, | ||||||
| data: article, | ||||||
| }; | ||||||
| } catch (error) { | ||||||
| return { | ||||||
| error: new Error(`Failed to load article: ${error.message}`), | ||||||
| }; | ||||||
| } | ||||||
| }, | ||||||
| }; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| <ReadMore>[See the `Content Collection` guide](/en/guides/content-collections/#creating-a-live-loader) for more information about creating a live loader and example usage.</ReadMore> | ||||||
|
|
||||||
| ## Object loader API | ||||||
|
|
||||||
| The API for [inline loaders](#inline-loaders) is very simple, and is shown above. This section shows the API for defining an object loader. | ||||||
| The API for [inline loaders](#inline-loaders) is very simple, and is shown above. This section shows the API for defining an [object loader](#object-loaders). | ||||||
|
|
||||||
| ### The `Loader` object | ||||||
|
|
||||||
|
|
@@ -670,3 +740,67 @@ The format of the `RenderedContent` object is: | |||||
| ``` | ||||||
|
|
||||||
| If the entry has Markdown content then you can use the [`renderMarkdown()`](#rendermarkdown) function to generate this object from the Markdown string. | ||||||
|
|
||||||
| ## Live loader API | ||||||
|
|
||||||
| This section shows the API for defining a [live loader](#live-loaders). | ||||||
|
|
||||||
| ### The `LiveLoader` object | ||||||
|
|
||||||
| A generic type to provide [type safety in your loader](/en/guides/content-collections/#type-safety-in-live-loaders). This describes an object and accepts four type parameters to describe, in this order: your data structure, your filters when querying a collection, your filters when querying a single entry, and errors. | ||||||
|
|
||||||
| This object contains the following properties: | ||||||
|
|
||||||
| #### `name` | ||||||
|
|
||||||
| <p> | ||||||
|
|
||||||
| **Type:** `string` | ||||||
| </p> | ||||||
|
|
||||||
|
|
||||||
| A unique name for the loader, used in logs. | ||||||
|
|
||||||
| #### `loadCollection()` | ||||||
|
|
||||||
| <p> | ||||||
|
|
||||||
| **Type:** `(context: LoadCollectionContext<TCollectionFilter>) => Promise<LiveDataCollection<TData> | { error: TError; }>` | ||||||
| </p> | ||||||
|
|
||||||
| Defines a method to load a collection of entries. This function receives a [context object](#loadcollectioncontext) containing an optional `filter` property and must return a the data associated to this collection or the errors. | ||||||
|
|
||||||
| #### `loadEntry()` | ||||||
|
|
||||||
| <p> | ||||||
|
|
||||||
| **Type:** `(context: LoadEntryContext<TEntryFilter>) => Promise<LiveDataEntry<TData> | undefined | { error: TError; }>` | ||||||
| </p> | ||||||
|
|
||||||
| Defines a method to load a single entry. This function receives a [context object](#loadentrycontext) containing a `filter` property and returns either the data associated to the requested entry, `undefined` when the entry cannot be found, or the errors. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ### `LoadCollectionContext` | ||||||
|
|
||||||
| This object is passed to the [`loadCollection()` method](#loadcollection) of the loader and contains the following properties: | ||||||
|
|
||||||
| #### `filter` | ||||||
|
|
||||||
| <p> | ||||||
|
|
||||||
| **Type:** `Record<string, any>` | ||||||
| </p> | ||||||
|
|
||||||
| An object describing the filters supported by your loader. | ||||||
|
|
||||||
| ### `LoadEntryContext` | ||||||
|
|
||||||
| This object is passed to the [`loadEntry()` method](#loadentry) of the loader and contains the following properties: | ||||||
|
|
||||||
| #### `filter` | ||||||
|
|
||||||
| <p> | ||||||
|
|
||||||
| **Type:** `Record<string, any>` | ||||||
| </p> | ||||||
|
|
||||||
| An object describing the filters supported by your loader. | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo + nit