{entry.data.name}
+{entry.data.description}
+``` + +Sans la mise en cache des routes activée, vous pouvez utiliser des indications de cache pour définir manuellement les en-têtes de réponse pour votre propre stratégie de mise en cache : + +```astro title="src/pages/boutique/[id].astro" +--- +export const prerender = false; // Pas nécessaire en mode `server` + +import { getLiveEntry } from 'astro:content'; + +const { entry, error, cacheHint } = await getLiveEntry('produits', Astro.params.id); + +if (error) { + return Astro.redirect('/404'); +} + +if (cacheHint?.tags) { + Astro.response.headers.set('Cache-Tag', cacheHint.tags.join(',')); +} + +if (cacheHint?.lastModified) { + Astro.response.headers.set('Last-Modified', cacheHint.lastModified.toUTCString()); +} +--- + +{entry.data.name}
+{entry.data.description}
+``` + +:::note +Les indications de cache n'entraînent pas automatiquement la mise en cache de la réponse par Astro. Elles fournissent des valeurs que vous pouvez transmettre à la [mise en cache des routes](/fr/reference/experimental-flags/route-caching/) ou utiliser pour implémenter votre propre stratégie de mise en cache. +::: + +## Distribuer votre chargeur + +Les chargeurs peuvent être définis dans votre site ou sous forme de paquet npm distinct. Si vous souhaitez partager votre chargeur avec la communauté, vous pouvez [le publier sur npm avec les mots-clés `withastro` et `astro-loader`](/fr/reference/publish-to-npm/#données-du-fichier-packagejson). + +Un chargeur doit exporter une fonction qui renvoie un objet `LiveLoader` pour les chargeurs en direct ou un objet `Loader` pour les chargeurs appelés lors de la compilation, permettant aux utilisateurs de le configurer avec leurs propres paramètres. + ## API des chargeurs sous forme d'objet -L'API pour les [chargeurs incorporés](#chargeurs-incorporés) est très simple et est présentée ci-dessus. Cette section présente l'API permettant de définir un chargeur sous forme d'objet. ++ +**Type :** `Loader` +
+ +Une fonction de chargeur renvoie un objet comportant deux propriétés obligatoires. En plus de fournir un nom pour le chargeur, cet objet décrit comment récupérer les données de la collection. + +Vous pouvez également renvoyer une troisième propriété définissant un schéma pour valider les entrées de votre collection. Utilisez l'opérateur [`satisfies` de TypeScript](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator) au lieu d'une annotation de type de retour afin de garantir la sûreté du typage au sein de votre objet de chargeur et de préserver l'inférence de type lorsque celui-ci est utilisé dans une collection. + +#### `Loader.name`
-**Type** : `string`
+**Type :** `string`
+
-**Type** : (context: LoaderContext) => Promise<void>
+**Type :** (context: LoaderContext) => Promise<void>
+
-**Type** : `ZodSchema | Promise
+
+**Type :** `() => Promise<{ schema: ZodSchema; types: string }>`
+
-**Type** : `string`
+**Type :** `string`
+
-**Type** : [`DataStore`](#datastore)
+**Type :** [`DataStore`](#datastore)
+
-**Type** : `MetaStore`
+**Type :** `MetaStore`
+
-**Type** : [`AstroIntegrationLogger`](/fr/reference/integrations-reference/#astrointegrationlogger)
+**Type :** [`AstroIntegrationLogger`](/fr/reference/integrations-reference/#astrointegrationlogger)
+
-**Type** : `AstroConfig`
+**Type :** `AstroConfig`
+
-**Type** : `(props: ParseDataOptions
-**Type** : `(markdown: string) => Promise
-**Type** : `(data: Record
+
+**Type :** `(data: Record
-**Type** : `FSWatcher`
+**Type :** `FSWatcher`
+
-**Type** : `Record
-**Type** : (key: string) => DataEntry | undefined
+**Type :** (key: string) => DataEntry | undefined
+
-**Type** : (entry: DataEntry) => boolean
+**Type :** (entry: DataEntry) => boolean
+
-**Type** : `() => Array<[id: string, DataEntry]>`
+**Type :** () => Array\<[id: string, DataEntry]\>
+
-**Type** : `() => Array
-**Type** : `() => Array() => Array\<DataEntry\>
+
-**Type** : `(key: string) => void`
+**Type :** `(key: string) => void`
+
-**Type** : `() => void`
+**Type :** `() => void`
+
-**Type** : `(key: string) => boolean`
+**Type :** `(key: string) => boolean`
+
-**Type** : `string`
+**Type :** `string`
+
-**Type** : `Record
-**Type** : `string | undefined`
+**Type :** `string | undefined`
+
-**Type** : `string | undefined`
+**Type :** `string | undefined`
+
-**Type** : `string | undefined`
+**Type :** `string | undefined`
+
-**Type** : `RenderedContent | undefined`
+**Type :** `RenderedContent | undefined`
+
+ +**Type :** `string` +
+ +Contient la chaîne de caractères rendue au format HTML. Celle-ci est utilisée par [`render()`](/fr/reference/modules/astro-content/#render) pour renvoyer un composant qui effectue le rendu de ce HTML. + +##### `DataEntry.rendered.metadata` + ++ +**Type :** `object | undefined` +
+ +Décrit les métadonnées présentes dans ce fichier. Cela inclut les chemins d'accès aux images (`imagePaths`), les titres (`headings`), le `frontmatter` et toutes autres métadonnées présentes dans le fichier. Si le fichier n'a pas été rendu au format HTML, cette valeur sera `undefined`. + +###### `DataEntry.rendered.metadata.imagePaths` + ++ +**Type :** `string[]` +
+ +Spécifie la liste des chemins d'accès aux images présentes dans cette entrée. Chaque chemin est relatif au [chemin de l'entrée (`filePath`)](#dataentryfilepath). + +###### `DataEntry.rendered.metadata.headings` + ++ +**Type :** `MarkdownHeading[]` +
+ +Spécifie la liste des titres présents dans ce fichier. Chaque titre est décrit par une profondeur (`depth`) déterminée par le niveau du titre (`h1 -> h6`), un `slug` généré avec [`github-slugger`](https://github.com/Flet/github-slugger), et son contenu (`text`). + +###### `DataEntry.rendered.metadata.frontmatter` + +
+
+**Type :** `Record
+
+**Type :** `LiveLoader
+
+**Type :** `string`
+
+
+**Type :** (context: LoadCollectionContext\
+
+
+**Type :** (context: LoadEntryContext\
+
+
+**Type :** `{ filter?: TCollectionFilter; }`
+
+
+**Type :** `Record
+**Default:** `never`
+
+
+**Type :** `{ filter: TEntryFilter; }`
+
+
+**Type :** `Record
+**Default:** `never`
+
+
+**Type :** \{ id: string; data: TData; rendered?: \{ html: string \}; cacheHint?: CacheHint; \}
+
+
+**Type :** `string`
+
+
+**Type :** `Record
+
+**Type :** `{ html: string }`
+
+
+**Type :** [`CacheHint`](#cachehint)
+
+
+**Type :** \{ entries: Array\<LiveDataEntry\
+
+
+**Type :** Array\<LiveDataEntry\
+
+
+**Type :** [`CacheHint`](#cachehint)
+
+
+**Type :** `Array
+
+**Type :** `Date`
+
@@ -34,11 +44,11 @@ import {
+
+**Type :** `(config: LiveCollectionConfig) => LiveCollectionConfig`
+
+
+**Type :** `LiveLoader`
+
+
+**Type :** ZodType
+
-**Type :** `(collection: string) => ZodEffects
+**Type :** (collection: CollectionKey) => ZodEffects\
-**Type :** `(collection: string, filter?: (entry: CollectionEntry(collection: CollectionKey, filter?: (entry: CollectionEntry) => boolean) => CollectionEntry[]
+
+**Type :** (collection: string, filter?: LiveLoaderCollectionFilterType) => Promise\<LiveDataCollectionResult\>
+
-**Types :**
-* `(collection: string, id: string) => Promise(collection: CollectionKey, id: string) => Promise\<CollectionEntry | undefined\>
+* (\{ collection: CollectionKey, id: string \}) => Promise\<CollectionEntry | undefined\>
+
+**Type :** (collection: string, filter: string | LiveLoaderEntryFilterType) => Promise\<LiveDataEntryResult\>
+
-**Type :** `(Array<{ collection: string, id: string }>) => Array(\{ collection: CollectionKey, id: string \}[]) => CollectionEntry[]
-**Type :** `(entry: CollectionEntry) => Promise(entry: CollectionEntry) => Promise\
**Type :** `string` +
Un identifiant unique. Notez que tous les identifiants du chargeur `glob()` intégré d'Astro sont transformés en slug. -#### `collection` +#### `CollectionEntry.collection` -**Exemple de type :** `'blog' | 'authors' | ...` ++ +**Type :** [`CollectionKey`](#collectionkey) +
Le nom d'une collection dans laquelle se trouvent les entrées. Il s'agit du nom utilisé pour référencer la collection dans votre schéma et dans les fonctions de requête. -#### `data` +#### `CollectionEntry.data` + +
**Type :** `CollectionSchema
**Type :** `string | undefined` +
Une chaîne de caractères contenant le corps brut et non compilé du document Markdown ou MDX. Notez que si [`retainBody`](/fr/reference/content-loader-reference/#retainbody) est défini sur `false`, cette valeur sera `undefined` au lieu de contenir le contenu brut du fichier. +#### `CollectionEntry.rendered` + ++ +**Type :** `RenderedContent | undefined` +
+ +Le contenu rendu d'une entrée tel qu'[enregistré par votre chargeur](/fr/reference/content-loader-reference/#dataentryrendered). Par exemple, il peut s'agir du contenu rendu d'une entrée Markdown ou de code HTML provenant d'un CMS. + +#### `CollectionEntry.filePath` + ++ +**Type :** `string | undefined` +
+ +Le chemin d'accès à une entrée, relatif à votre répertoire de projet. Cette valeur n'est disponible que pour les entrées locales. + ### `CollectionKey` -
+
+**Exemple de type :** `'blog' | 'authors' | ...`
+
+
+**Type :** \{ entries?: Array\<LiveDataEntry\
+
+
+**Type :** Array\<LiveDataEntry\
+
+ +**Type :** `TError | LiveCollectionError | undefined` +
+ +Une erreur renvoyée lorsque le chargeur n'a pas pu charger la collection. Il peut s'agir d'une erreur personnalisée définie par le chargeur ou d'une erreur intégrée. + +L'exemple suivant accède à l'erreur renvoyée lors de la récupération de données à partir d'une collection en direct nommée `produits` : + +```astro title="src/pages/boutique/index.astro" +--- +import { getLiveCollection } from 'astro:content'; + +const { error } = await getLiveCollection('produits'); +--- +``` + +
+
+**Type :** CacheHint | undefined
+
+
+**Type :** \{ entry?: LiveDataEntry\
+
+
+**Type :** LiveDataEntry\
+
+ +**Type :** `TError | LiveCollectionError | undefined` +
+ +Une erreur renvoyée lorsque le chargeur n'a pas pu charger l'entrée. Il peut s'agir d'une erreur personnalisée définie par le chargeur ou d'une erreur intégrée. + +L'exemple suivant récupère l'entrée demandée et toute erreur possible depuis une collection en direct nommée `produits`, puis redirige vers la page 404 si une erreur existe : + +```astro title="src/pages/boutique/[id].astro" +--- +import { getLiveEntry } from 'astro:content'; + +const { entry, error } = await getLiveEntry('produits', Astro.params.id); + +if (error) { + return Astro.redirect('/404'); +} +--- +{entry.data.name}
+``` + +
+
+**Type :** CacheHint | undefined
+