Skip to content
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

[Content collections] Clarify type generation step #2345

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/pages/en/guides/content-collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default defineConfig({
});
```

Then, run `astro dev`, `astro build`, or `astro sync` to generate types. This will make the `astro:content` module available for querying and configuring your content collections.

### Update TypeScript configuration

To benefit from the full TypeScript and autocompletion features of [using schemas with your collections](#defining-a-collection-schema), you may also need to update `tsconfig.json`. Follow the instructions below based on your Astro project's current [TypeScript configuration](/en/guides/typescript/#setup).
Expand Down Expand Up @@ -135,6 +137,10 @@ To configure schemas, create a `src/content/config.ts` file (`.js` and `.mjs` ex
2. Define a `schema` for each collection.
2. Export a single `collections` object, with each object key corresponding to the collection's folder name.

:::tip
Run `astro dev`, `astro build`, or `astro sync` before using the `astro:content` module. These commands will generate types for utilities like `defineCollection()`.
:::

For example, say you maintain two collections: one for release announcements and one for blog content. Your entries at `src/content/releases/` should include a `title` and `version`. Your `src/content/engineering-blog/` collection entries should have a `title`, list of `tags`, and an optional `image` URL.

You can specify each expected property in the `schema` field of `defineCollection`:
Expand Down Expand Up @@ -238,6 +244,10 @@ export const collections = { blog };

## Querying content collections

:::tip
Run `astro dev`, `astro build`, or `astro sync` before using the `astro:content` module. These commands will generate types for utilities like `getCollection()` and `getEntry()`.
:::

Astro provides two functions to query collections:

### `getCollection()`
Expand Down