AgentSkillsCN

astro

Astro框架的岛屿架构模式、客户端指令(client:load、client:visible、client:idle、client:only、client:media)、集成方案(MDX、Tailwind、React/Vue/Svelte)、视图过渡API,以及部署策略。在构建内容导向型站点、实施岛屿部分水合、搭建多框架项目、默认以零JS优化性能,或部署静态/SSR站点时使用此功能。

SKILL.md
--- frontmatter
name: astro
description: >
  Astro framework patterns for Islands Architecture, client directives (client:load, client:visible, client:idle, client:only, client:media), integrations (MDX, Tailwind, React/Vue/Svelte), View Transitions API, and deployment strategies.
  Use when building content-focused sites, implementing partial hydration with islands, setting up multi-framework projects, optimizing performance with zero-JS by default, or deploying static/SSR sites.

Astro Framework

Astro 5.x framework for building fast, content-focused websites with Islands Architecture and zero JavaScript by default.

Core Principles

1. Zero JavaScript by Default

Astro ships ZERO JavaScript unless you explicitly add a client directive.

astro
<!-- Static HTML (no JS) -->
<Header />
<Footer />

<!-- Interactive island (JS loaded) -->
<Counter client:idle />

Rule: Only use client directives when components need hooks, state, or browser APIs.

2. Client Directives

DirectiveWhen to UseBehavior
client:loadCritical interaction needed immediatelyHydrates on page load
client:idleNon-critical interactivityHydrates when browser idle
client:visibleBelow the fold contentHydrates when scrolled into view
client:media="(query)"Responsive componentsHydrates when media query matches
client:only="framework"Breaks during SSRSkips SSR, client-only render

Decision tree: Needs immediate interaction? → client:load | Non-critical? → client:idle | Below fold? → client:visible | Only on mobile/desktop? → client:media | Breaks SSR? → client:only

3. Multi-Framework Support

Mix React, Vue, Svelte in the same project:

bash
bun astro add react vue svelte
astro
---
import ReactForm from './Form.jsx';
import VueChart from './Chart.vue';
import SvelteCounter from './Counter.svelte';
---
<ReactForm client:idle />
<VueChart client:visible />
<SvelteCounter client:load />

Quick Start

bash
bun create astro@latest my-project
cd my-project
bun astro add tailwind react
bun dev

Project Structure

code
src/
├── components/       # .astro, .jsx, .vue, .svelte
├── layouts/          # Page layouts
├── pages/            # File-based routing
│   └── blog/[slug].astro
├── content/          # Content Collections (Markdown/MDX)
│   └── blog/         # Collection folders
├── styles/           # Global CSS
└── content.config.ts # Content collections schema (Astro 5+)

Configuration

javascript
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import react from '@astrojs/react';

export default defineConfig({
  site: 'https://example.com',
  output: 'static',  // or 'server'
  integrations: [tailwind(), react()],
});

Output modes (Astro 5):

  • static - Blog, docs, marketing (default). Use export const prerender = false for SSR pages.
  • server - All pages SSR (requires adapter)

Astro 5 Change: output: 'hybrid' was removed. The hybrid behavior is now the default in static mode.


View Transitions

Enable SPA-like navigation with the Client Router:

astro
---
import { ClientRouter } from 'astro:transitions';
---
<html>
  <head>
    <ClientRouter />
  </head>
  <body><slot /></body>
</html>

Note: In Astro 5, ViewTransitions was renamed to ClientRouter.


Commands

bash
bun dev                  # Start dev server (localhost:4321)
bun build                # Build for production
bun preview              # Preview production build
bun astro check          # TypeScript checks

Core Patterns

Organized by priority - load on-demand as needed:

High Priority (Common Use Cases)

Medium Priority (Specific Features)

Low Priority (Advanced)


Real-World Recipes

Complete examples for common scenarios:

  • Blog Setup - Full blog with tags, RSS, layouts
  • Multi-Step Forms - React form with API integration
  • Authentication - Cookie-based auth with protected routes
  • SEO - Open Graph, Twitter Cards, structured data
  • i18n - Multi-language support
  • Dark Mode - Theme toggle with localStorage

External Resources