Skip to content
返回技术栈推荐

Astro 内容站点技术栈

Astro 5 + MDX + Content Collections + Cloudflare Pages,用于高性能内容站点。

静态站点简单

技术栈组成

FrameworkAstro 5
ContentMDX + Content Collections
StylingTailwindCSS 4
SearchPagefind
CMSKeystatic (optional)
DeployCloudflare Pages

为什么

Astro 默认零 JS,Lighthouse 开箱即得 95+ 分。Content Collections 提供类型安全的 frontmatter。

Cloudflare Pages 30 秒部署全球 CDN。完美用于博客、文档和营销站点。

何时使用

选择 Astro 用于内容驱动站点:博客、文档、作品集、营销页面和落地页。避免交互仪表板(用 Next.js)或实时应用。

如何做

1. 脚手架 — npm create astro@latest mysite -- --template blog 2. 添加集成 — npx astro add tailwind mdx 3. Content Collections — src/content/blog/*.mdx 带 schema 验证 4. 编写页面 — src/pages/index.astro, blog/[slug].astro 5. 添加 Pagefind — npm i pagefind,构建后 pagefind --site dist 6. 部署 — npx wrangler pages deploy dist(或关联 GitHub 仓库) 7. 自定义域名 — Cloudflare 面板 > Pages > 自定义域名 8. 分析 — Cloudflare Web Analytics(免费、隐私友好)

示例

// src/content.config.ts — Content Collections schema import { defineCollection, z } from "astro:content"; import { glob } from "astro/loaders"; const blog = defineCollection({ loader: glob({ pattern: "**/*.mdx", base: "./src/content/blog" }), schema: z.object({ title: z.string(), description: z.string(), pubDate: z.coerce.date(), tags: z.array(z.string()).default([]), draft: z.boolean().default(false), }), }); export const collections = { blog }; // src/pages/blog/[slug].astro — 类型安全页面 --- import { getCollection, render } from "astro:content"; export async function getStaticPaths() { const posts = await getCollection("blog", ({ data }) => !data.draft); return posts.map((post) => ({ params: { slug: post.id }, props: { post } })); } const { post } = Astro.props; const { Content } = await render(post); --- <article> <h1>{post.data.title}</h1> <time>{post.data.pubDate.toLocaleDateString()}</time> <Content /> </article>

常见问题

  • ⚠Content collection 未找到 — 忘记在 content.config.ts 导出 collections
  • ⚠MDX 组件不渲染 — 需要导入并传 components 属性给 render()
  • ⚠Pagefind 索引为空 — 构建后运行:astro build && pagefind --site dist
  • ⚠Cloudflare Pages 构建失败 — Node.js 版本不匹配,设置 NODE_VERSION 环境变量

最佳实践

  • ✓使用 Astro Islands — 用 client:visible 添加交互组件,其余保持静态
  • ✓生成 sitemap + RSS — @astrojs/sitemap 和 @astrojs/rss 用于 SEO 和阅读器
  • ✓非技术作者用 Keystatic — 基于 git 的 CMS,无需外部服务
  • ✓Cloudflare 设置缓存头 — 资产长 TTL,HTML 短 TTL

常见问题解答

什么是Astro 内容站点技术栈?适合做什么项目?

Astro 5 + MDX + Content Collections + Cloudflare Pages,用于高性能内容站点。 它通过分层组合提供端到端类型安全与开箱即用的部署体验,适合从 MVP 到生产 SaaS 的快速迭代。

如何用Astro 内容站点技术栈在 10 分钟内跑通一个 demo?

两步起步:① 脚手架 — npm create astro@latest mysite -- --template blog;② 添加集成 — npx astro add tailwind mdx。先跑通本地 dev server,再接一个真实数据源验证端到端链路。

Astro 内容站点技术栈 最常见的坑是什么?怎么避免?

最高频的坑是:Content collection 未找到 — 忘记在 content.config.ts 导出 collections。避免方法:在 PR 模板里加入对应 checklist,CI 中跑类型检查 + lint + 单测,三重把关再合并。

Astro 内容站点技术栈 的最佳实践有哪些?

Astro 内容站点技术栈 的最佳实践包括:使用 Astro Islands — 用 client:visible 添加交互组件,其余保持静态;生成 sitemap + RSS — @astrojs/sitemap 和 @astrojs/rss 用于 SEO 和阅读器;非技术作者用 Keystatic — 基于 git 的 CMS,无需外部服务。其中首条「使用 Astro Islands — 用 client:visible 添加交互组件,其余保持静态」应在项目初始化时就落地,避免后期返工。

什么情况下不该选Astro 内容站点技术栈?

三种情况另选他栈:① 团队对该栈核心框架零经验且工期紧张(学习曲线风险);② 项目是纯内容/营销站(选 Astro 更轻);③ 需要极高吞吐(>1 万 RPS)或强一致微服务(选 Go/Rust)。Astro 内容站点技术栈 的强项是中高复杂度全栈与 API,别在简单静态页上过度工程。