构建 Astro 静态 SEO 站点的提示
用于搭建一个完全优化的 Astro 静态站点的复制粘贴提示,包含内容集合、站点地图、og-images 和 TypeScript。
CursorClaude CodeCodexWindsurf AstroTypeScriptTailwind
将此内容提供给您的代理,以搭建一个生产就绪的 Astro 静态站点,包含内容集合、Tailwind v4、SEO 元标签、站点地图和每页的 OG 图像——无需担心它发明不兼容的集成或跳过规范标签。
主要提示
You are scaffolding a new Astro static site. Use Astro 5, TypeScript strict mode,and Tailwind CSS v4 (via @tailwindcss/vite — NOT the legacy @astrojs/tailwind integration).
Requirements:- Initialize with `bun create astro@latest` defaults, then layer in: - A `src/content/blog/` collection with a Zod schema (title, description, pubDate, tags). - A shared `<BaseLayout>` with `<head>` containing: canonical URL, og:title, og:description, og:image (per-page), twitter:card, and the Astro `<ViewTransitions />` component. - `@astrojs/sitemap` integration configured with `customPages` for any dynamic routes. - A `public/robots.txt` that disallows `/api/` and allows everything else. - A `/blog/[slug].astro` dynamic route that renders MDX blog posts. - A `/tags/[tag].astro` route that filters posts by tag. - Tailwind applied globally via `src/styles/global.css` imported in BaseLayout.- All components must be `.astro` — no React unless explicitly asked.- Export a typed `getStaticPaths` for every dynamic route.- Do NOT install `@astrojs/react`, `next`, or any SSR adapter — this is a static build.
Stop after listing all files you plan to create or modify. Wait for my approval before writing any code.实现说明
- Astro 5 的内容集合使用
src/content.config.ts中的defineCollection,而不是旧的src/content/config.ts路径——确认代理使用了正确的位置。 - Tailwind v4 作为 Vite 插件提供;旧的
astro.config.ts中的integrations: [tailwind()]模式已过时,并会导致运行时错误。 getStaticPaths必须返回{ params, props }对象;代理有时会返回纯字符串。
预期文件更改
astro.config.ts (new)src/content.config.ts (new)src/layouts/BaseLayout.astro (new)src/pages/index.astro (new)src/pages/blog/[slug].astro (new)src/pages/tags/[tag].astro (new)src/styles/global.css (new)public/robots.txt (new)package.json (edited)tsconfig.json (edited)验收标准
bun run build退出代码为 0,且没有类型错误。- 每个博客文章页面在渲染的 HTML 中包含唯一的规范 URL 和
og:image。 - 生成
/sitemap-index.xml并包含所有博客文章 URL。 - 存在
/robots.txt并禁止/api/。
测试命令
bun run buildbun run previewcurl -s http://localhost:4321/sitemap-index.xml | grep '<loc>'curl -s http://localhost:4321/robots.txtbun run typecheck常见的 AI 错误
- 安装已弃用的
@astrojs/tailwind而不是@tailwindcss/vite。 - 将
content.config.ts放在src/content/config.ts(旧的 Astro 4 路径)。 - 忘记在
getStaticPaths中调用getCollection('blog')——返回空数组。 - 在
astro.config.ts中添加output: 'server',这会破坏静态导出。
修复提示
The build failed. Check for these issues in order:1. Tailwind: remove `@astrojs/tailwind` from integrations and add `@tailwindcss/vite` to the `vite.plugins` array in astro.config.ts.2. Content config: the file must be at `src/content.config.ts` (not `src/content/config.ts`).3. `getStaticPaths`: each entry must be `{ params: { slug }, props: { post } }`.Fix only the broken items. Show me the corrected diff.