-
-
Notifications
You must be signed in to change notification settings - Fork 341
Migrate to Astro 5 content layer API #1687
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
Conversation
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2025-03-10-marketing-team-minutes/61466/1 |
3b27fe3
to
7ef3833
Compare
7ef3833
to
0fd6b5b
Compare
loader: glob({ pattern: '**/[^_]*.yaml', base: './src/content/menus' }), | ||
}); | ||
|
||
// TODO get `research` from astro collection instead of hardcoded json |
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.
it looks like research is using this json inside the astro file
nixos-homepage/src/pages/research.astro
Line 227 in c0b1510
const papers: ReadonlyArray<Paper> = [ |
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.
yeah, lets tackle this in the PR you mentioned since it does not hurt content collection migration right now
loader: glob({ pattern: '**/[^_]*.{md,mdx}', base: './src/content/blog' }), | ||
}); | ||
|
||
const community = defineCollection({ | ||
loader: glob({ pattern: '**/[^_]*.yaml', base: './src/content/community' }), | ||
}); | ||
|
||
const download = defineCollection({ | ||
loader: glob({ | ||
pattern: '**/[^_]*.{md,mdx}', |
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.
should we glob every directory on md,mdx,yaml or is this fine? it might be annoying in the future if we add a new filetype to a collection and forget to edit this config
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.
I guess we can fix it then. At the end of the day this prevents devs from inserting incompatible file types without implementation on the rendering side.
.filter( | ||
(team) => team.slug.split('_')[1] === 'foundation-board', | ||
)[0] | ||
.find((team) => team.id.split('_')[1] === 'foundation-board') |
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.
small refactor to use find
instead of filter(...)[0]
Previous content config was autogenerated, but Astro 5 requires it to be defined explicitly. Step 1 under https://docs.astro.build/en/guides/upgrade-to/v5/#updating-existing-collections N.B. `landingFeatures` and `learningManuals` were changed to camel case because I couldn't find a way to kebab case their ids with `defineCollection`
Step 4 from https://docs.astro.build/en/guides/upgrade-to/v5/#updating-existing-collections > Switch to the new render() function. Entries no longer have a render() method, as they are now serializable plain objects. Instead, import the render() function from astro:content.
Step 3 of https://docs.astro.build/en/guides/upgrade-to/v5/#updating-existing-collections N.B. I didn't remove `slug` from the filename of `src/pages/community/teams/[...slug].astro` since we are defining that slug in getStaticPaths (which also seems to match the example from the documentation)
Per https://docs.astro.build/en/guides/upgrade-to/v5/#breaking-changes-to-legacy-content-and-data-collections > Sort order of generated collections is non-deterministic and platform-dependent. This means that if you are calling getCollection(), the order in which entries are returned may be different than before. If you need a specific order, you must sort the collection entries yourself. Except for blog entries which are already being sorted by date, we explicitly sort lexicographically (using [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) because [array sort wants a comparator function that returns negative, 0 or positive to indicate order](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
0fd6b5b
to
1b90683
Compare
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.
Awesome work! Thanks for working on this! :)
Fixes: #1611
Following https://docs.astro.build/en/guides/upgrade-to/v5/#updating-existing-collections, changes of the same type are grouped into commits
turn off legacy.collections and define
src/content.config.ts
. Previously we were getting the content config autogenerated by directory. This is technically still available as a deprecated feature, but it wouldn't work for us anyway because it only globs on md/mdx filesreplace slug with id. Did not do the same for
src/pages/community/teams/[...slug].astro
because it wasn't strictly necessary and the example in the migration guide has an example with[slug]
in the filename getting populated from a collection entry ingetStaticPaths
Switch to the new render() function.
Manually sort collections when necessary. Previously we were relying on collection entries being sorted lexicographically from the directory, but this behavior has now changed. Except for blog entries which are already being sorted by date, we explicitly sort lexicographically (using
localeCompare
because array sort wants a comparator function that returns negative, 0 or positive to indicate order