Switch to Content Collections
How to switch to Content Collections.
It’s possible to switch to Content Collections to generate type-safe data collections from MDX files. This approach provides a structured way to manage blog posts while maintaining full type safety throughout your application.
1. Swap out the required dependencies
Remove the existing dependencies…
… and install the new dependencies…
2. Update the .gitignore
file
Add .content-collections
to the .gitignore
file:
3. Modify the CMS package scripts
Now we need to modify the CMS package scripts to replace the basehub
commands with content-collections
.
We’re using the Content Collections CLI directly to generate the collections prior to Next.js processes. The files are cached and not rebuilt in the Next.js build process. This is a workaround for this issue.
4. Modify the relevant CMS package files
5. Update the sitemap.ts
file
Update the sitemap.ts
file to scan the content
directory for MDX files:
6. Create your collections
Create a new content collections file in the cms
package, then import the collections in the web
package.
title
field to _title
and the _meta.path
field to _slug
to match the default next-forge CMS.7. Create your content
To create a new blog post, add a new MDX file to the apps/web/content/blog
directory. The file name will be used as the slug for the blog post and the frontmatter will be used to generate the blog post page. For example:
The same concept applies to the legal
collection, which is used to generate the legal policy pages. Also, the image
field is the path relative to the app’s root public
directory.
8. Remove the environment variables
Finally, remove all instances of BASEHUB_TOKEN
from the @repo/env
package.
9. Bonus features
Fumadocs MDX Plugins
You can use the Fumadocs MDX plugins to enhance your MDX content.
Reading Time
You can calculate reading time for your collection by adding a transform function.
Low-Quality Image Placeholder (LQIP)
You can generate a low-quality image placeholder for your collection by adding a transform function.