1 条规则
# Astro Best Practices
## Core Principles
- Prefer static generation (SSG) over server-side rendering when possible
- Use partial hydration with `client:*` directives only when JavaScript is required
- Keep client-side JavaScript minimal for optimal performance
- Follow Astro's file-based routing conventions
## Project Structure
```
src/content.config.ts # Build-time content collection definitions
src/
├── components/ # Reusable UI components (.astro, .tsx, .vue)
├── layouts/ # Page layouts with common structure
├── pages/ # File-based routes
├── content/ # Content entries (Markdown/MDX)
├── styles/ # Global styles
└── assets/ # Processed assets (images, fonts)
public/ # Static assets (served as-is)
```
## Components
- Use `.astro` components for static content (zero JS by default)
- Reserve framework components (React, Vue, Svelte) for interactive features
- Apply client directives strategically:
- `client:load` - Critical interactivity needed immediately
- `client:idle` - Lower priority, load when browser is idle
- `client:visible` - Load when component enters viewport
- `client:media` - Load based on media query
- `client:only` - Skip server render entirely
## Content Collections
- Define collections in `src/content.config.ts`
- Use loaders and Zod schemas for type-safe content
- Use `getCollection()` and `getEntry()` for querying
- Use `render(entry)` instead of relying on legacy `entry.render()`
- Leverage frontmatter for metadata
- Prefer MDX for content requiring components
## Performance
- Use `<Image />` component for automatic optimization
- Implement view transitions and client routing with `<ClientRouter />` when appropriate
- Minimize layout shifts with proper image dimensions
- Use content collections for large content sites
## Accessibility
- Use semantic HTML elements (`<nav>`, `<main>`, `<article>`, etc.)
- Ensure keyboard navigation works for all interactive elements
- Provide alt text for images (or `alt=""` for decorative images)
- Maintain proper heading hierarchy (h1 → h6)
- Test with screen readers and keyboard-only navigation
## Data Fetching
- Fetch data in component frontmatter (server-side by default)
- Use `Astro.props` for component data passing
- Implement `getStaticPaths()` for dynamic routes in SSG mode
- Consider API routes for server-side endpoints
## Styling
- Scoped styles are default in `.astro` components
- Use `is:global` directive sparingly for global styles
- Prefer CSS custom properties for theming
- Support dark mode with `prefers-color-scheme` or class-based toggle
## Common Patterns
```astro
---
// Frontmatter: runs at build time (SSG) or request time (SSR)
import Layout from '../layouts/Layout.astro'
import { getCollection } from 'astro:content'
const posts = await getCollection('blog')
---
<Layout title="Blog">
<main>
{posts.map(post => (
<article>
<h2>{post.data.title}</h2>
</article>
))}
</main>
</Layout>
<style>
/* Scoped by default */
article {
padding: 1rem;
}
</style>
```相关插件
filestack↓ 6
Filestack 官方插件,可直接在 Cursor 中上传文件、构建转换并生成安全策略。Archcore↓ 0
让 AI agent 按照你项目的架构、规则和决策来编写代码。cloudinary↓ 0
在 Cursor 中直接使用 Cloudinary,通过自然对话管理资源、应用转换、优化媒体等。Lightrun↓ 16
Lightrun 调试 Skill 与 MCP 集成,用于调查生产环境运行时问题。Redmine MCP↓ 16
将 Cursor 连接到 Redmine,通过 MCP 拉取带完整上下文、评论与附件的 issue。Membase↓ 9
为 Cursor 提供持久长期记忆,连接 Membase MCP 实现混合向量检索与知识图谱,无需 CLI 或 API key。wopee-mcp↓ 4
用于 Wopee.io 自主端到端测试的 MCP server,分析 Web 应用、生成并执行基于 Playwright 的功能测试并校验结果。Askable↓ 3
面向 Askable UX 研究平台的 Cursor 插件,通过 MCP 接入。