Skip to content

Conversation

albertchae
Copy link
Contributor

@albertchae albertchae commented Mar 10, 2025

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 files

    Auto-generating collections for folders in "src/content/" that are not defined as collections.
    This is deprecated, so you should define these collections yourself in "src/content.config.ts".
    The following collections have been auto-generated: blog, community, download, explore, landing, landing-features, learning-manuals, menus, research, sponsors, teams
    
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/community"
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/explore"
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/landing"
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/menus"
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/sponsors"
    08:35:29 [WARN] [glob-loader] No files found matching "**/*{.md,.mdx},!**/_*/**/*{.md,.mdx},!**/_*{.md,.mdx}" in directory "src/content/research"
    
  • replace 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 in getStaticPaths

  • 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.

  • 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

    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.

@nixos-discourse
Copy link

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

@albertchae albertchae force-pushed the 1611-astro-5-content-layer-api branch from 3b27fe3 to 7ef3833 Compare March 17, 2025 17:39
Copy link
Contributor

@albertchae albertchae force-pushed the 1611-astro-5-content-layer-api branch from 7ef3833 to 0fd6b5b Compare March 18, 2025 11:27
Copy link
Contributor

loader: glob({ pattern: '**/[^_]*.yaml', base: './src/content/menus' }),
});

// TODO get `research` from astro collection instead of hardcoded json
Copy link
Contributor Author

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

const papers: ReadonlyArray<Paper> = [
instead of the content collection. I see some discussion about this in a related PR #1602

Copy link
Collaborator

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

Comment on lines +5 to +14
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}',
Copy link
Contributor Author

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

Copy link
Collaborator

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')
Copy link
Contributor Author

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]

@albertchae albertchae changed the title WIP to migrate to Astro 5 content layer API Migrate to Astro 5 content layer API Mar 18, 2025
@albertchae albertchae marked this pull request as ready for review March 18, 2025 11:38
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)
@albertchae albertchae force-pushed the 1611-astro-5-content-layer-api branch from 0fd6b5b to 1b90683 Compare March 18, 2025 11:38
Copy link
Contributor

Copy link
Collaborator

@thilobillerbeck thilobillerbeck left a 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! :)

@thilobillerbeck thilobillerbeck merged commit 1dc4ce0 into main Mar 18, 2025
3 checks passed
@thilobillerbeck thilobillerbeck deleted the 1611-astro-5-content-layer-api branch March 18, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate to Content Layer API
3 participants