# AGENTS.md：Astro静态站点规则

> 一份即插即用的AGENTS.md文件，用于Astro静态站点，确保AI代理在栈内运行，防止SSR漂移，并强制执行内容集合约定。

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

---

将此文件放入你的仓库根目录，命名为`AGENTS.md`。面向Astro的代理（Cursor、Claude Code、Codex）将在启动时自动读取它，并在每次代码生成会话中保持符合约定。

## AGENTS.md

```md title="AGENTS.md"
# Project Rules — Astro Static Site

## Stack
- Astro 5 (static output, `output: "static"`). No SSR adapter unless explicitly requested.
- TypeScript strict mode. All `.astro` component scripts are TypeScript.
- Tailwind CSS v4. No CSS-in-JS. No inline `style` attributes unless value is truly dynamic.
- Content managed via Astro Content Collections (Zod schemas in `src/content.config.ts`).

## Hard rules
- Never switch `output` to `"server"` or `"hybrid"` — this is a static build.
- Never add `client:load` to a component that does not require browser interactivity.
  Prefer `client:idle` or `client:visible` when a client directive is genuinely needed.
- All content files live in `src/content/<collection>/`. Do not create ad-hoc markdown
  files outside a defined collection.
- Frontmatter must match the Zod schema in `src/content.config.ts`. Validate before
  writing — wrong field names cause build failures, not runtime errors.
- Images must go through Astro's `<Image />` component or `getImage()` helper.
  Never use bare `<img>` tags — they bypass optimisation and break CLS scores.
- Never read environment variables with `import.meta.env` inside a `.ts` utility that
  may be imported on the client; mark those files with a `// server-only` comment and
  import them only from `.astro` files or API endpoints.
- Do not add a dependency without stating the reason. Prefer Astro-native solutions
  (content collections, view transitions, image optimisation) before reaching for npm.

## Conventions
- Components live in `src/components/`. Page layouts live in `src/layouts/`.
  One component per file; file name matches the exported component name.
- Route pages live under `src/pages/`. Dynamic routes use bracket notation:
  `src/pages/[slug].astro`. Static paths are generated via `getStaticPaths()`.
- Shared TypeScript utilities live in `src/lib/`. Import with the `@/` alias
  (configured in `tsconfig.json`).
- Tailwind classes follow mobile-first order: base → `sm:` → `md:` → `lg:`.
  No arbitrary values unless a design token does not exist.
- Every page component must export a `<meta>` block via the layout that includes
  `title`, `description`, and Open Graph tags.

## SEO conventions
- Every `src/pages/*.astro` page must render a canonical `<link rel="canonical">`.
- Blog / content pages must include `datePublished` and `dateModified` in
  JSON-LD structured data.
- Do not create two pages that share the same `<title>` or `<meta name="description">`.

## Definition of done
- `astro check` passes with zero errors.
- `astro build` completes without warnings.
- Lighthouse performance score ≥ 90 on a representative page (run `unlighthouse`).
- No new `any` types. No unused imports.
- All new content frontmatter passes the Zod schema (`bun run typecheck`).
```

## 为什么制定这些规则

- **“绝不切换输出到服务器”** 是静态Astro站点中杠杆率最高的规则。当代理发现缺少某个功能时，常常会建议添加SSR适配器作为快速修复——但这会悄然改变部署目标，破坏CDN缓存，并增加冷启动延迟。这条规则强制代理在静态范式内解决问题。
- **“图片必须通过 `<Image />` 处理”** 消除了内容站点上最常见的AI错误：使用裸`<img>`标签会拖累核心网页指标。Astro的图片管线会自动处理格式转换、响应式`srcset`和懒加载——如果不明确告知，代理就会绕过它。
- **Zod前置元数据验证** 在类型检查时捕获模式不匹配，而不是在构建时，更糟糕的情况是静默渲染错误数据。编写markdown文件的代理常常会幻觉出错误的前置元数据字段名。

## 适用场景

- 基于Astro构建的营销网站、博客、文档站点和SEO内容站点，具有固定的静态输出目标。

## 不适用场景

- 使用`output: "server"`或`"hybrid"`且带有边缘/SSR适配器的Astro项目——这些项目需要不同的规则集，允许`client:load`和服务端数据获取模式。