{
  "id": "agents-md-for-astro-static-site",
  "type": "rules",
  "category": "rules",
  "locale": "zh",
  "url": "/zh/rules/agents-md-for-astro-static-site",
  "title": "AGENTS.md：Astro静态站点规则",
  "description": "一份即插即用的AGENTS.md文件，用于Astro静态站点，确保AI代理在栈内运行，防止SSR漂移，并强制执行内容集合约定。",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Astro",
    "TypeScript",
    "Tailwind"
  ],
  "tags": [
    "agents-md",
    "astro",
    "typescript",
    "tailwind",
    "conventions",
    "seo"
  ],
  "difficulty": null,
  "updated": "2026-06-08",
  "markdown": "将此文件放入你的仓库根目录，命名为`AGENTS.md`。面向Astro的代理（Cursor、Claude Code、Codex）将在启动时自动读取它，并在每次代码生成会话中保持符合约定。\n\n## AGENTS.md\n\n```md title=\"AGENTS.md\"\n# Project Rules — Astro Static Site\n\n## Stack\n- Astro 5 (static output, `output: \"static\"`). No SSR adapter unless explicitly requested.\n- TypeScript strict mode. All `.astro` component scripts are TypeScript.\n- Tailwind CSS v4. No CSS-in-JS. No inline `style` attributes unless value is truly dynamic.\n- Content managed via Astro Content Collections (Zod schemas in `src/content.config.ts`).\n\n## Hard rules\n- Never switch `output` to `\"server\"` or `\"hybrid\"` — this is a static build.\n- Never add `client:load` to a component that does not require browser interactivity.\n  Prefer `client:idle` or `client:visible` when a client directive is genuinely needed.\n- All content files live in `src/content/<collection>/`. Do not create ad-hoc markdown\n  files outside a defined collection.\n- Frontmatter must match the Zod schema in `src/content.config.ts`. Validate before\n  writing — wrong field names cause build failures, not runtime errors.\n- Images must go through Astro's `<Image />` component or `getImage()` helper.\n  Never use bare `<img>` tags — they bypass optimisation and break CLS scores.\n- Never read environment variables with `import.meta.env` inside a `.ts` utility that\n  may be imported on the client; mark those files with a `// server-only` comment and\n  import them only from `.astro` files or API endpoints.\n- Do not add a dependency without stating the reason. Prefer Astro-native solutions\n  (content collections, view transitions, image optimisation) before reaching for npm.\n\n## Conventions\n- Components live in `src/components/`. Page layouts live in `src/layouts/`.\n  One component per file; file name matches the exported component name.\n- Route pages live under `src/pages/`. Dynamic routes use bracket notation:\n  `src/pages/[slug].astro`. Static paths are generated via `getStaticPaths()`.\n- Shared TypeScript utilities live in `src/lib/`. Import with the `@/` alias\n  (configured in `tsconfig.json`).\n- Tailwind classes follow mobile-first order: base → `sm:` → `md:` → `lg:`.\n  No arbitrary values unless a design token does not exist.\n- Every page component must export a `<meta>` block via the layout that includes\n  `title`, `description`, and Open Graph tags.\n\n## SEO conventions\n- Every `src/pages/*.astro` page must render a canonical `<link rel=\"canonical\">`.\n- Blog / content pages must include `datePublished` and `dateModified` in\n  JSON-LD structured data.\n- Do not create two pages that share the same `<title>` or `<meta name=\"description\">`.\n\n## Definition of done\n- `astro check` passes with zero errors.\n- `astro build` completes without warnings.\n- Lighthouse performance score ≥ 90 on a representative page (run `unlighthouse`).\n- No new `any` types. No unused imports.\n- All new content frontmatter passes the Zod schema (`bun run typecheck`).\n```\n\n## 为什么制定这些规则\n\n- **“绝不切换输出到服务器”** 是静态Astro站点中杠杆率最高的规则。当代理发现缺少某个功能时，常常会建议添加SSR适配器作为快速修复——但这会悄然改变部署目标，破坏CDN缓存，并增加冷启动延迟。这条规则强制代理在静态范式内解决问题。\n- **“图片必须通过 `<Image />` 处理”** 消除了内容站点上最常见的AI错误：使用裸`<img>`标签会拖累核心网页指标。Astro的图片管线会自动处理格式转换、响应式`srcset`和懒加载——如果不明确告知，代理就会绕过它。\n- **Zod前置元数据验证** 在类型检查时捕获模式不匹配，而不是在构建时，更糟糕的情况是静默渲染错误数据。编写markdown文件的代理常常会幻觉出错误的前置元数据字段名。\n\n## 适用场景\n\n- 基于Astro构建的营销网站、博客、文档站点和SEO内容站点，具有固定的静态输出目标。\n\n## 不适用场景\n\n- 使用`output: \"server\"`或`\"hybrid\"`且带有边缘/SSR适配器的Astro项目——这些项目需要不同的规则集，允许`client:load`和服务端数据获取模式。"
}