-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Stabilize live collections #14550
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
Merged
+92
−38
Merged
Stabilize live collections #14550
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78ee94c
Stabilise live collections
ascorbic 33fa762
Apply suggestions from code review
ascorbic d00cddd
Merge branch 'next' into stabilise-live-collections
ascorbic d3bc6ab
Apply suggestions from code review
ascorbic 89cb2fa
Clarify changes needed for Astro 6 upgrade
ascorbic b3bb026
Merge branch 'next' into stabilise-live-collections
ascorbic 6170318
Apply suggestions from code review
ascorbic 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Adds support for live content collections | ||
|
|
||
| Live content collections are a new type of [content collection](https://docs.astro.build/en/guides/content-collections/) that 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. | ||
|
|
||
| #### Live collections vs build-time collections | ||
|
|
||
| In Astro 5.0, the content layer API added support for adding diverse content sources to content collections. You can create loaders that fetch data from any source at build time, and then access it inside a page via `getEntry()` and `getCollection()`. The data is cached between builds, giving fast access and updates. | ||
|
|
||
| However, there was no method for updating the data store between builds, meaning any updates to the data needed a full site deploy, even if the pages are rendered on demand. This meant that content collections were not suitable for pages that update frequently. Instead, these pages tended to access the APIs directly in the frontmatter. This worked, but it led to a lot of boilerplate, and meant users didn't benefit from the simple, unified API that content loaders offer. In most cases, users tended to individually create loader libraries shared between pages. | ||
|
|
||
| Live content collections ([introduced experimentally in Astro 5.10](https://astro.build/blog/live-content-collections-deep-dive/)) solve this problem by allowing you to create loaders that fetch data at runtime, rather than build time. This means that the data is always up-to-date, without needing to rebuild the site. | ||
|
|
||
| #### How to use | ||
|
|
||
| To use live collections, create a new `src/live.config.ts` file (alongside your `src/content.config.ts` if you have one) to define your live collections with a live content loader using the new `defineLiveCollection()` function from the `astro:content` module: | ||
|
|
||
| ```ts title="src/live.config.ts" | ||
| import { defineLiveCollection } from 'astro:content'; | ||
| import { storeLoader } from '@mystore/astro-loader'; | ||
|
|
||
| const products = defineLiveCollection({ | ||
| loader: storeLoader({ | ||
| apiKey: process.env.STORE_API_KEY, | ||
| endpoint: 'https://api.mystore.com/v1', | ||
| }), | ||
| }); | ||
|
|
||
| export const collections = { products }; | ||
| ``` | ||
|
|
||
| You can then use the `getLiveCollection()` and `getLiveEntry()` functions to access your live data: | ||
|
|
||
| ```astro | ||
| --- | ||
| import { getLiveCollection, getLiveEntry, render } from 'astro:content'; | ||
| // Get all products | ||
| const { entries: allProducts, error } = await getLiveCollection('products'); | ||
| if (error) { | ||
| // Handle error appropriately | ||
| console.error(error.message); | ||
| } | ||
| // Get products with a filter (if supported by your loader) | ||
| const { entries: electronics } = await getLiveCollection('products', { category: 'electronics' }); | ||
| // Get a single product by ID (string syntax) | ||
| const { entry: product, error: productError } = await getLiveEntry('products', Astro.params.id); | ||
| if (productError) { | ||
| return Astro.redirect('/404'); | ||
| } | ||
| // Get a single product with a custom query (if supported by your loader) using a filter object | ||
ascorbic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { entry: productBySlug } = await getLiveEntry('products', { slug: Astro.params.slug }); | ||
| const { Content } = await render(product); | ||
| --- | ||
| <h1>{product.data.title}</h1> | ||
| <Content /> | ||
| ``` | ||
|
|
||
| #### Upgrading from experimental live collections | ||
|
|
||
| If you were using the experimental feature, you must remove the `experimental.liveContentCollections` flag from your `astro.config.*` file: | ||
|
|
||
| ```diff | ||
| export default defineConfig({ | ||
| // ... | ||
| - experimental: { | ||
| - liveContentCollections: true, | ||
| - }, | ||
| }); | ||
| ``` | ||
ascorbic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| No other changes to your project code are required as long as you have been keeping up with Astro 5.x patch releases, which contained breaking changes to this experimental feature. If you experience problems with your live collections after upgrading to Astro v6 and removing this flag, please review the [Astro CHANGELOG from 5.10.2](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md#5102) onwards for any potential updates you might have missed, or follow the [current v6 documentation for live collections](https://docs.astro.build/en/guides/content-collections/). | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.