Astro content sites

From findscripter/everything-skills · View interactive version

Astro content sites is an AI agent skill from the GitHub repository findscripter/everything-skills. 当构建博客/文档/营销页/作品集等内容密集站点且追求 Core Web Vitals 时使用;用 Astro 岛屿架构产出默认零 JS 的 SSG/SSR 站点(含内容集合、按需水合、多框架组件);不适用于重交互的 SPA 或全栈应用主体;触发词:astro、.astro 文件、client: 指令、content collections、Astro.props、岛屿架构。

How to use this skill

Skill file

何时使用

不该用的边界:

步骤

  1. 初始化项目:npm create astro@latest my-site → cd my-site → npm install → npm run dev。
  2. 按需加集成:npx astro add tailwind | react | mdx | sitemap | vercel(分别对应 Tailwind、React 组件、MDX、自动 sitemap.xml、Vercel SSR 适配器)。
  3. 遵循目录约定:src/pages/(基于文件的路由)、src/layouts/(页面壳)、src/components/、src/content/(类型安全内容集合)、src/styles/、public/(原样拷贝的静态资源)、astro.config.mjs。
  4. 写 .astro 组件:顶部 --- 代码栅栏只在服务端运行,下方是模板;<style> 默认作用域隔离。
  5. 配路由:index.astro→/,[slug].astro→动态,[...path].astro→兜底;静态模式下动态路由必须导出 getStaticPaths。
  6. 用内容集合管理 Markdown/MDX:在 src/content/config.ts 用 defineCollection + zod schema 定义,页面里 getCollection 读取。
  7. 岛屿水合:UI 框架组件默认渲染为静态 HTML,加 client: 指令才发送 JS 并变交互。
  8. 需要动态时在 astro.config.mjs 设 output: 'static' | 'server' | 'hybrid' 并配适配器;个别页面用 export const prerender = false 选入 SSR。

指令

# 创建并启动
npm create astro@latest my-site
cd my-site && npm install && npm run dev

# 按需添加集成
npx astro add tailwind        # Tailwind CSS
npx astro add react           # React 组件支持
npx astro add mdx             # MDX 支持
npx astro add sitemap         # 自动 sitemap.xml
npx astro add vercel          # Vercel SSR 适配器

client: 水合指令(核心约束,按需取用):

示例

.astro 组件 + 类型化 props(栅栏只在服务端运行):

---
// src/components/Card.astro
interface Props { title: string; href: string; description: string; }
const { title, href, description } = Astro.props;
---
<article class="card">
  <h2><a href={href}>{title}</a></h2>
  <p>{description}</p>
</article>
<style>
  /* 自动作用域隔离到本组件 */
  .card { border: 1px solid #eee; padding: 1rem; }
</style>

内容集合定义与动态路由渲染:

// src/content/config.ts
import { z, defineCollection } from 'astro:content';
const blog = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string(),
    date: z.coerce.date(),
    tags: z.array(z.string()).default([]),
    draft: z.boolean().default(false),
  }),
});
export const collections = { blog };
---
// src/pages/blog/[slug].astro
export async function getStaticPaths() {
  const posts = await getCollection('blog');
  return posts.map(post => ({ params: { slug: post.slug }, props: { post } }));
}
const { post } = Astro.props;
const { Content } = await post.render();
---
<h1>{post.data.title}</h1>
<Content />

岛屿水合(同一组件,水合与否行为不同):

---
import Counter from '../components/Counter.tsx';
import VideoPlayer from '../components/VideoPlayer.svelte';
---
<Counter initialCount={0} />                 <!-- 纯静态 HTML,零 JS -->
<Counter initialCount={0} client:load />     <!-- 加载即水合 -->
<VideoPlayer src="/demo.mp4" client:visible /><!-- 进入视口才水合 -->

SSR 配置与 RSS 端点:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({ output: 'hybrid', adapter: vercel() });
// src/pages/rss.xml.ts —— 用内容集合生成 RSS
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
export async function GET(context) {
  const posts = await getCollection('blog');
  return rss({
    title: 'My Blog', description: 'Latest posts', site: context.site,
    items: posts.map(p => ({ title: p.data.title, pubDate: p.data.date, link: `/blog/${p.slug}/` })),
  });
}

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT),原作者 suhaibjanjua。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.