Skip to content

Commit 84941e5

Browse files
committed
build time loader content in guide is more guide-y
1 parent 862e58b commit 84941e5

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/content/docs/en/guides/content-collections.mdx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Collections may not be your solution if:
7171

7272
## Types of collections
7373

74-
**[Build-time content collections](#build-time-content-collections)** are updated at build time, and data is saved to a storage layer. This provides excellent performance for most content, but may not be suitable for frequently updating data sources requiring up-to-the-moment data freshness, such as live stock prices.
74+
[Build-time content collections](#build-time-content-collections) are updated at build time, and data is saved to a storage layer. This provides excellent performance for most content, but may not be suitable for frequently updating data sources requiring up-to-the-moment data freshness, such as live stock prices.
7575

7676
For the best performance and scalability, use build-time content collections when one or more of these is true:
7777

@@ -85,7 +85,7 @@ For the best performance and scalability, use build-time content collections whe
8585
See [the official Astro blog starter template](https://github.com/withastro/astro/tree/latest/examples/blog) to get up and running quickly with an example of using the [built-in `glob()` loader](#the-glob-loader) and [defining a schema](#defining-the-collection-schema) for a collection of local Markdown or MDX blog posts.
8686
:::
8787

88-
**[Live content collections](#live-content-collections)** fetch their data at runtime rather than build time. This allows you to access frequently updated data from CMSs, APIs, databases, or other sources using a unified API, without needing to rebuild your site when the data changes. However, this can come at a performance cost since data is fetched at each request and returned directly with no data store persistence.
88+
[Live content collections](#live-content-collections) fetch their data at runtime rather than build time. This allows you to access frequently updated data from CMSs, APIs, databases, or other sources using a unified API, without needing to rebuild your site when the data changes. However, this can come at a performance cost since data is fetched at each request and returned directly with no data store persistence.
8989

9090
Live content collections are designed for data that changes frequently and needs to be up-to-date when a page is requested. Consider using them when one or more of these is true:
9191

@@ -163,7 +163,7 @@ Astro provides two built-in loader functions (`glob()` and `file()`) for fetchin
163163

164164
The [`glob()` loader](/en/reference/content-loader-reference/#glob-loader) fetches entries from directories of Markdown, MDX, Markdoc, JSON, YAML, or TOML files from anywhere on the filesystem. If you store your content entries locally using one file per entry, such as a directory of blog posts, then the `glob()` loader is all you need to access your content.
165165

166-
This loader accepts a `pattern` of entry files to match using glob patterns supported by [micromatch](https://github.com/micromatch/micromatch#matching-features), and a `base` file path of where your files are located. Each entry's `id` will be automatically generated from its file name.
166+
This loader requires a `pattern` of entry files to match using glob patterns supported by [micromatch](https://github.com/micromatch/micromatch#matching-features), and a `base` file path of where your files are located. An `id` for each entry will be automatically generated from its file name. You can use the [`generateID()`](/en/reference/content-loader-reference/#generateid) helper function if you need to transform these IDs to match a specific requirement.
167167

168168
```ts title="src/content.config.ts" {5}
169169
import { defineCollection } from 'astro:content';
@@ -178,9 +178,10 @@ export const collections = { blog };
178178

179179
#### The `file()` loader
180180

181-
The [`file()` loader](/en/reference/content-loader-reference/#file-loader) fetches multiple entries from a single local file. This is useful if your content entries are stored as multiple objects within a single JSON or TOML file. Each entry in the file must have a unique `id` key property.
181+
The [`file()` loader](/en/reference/content-loader-reference/#file-loader) fetches multiple entries from a single local `file` defined in your collection. Use this loader when you have a JSON, YAML, or TOML data file that can be parsed as an array of objects. Each entry in the file must have a unique `id` key property.
182+
183+
The `file()` loader will automatically detect and parse (based on their file extension) a single array of objects from JSON and YAML files, and will treat each top-level table as an independent entry in TOML files.
182184

183-
It accepts a file path to your file and optionally a [`parser` function](#parser-function) for data files it cannot parse automatically. Use this loader when your data file can be parsed as an array of objects.
184185

185186
```ts title="src/content.config.ts" {5}
186187
import { defineCollection } from 'astro:content';
@@ -193,13 +194,11 @@ const dogs = defineCollection({
193194
export const collections = { dogs };
194195
```
195196

196-
##### `parser` function
197-
198-
The `file()` loader accepts a second argument that defines a `parser` function. This allows you to specify a custom parser (e.g. `csv-parse`) to create a collection from a file's contents.
197+
##### Parsing other data formats
199198

200-
The `file()` loader will automatically detect and parse (based on their file extension) a single array of objects from JSON and YAML files, and will treat each top-level table as an independent entry in TOML files. Support for these file types is built-in, and there is no need for a `parser` unless you have a [nested JSON document](#nested-json-documents). To use other files, such as `.csv`, you will need to create a parser function.
199+
Support for parsing JSON, YAML, and TOML files into collection entries is built-in (unless you have a [nested JSON document](#nested-json-documents)). To load your collection from other file types, such as `.csv`, you will need to create a [parser function](/en/reference/content-loader-reference/#parser).
201200

202-
The following example shows importing a CSV parser, then loading a `cats` collection into your project by passing both a file path and `parser` function to the `file()` loader:
201+
The following example shows importing a third-party CSV parser, then defining a `cats` collection by passing both a `file` path and an options object containing a `parser` function to the `file()` loader:
203202

204203
```typescript title="src/content.config.ts" {3} "parser: (text) => parseCsv(text, { columns: true, skipEmptyLines: true })"
205204
import { defineCollection } from "astro:content";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ An optional object with the following properties:
144144
**Type:** `(text: string) => Record<string, Record<string, unknown>> | Array<Record<string, unknown>>`
145145
</p>
146146

147-
A callback function to create a collection from a file’s contents. Use it when you need to process file not supported by default (e.g. `.csv`) or when using [nested `.json` documents](/en/guides/content-collections/#nested-json-documents).
147+
A callback function to create a collection from a file’s contents. Use it when you need to process files other than JSON, YAML, or TOML that not supported by default (e.g. `.csv`) or when using [nested `.json` documents](/en/guides/content-collections/#nested-json-documents).
148148

149149
## Loader types
150150

0 commit comments

Comments
 (0)