Scripts2026年7月14日·1 分钟阅读

Contentlayer — Turn Content into Type-Safe Data for Next.js

Contentlayer transforms your Markdown, MDX, or CMS content into type-safe JSON data with auto-generated TypeScript definitions, making content as easy to use as a local database.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Contentlayer
直接安装命令
npx -y tokrepo@latest install e17ac78a-7f3c-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Contentlayer validates your content files (Markdown, MDX, JSON, YAML) against a schema you define, then generates typed JSON data and TypeScript definitions. It integrates deeply with Next.js so you can import content like a local module with full IntelliSense.

What Contentlayer Does

  • Reads Markdown, MDX, JSON, and YAML files from a content directory and validates against your schema
  • Generates .contentlayer output with typed JSON data and auto-generated TypeScript definitions
  • Provides a Next.js plugin for seamless integration with hot reload on content changes
  • Supports computed fields for transforming content at build time (e.g., reading time, slug generation)
  • Handles MDX compilation with customizable remark and rehype plugin chains

Architecture Overview

Contentlayer runs as a build step that watches your content directory. When files change, it parses each document, validates fields against your defineDocumentType schema, compiles MDX if needed, and writes typed JSON to a .contentlayer/generated folder. A TypeScript definition file is auto-generated so imports are fully typed. The Next.js plugin hooks into the dev server for live reload.

Self-Hosting & Configuration

  • Install contentlayer2 and the Next.js plugin as dev dependencies
  • Create a contentlayer.config.ts with document type definitions and field schemas
  • Wrap your Next.js config with withContentlayer() for automatic integration
  • Store content files in the configured contentDirPath (e.g., ./posts or ./docs)
  • Customize MDX processing with remark/rehype plugins in the makeSource options

Key Features

  • Full TypeScript support: auto-generated types for every document type and field
  • Live reload: content changes trigger immediate rebuilds during development
  • Computed fields: derive values like reading time, URL slug, or table of contents at build time
  • MDX support with configurable remark/rehype plugins for custom transformations
  • Incremental builds: only re-processes changed content files for fast iteration

Comparison with Similar Tools

  • next-mdx-remote — Renders MDX at runtime; Contentlayer pre-compiles at build time with full type safety
  • Fumadocs — Documentation framework with built-in search; Contentlayer is a general-purpose content SDK
  • Velite — Similar typed content layer; Contentlayer has deeper Next.js integration and a larger community
  • Sanity / Contentful — Cloud CMS platforms; Contentlayer works with local files, giving you Git-based version control

FAQ

Q: Is Contentlayer still maintained? A: The original contentlayer package is no longer actively maintained. The community fork contentlayer2 continues development and is compatible with recent Next.js versions.

Q: Does it work with frameworks besides Next.js? A: Contentlayer is designed primarily for Next.js. The core content processing works standalone, but the plugin ecosystem and hot reload are Next.js-specific.

Q: How does it handle images in Markdown? A: Images referenced in Markdown are not processed by Contentlayer directly. Use next/image with a remark plugin to optimize images, or store them in the public directory.

Q: Can I use it with a headless CMS? A: Contentlayer is designed for file-based content. For CMS integration, consider using the CMS's API directly or tools like Sanity's GROQ with their own type generation.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产