Project Structure
Ease
This project's structure is designed to make it extremely easy to add and manage content. We utilize Next.js route groups and MDX to organize our content, along with frontmatter for metadata.
Writing
We organize our content using route groups. This means that when you create a new folder in the posts directory, it automatically creates a new route group for your content.
Adding a New Category
To add a new category:
- Duplicate the guides Folder: Copy the existing guides folder inside the posts directory.
- Rename the Folder: Rename the duplicated folder to your desired category name.
- Add Content: Inside your new category folder, add your content files (usually .mdx files).
This structure allows you to expand the site's content effortlessly without manual route configurations.
For more information on routing in Next.js, refer to the Next.js Routing Documentation.
Components
We use MDX to write our content, which allows us to include React components directly within our markdown files.
Adding a New Component
To add a new component:
- Create the Component: Define your React component as you normally would.
- Register the Component: Add your component to the
mdx-components.tsx
file in the root of the project. This file maps component names to their implementations, making them available in your MDX files.
For more details on using MDX with Next.js, see the Next.js MDX Documentation.
Frontmatter
We use frontmatter to add metadata to our markdown files. This metadata is crucial for categorization, and providing additional context about each post. We've defined an interface for the frontmatter in the types
directory called Post
.
The Post Interface
Here's the Post
interface for reference:
Using Frontmatter in Your Posts
At the top of your .mdx files, include frontmatter enclosed within --- delimiters. Here's an example:
Example of a Complete Frontmatter
Automating MDX Timestamp Updates
To streamline the management of timestamps in your MDX files, we have implemented a script called update-mdx-timestamps.js. This script runs automatically during the next build process and ensures that the created and updated timestamps in your MDX frontmatter are accurate and up-to-date. The script is designed to run automatically during the build process, but you can also run it manually if needed.
Running Manually
Overriding the Created Timestamp
By default, the script does not override the created timestamp if it already exists. To force the script to update the created timestamp for all files, use the --override-created flag:
Integration with Build Process
To ensure the script runs automatically, you can add it to the scripts section of your package.json:
This way, every time you run npm run build, the script will execute before the Next.js build process begins.
Conclusion
This project structure, combined with the use of MDX and frontmatter, provides a powerful and flexible way to manage content in your Next.js application. Whether you're adding new categories, integrating custom components, or enhancing your posts with rich metadata, this setup is designed to scale with your needs.