-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 Layer #11360
Content Layer #11360
Conversation
🦋 Changeset detectedLatest commit: fdd7b83 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
* wip * wip * wip * Update demo * Add meta * wip * Add file loader * Add schema validation * Remove log * Changeset * Format * Lockfile * Fix type * Handle loading for data store JSON * Use rollup util to import JSON * Fix types * Format * Add tests * Changes from review
* wip * wip * wip * Update demo * Add meta * wip * Add file loader * Add schema validation * Remove log * Changeset * Format * Lockfile * Fix type * Handle loading for data store JSON * Use rollup util to import JSON * Fix types * Format * Add tests * Changes from review * Sync content layer in dev
* fix: watch for content layer changes * Add test
* wip * Add simple loader * Fix type guard * Tighten loader schema * Add loader function to type
* feat: add glob loader * Enable watching and fix paths * Store the full entry object, not just data * Add generateId support * Fix test * Rename loaders to sync * Refacctor imports * Use getEntry * Format * Fix import * Remove type from output * Windows path * Add test for absolute path * Update lockfile * Debugging windows * Allow file URL for base dir * Reset time limit
* feat: add glob loader * Enable watching and fix paths * Store the full entry object, not just data * Add generateId support * Fix test * Rename loaders to sync * Refacctor imports * Use getEntry * Format * Fix import * Remove type from output * Windows path * Add test for absolute path * Update lockfile * Debugging windows * Allow file URL for base dir * Reset time limit * wip: add markdown rendering to content layer * use cached entries * CLean up types * Instrument more of the build * Add digest helper * Add comments * Make image extraction work * feat: image support for content layer (#11469) * wip * wip * Add image to benchmark * Stub assets if missing * Resolve assets in data * Ignore virtual module * Format * rm log * Handle images when using cached data * Fix CCC * Add a comment * Changes from review * Format * Use relative paths for asset files * Pass all md props to getImage * Ensure dotastro dir exists * Fix tests * Changes from review * Don't use temp array in getcollection * Add error handling * Format * Handle paths that are already relative * Dedupe sync runs * Fix syncing in dev * Changes from review * Windows paths ftw * feat(content-layer): support references in content layer (#11494) * Support references in content layer * Fix utf8 rendering * Warn for invalid entries * Fix test * lol windows paths * Remove assertion
* Update content layer flag docs * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * More markdoc * Typo * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Add changeset for content layer experimental release * Update changeset * Update .changeset/smooth-chicken-wash.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* feat: make inline config 1st arg * fix: run config done in sync * feat: start working on injectTypes * feat: write files * feat: adapt core features * feat: migrate db to injectTypes * feat: special db handling * feat: update settings instead of workarounds * fix: create dotAstroDir * feat: refactor sync tests * fix: path * fix: paths * chore: add comments * feat: overwrite content file if exists * chore: remove unused db env related code * feat: use dotAstroDir for settings * chore: simplify astro env sync * feat: use dotAstroDir for preferences * feat: handle db in integration api * chore: reorganize * feat: format * feat: add test * Discard changes to examples/basics/astro.config.mjs * Discard changes to examples/basics/package.json * Discard changes to pnpm-lock.yaml * chore: remove test files * feat: update examples dts * fix: dts * chore: changesets * fix: indentation * Apply suggestions from code review Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Apply suggestions from code review Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * chore: format * Update packages/astro/src/integrations/hooks.ts * Update .changeset/mean-horses-kiss.md * feat: remove formatting * feat: handle fs errors * feat: remove astro:db special path handling * feat: add fs error * Update packages/astro/src/content/types-generator.ts * Update .changeset/mean-horses-kiss.md * Update errors-data.ts * Update .changeset/mean-horses-kiss.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/mean-horses-kiss.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
* feat: add type to infer input type of collection * refactor: * feat: generate json schema for content too * feat: generate a manifest of all the collections * refactor: unnecessary type * fix: only add content collections to manifest * chore: changeset * fix: generate file URLs * fix: flag it properly * fix: save in lower case * docs: add jsdoc to experimental option * nit: move function out * fix: match vscode flag name * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update serious-pumas-run.md * test: add tests * Add content layer support * Apply suggestions from code review * fix: test * Update .changeset/serious-pumas-run.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Apply suggestions from code review * Remove check for json --------- Co-authored-by: Matt Kane <m@mk.gg> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* fix: code component was missing support for meta string Fixed #11604 * Create odd-buttons-pay.md * <Code>: add reference link for meta prop * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/odd-buttons-pay.md * Update .changeset/odd-buttons-pay.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
* wip * done i think * Add changeset * Use hook instead * Reorder hooks [skip ci] * Update .changeset/eleven-pens-glow.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Fix run * Fix link * Add link Co-authored-by: Sarah Rainsberger <sarah11918@users.noreply.github.com> * More accurate migration [skip ci] --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Sarah Rainsberger <sarah11918@users.noreply.github.com>
* wip * done * Add changeset * Format * Update * Fix houston * Fix test * Fix test
message: (err as Error).message, | ||
...AstroErrorData.ContentLayerWriteError, | ||
}); | ||
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: err }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Princesseuh Will this surface the actual FS error message to the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the cause
, yes! It's shown in both our CLI and our overlay, though the styling isn't quite amazing
This is the work-in-progress branch for the Content Layer
The Content Layer API is a new way to handle content and data in Astro. It builds upon content collections, taking them beyond local files in
src/content
and allowing you to fetch content from anywhere, including remote APIs, or files anywhere in your project. As well as being more powerful, the Content Layer is designed to be more performant, helping sites scale to thousands of pages. Data is cached between builds and updated incrementally. Markdown parsing is also 5-10x faster, with similar scale reductions in memory. While the feature is experimental and subject to breaking changes, we invite you to try it today and let us know how it works for you.Enabling content layer
To enable, add the
contentLayer
flag to theexperimental
object in your Astro config:Using the content layer
Tip
The content layer APIs are based on content collections, so you can learn more about them in the content collection docs. Any differences are highlighted below.
To use content layer, create a collection in
src/content/config.ts
with aloader
property. For local files where there is one entry per file, use theglob()
loader. You can put your content files anywhere, but not insrc/content
because these would be handled by the current content collections instead. In this example the files are insrc/data
.You can load multiple entries from a single JSON file using the
file()
loader. In this case the data must either be an array of objects, which each contain anid
property, or an object where each key is the ID.Array syntax:
Object syntax:
The collection is then defined using the
file()
loader:The collection can be queried in the same way as content collections:
Rendering content
Entries generated from markdown or MDX can be rendered directly to a page using the
render()
function.Warning
The syntax for rendering content layer collections is different from existing content collections syntax.
Creating a content layer loader
Content layer loaders aren't restricted to just loading local files. You can also use loaders to load or generate content from anywhere. The simplest type of loader is an async function that returns an array of objects, each of which has an
id
:For more advanced loading logic, you can define an object loader. This allows incremental updates and conditional loading, and gives full access to the data store. See the API in the draft RFC.
Migrating a markdown or MDX collection to content layer
If you have used content collections, content layer will be very familiar as most of the APIs are the same. You can try converting an existing content collection to content layer if it uses markdown, MDX or JSON, with these steps:
src/content
. This is so it won't be handled as a current content collection. This example assumes the content has been moved tosrc/data
. Theconfig.ts
file must remain insrc/content
.type
set, and needs aloader
defined.slug
toid
. Content layer collections do not have aslug
field. You should useid
instead, which has the same syntax.render()
function. Entries no longer have arender()
method, as they are now serializable plain objects. Instead, import therender()
function fromastro:content
.The
getEntryBySlug
andgetDataEntryByID
functions are deprecated and cannot be used with content layer collections. Instead, usegetEntry
, which is a drop-in replacement for both.Learn more
To see the full API look at the draft RFC and share your feedback on the feature and API.