# Prompt-to-PR：使用 Astro 构建静态 SEO 网站

> 从零开始使用 Astro、Tailwind、MDX 内容集合、站点地图和规范标签搭建内容驱动的静态 SEO 网站的完整 SOP。

**Type:** Playbook  
**Tools:** Cursor, Claude Code, Codex, Windsurf  
**Stack:** Astro, TypeScript, Tailwind  
**Difficulty:** medium  
**Updated:** 2026-06-08

---

搭建一个生产就绪、完全静态的 Astro 5 网站，针对自然搜索进行优化：内容集合、Tailwind 排版、`@astrojs/sitemap`，以及从一开始就配置正确的 `<head>` 元数据。

## 1. 需求

构建一个在 Lighthouse SEO 和可访问性上获得 100 分的静态营销/内容网站。内容使用由 Astro 内容集合管理的 MDX 文件编写。无需 JavaScript 框架——输出是纯 HTML+CSS，带有可选岛屿。

## 2. 初始提示

```txt title="First Prompt"
Scaffold a new Astro 5 static SEO site from scratch in the current directory.

Requirements:
1. Init with: `bunx create astro@latest . --template minimal --typescript strict --no-git`
2. Add integrations:
   - `@astrojs/tailwind` with `@tailwindcss/typography`
   - `@astrojs/sitemap`
   - `@astrojs/mdx`
3. Create a content collection `blog` in `src/content/blog/` with this Zod schema:
   title, description, pubDate (Date), updatedDate (Date, optional),
   author (string, default "Admin"), tags (string[], default []), draft (bool, default false).
4. Create a BaseLayout.astro with:
   - A `<head>` block: charset, viewport, canonical (`Astro.url.href`),
     og:title, og:description, og:url, og:type, twitter:card.
   - Accept `title`, `description`, `image` props.
5. Create pages:
   - `/` — hero + last 6 non-draft posts
   - `/blog` — paginated list (10 per page) using `paginate()`
   - `/blog/[slug]` — single post rendered with `<Content />`
   - `/tags/[tag]` — posts filtered by tag
6. Create `src/content/blog/hello-world.mdx` as a real sample post.
7. Configure `astro.config.ts`:
   - `site: process.env.SITE_URL ?? "http://localhost:4321"`
   - `integrations: [tailwind(), sitemap(), mdx()]`
   - `output: "static"`
8. Add a `.env.example` with `SITE_URL=https://example.com`.
```

## 3. 预期文件变更

```txt
astro.config.ts
tailwind.config.ts
src/content.config.ts                    (blog collection schema)
src/layouts/BaseLayout.astro             (head + canonical + OG tags)
src/pages/index.astro
src/pages/blog/index.astro               (paginated)
src/pages/blog/[slug].astro
src/pages/tags/[tag].astro
src/content/blog/hello-world.mdx
.env.example
package.json                             (updated with all integrations)
```

## 4. 检查清单

- `astro.config.ts` 设置了 `site`——这是 `@astrojs/sitemap` 生成绝对 URL 所必需的。
- `BaseLayout.astro` 使用 `Astro.url.href` 输出 `<link rel="canonical">`。
- 博客列表页面使用来自 `paginate()` 的 `Astro.props.page.data`——而不是直接调用 `getCollection()`。
- 草稿文章（`draft: true`）在生产环境中通过 `import.meta.env.PROD` 守卫排除。
- `<html lang="en">` 设置在根元素上。
- `tailwind.config.ts` 包含 `typography` 插件，并针对 `src/**/*.{astro,mdx}`。
- 存在 `sitemap()` 集成——在 `bun run build` 后验证 `dist/sitemap-index.xml` 是否存在。

## 5. 测试命令

```bash
bun install
bun run dev
# visit http://localhost:4321 and confirm hero + posts render

bun run build
# expect zero errors

bun run preview
# check /sitemap-index.xml and /sitemap-0.xml exist
# check /blog and /blog/hello-world render with correct <title> and canonical

# Lighthouse CLI smoke test (optional)
bunx lighthouse http://localhost:4321 --output json --quiet | jq '.categories.seo.score'
# expect 1 (100%)
```

## 6. 常见失败

- **站点地图生成相对 URL**——`astro.config.ts` 中缺少 `site`。请添加它。
- **`getCollection` 在生产环境中返回草稿**——过滤：`posts.filter(p => !p.data.draft || !import.meta.env.PROD)`。
- **`/blog` 上的分页 404**——第一页必须是 `/blog/`（索引），而不是 `/blog/1`。Astro 的 `paginate()` 默认输出 `/blog/`、`/blog/2/` 等。
- **MDX 内容未应用样式**——在 `[slug].astro` 的文章包装器上缺少 `@tailwindcss/typography` 的 `prose` 类。
- **OG 标签使用了相对图片路径**——必须是绝对 URL。使用 `Astro.site` 作为前缀。

## 7. 修复提示

```txt title="Fix Prompt"
The sitemap at /sitemap-0.xml contains relative paths like "/blog/hello-world"
instead of absolute URLs like "https://example.com/blog/hello-world".

Fix: add `site: "https://example.com"` (or `process.env.SITE_URL`) to the
top-level astro.config.ts object. The sitemap integration reads this value
to prefix all URLs.
```

## 8. PR 描述

```md title="PR description"
## Init: Static Astro 5 SEO site

- Astro 5 + TypeScript strict + Tailwind (with `@tailwindcss/typography`)
- `@astrojs/sitemap` and `@astrojs/mdx` integrations
- Content collection `blog` with Zod schema (title, description, pubDate, tags, draft)
- BaseLayout with canonical, OG, and Twitter Card meta tags
- Pages: home, paginated blog list, single post, tag archive
- Sample `hello-world.mdx` post
- `bun run build` emits `sitemap-index.xml` and all static pages
```