Skip to content

Commit aa5f25d

Browse files
ArmandPhilippotascorbicsarah11918yanthomasdev
authored
[v6] Update astro:content docs (#12620)
Co-authored-by: Matt Kane <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Yan <[email protected]>
1 parent 2d40073 commit aa5f25d

File tree

2 files changed

+222
-60
lines changed

2 files changed

+222
-60
lines changed

src/content/docs/en/reference/content-loader-reference.mdx

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sidebar:
55
i18nReady: true
66
---
77
import Since from '~/components/Since.astro';
8+
import ReadMore from '~/components/ReadMore.astro';
89

910
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/).
1011

@@ -176,7 +177,7 @@ const countries = defineCollection({
176177

177178
### Object loaders
178179

179-
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.
180+
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.
180181

181182
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.
182183

@@ -222,9 +223,78 @@ const blog = defineCollection({
222223
});
223224
```
224225

226+
### Live loaders
227+
228+
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.
229+
230+
```ts title="src/article-loader.ts"
231+
import type { LiveLoader } from 'astro/loaders';
232+
import { fetchFromCMS } from './cms-client.js';
233+
234+
interface Article {
235+
id: string;
236+
title: string;
237+
content: string;
238+
author: string;
239+
}
240+
241+
export function articleLoader(config: { apiKey: string }): LiveLoader<Article> {
242+
return {
243+
name: 'article-loader',
244+
loadCollection: async ({ filter }) => {
245+
try {
246+
const articles = await fetchFromCMS({
247+
apiKey: config.apiKey,
248+
type: 'article',
249+
filter,
250+
});
251+
252+
return {
253+
entries: articles.map((article) => ({
254+
id: article.id,
255+
data: article,
256+
})),
257+
};
258+
} catch (error) {
259+
return {
260+
error: new Error(`Failed to load articles: ${error.message}`),
261+
};
262+
}
263+
},
264+
loadEntry: async ({ filter }) => {
265+
try {
266+
// filter will be { id: "some-id" } when called with a string
267+
const article = await fetchFromCMS({
268+
apiKey: config.apiKey,
269+
type: 'article',
270+
id: filter.id,
271+
});
272+
273+
if (!article) {
274+
return {
275+
error: new Error('Article not found'),
276+
};
277+
}
278+
279+
return {
280+
id: article.id,
281+
data: article,
282+
};
283+
} catch (error) {
284+
return {
285+
error: new Error(`Failed to load article: ${error.message}`),
286+
};
287+
}
288+
},
289+
};
290+
}
291+
```
292+
293+
<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>
294+
225295
## Object loader API
226296

227-
The API for [inline loaders](#inline-loaders) is very simple, and is shown above. This section shows the API for defining an object loader.
297+
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).
228298

229299
### The `Loader` object
230300

@@ -670,3 +740,67 @@ The format of the `RenderedContent` object is:
670740
```
671741

672742
If the entry has Markdown content then you can use the [`renderMarkdown()`](#rendermarkdown) function to generate this object from the Markdown string.
743+
744+
## Live loader API
745+
746+
This section shows the API for defining a [live loader](#live-loaders).
747+
748+
### The `LiveLoader` object
749+
750+
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.
751+
752+
This object contains the following properties:
753+
754+
#### `name`
755+
756+
<p>
757+
758+
**Type:** `string`
759+
</p>
760+
761+
762+
A unique name for the loader, used in logs.
763+
764+
#### `loadCollection()`
765+
766+
<p>
767+
768+
**Type:** `(context: LoadCollectionContext<TCollectionFilter>) => Promise<LiveDataCollection<TData> | { error: TError; }>`
769+
</p>
770+
771+
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.
772+
773+
#### `loadEntry()`
774+
775+
<p>
776+
777+
**Type:** `(context: LoadEntryContext<TEntryFilter>) => Promise<LiveDataEntry<TData> | undefined | { error: TError; }>`
778+
</p>
779+
780+
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.
781+
782+
### `LoadCollectionContext`
783+
784+
This object is passed to the [`loadCollection()` method](#loadcollection) of the loader and contains the following properties:
785+
786+
#### `filter`
787+
788+
<p>
789+
790+
**Type:** `Record<string, any>`
791+
</p>
792+
793+
An object describing the filters supported by your loader.
794+
795+
### `LoadEntryContext`
796+
797+
This object is passed to the [`loadEntry()` method](#loadentry) of the loader and contains the following properties:
798+
799+
#### `filter`
800+
801+
<p>
802+
803+
**Type:** `Record<string, any>`
804+
</p>
805+
806+
An object describing the filters supported by your loader.

0 commit comments

Comments
 (0)