astro
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAstro
Astro
Overview
概述
Astro is an all-in-one web framework for building fast, content-focused websites. It uses an islands architecture that ships zero JavaScript by default, hydrating only interactive components on demand. Components from React, Svelte, Vue, Solid, and Preact can coexist in a single project.
When to use: Content-driven sites (blogs, docs, marketing), portfolios, e-commerce storefronts, any site where most pages are primarily static with isolated interactive regions.
When NOT to use: Highly interactive single-page applications (dashboards, real-time collaboration tools), apps requiring full client-side routing with shared global state across all components.
Astro 是一款全能型Web框架,用于构建快速、以内容为核心的网站。它采用孤岛架构,默认情况下不发送任何JavaScript,仅按需 hydration 交互式组件。React、Svelte、Vue、Solid和Preact的组件可在同一个项目中共存。
适用场景: 内容驱动型网站(博客、文档、营销站点)、作品集、电商店铺前端,以及大部分页面以静态为主且仅包含孤立交互区域的任何网站。
不适用场景: 高交互性单页应用(仪表盘、实时协作工具)、需要全客户端路由且所有组件共享全局状态的应用。
Quick Reference
快速参考
| Pattern | API / Directive | Key Points |
|---|---|---|
| Content collection | | Zod schemas, glob/file loaders, type-safe queries |
| Query collection | | Returns typed array, supports filter callback |
| Single entry | | Fetch by collection name and entry ID |
| Island (load) | | Hydrate immediately on page load |
| Island (idle) | | Hydrate when browser is idle |
| Island (visible) | | Hydrate when component enters viewport |
| Island (media) | | Hydrate on media query match |
| Island (client-only) | | Skip SSR, render only on client |
| View transitions | | Add to |
| Persist state | | Maintain island state across navigations |
| Programmatic navigate | | Client-side navigation from scripts |
| Static output | | Pre-render all pages at build time (default) |
| Server output | | Server-render all pages on demand |
| Hybrid (opt-in SSR) | | Static by default, opt individual pages into SSR |
| Hybrid (opt-in static) | | SSR by default, opt individual pages into static |
| Server islands | | Defer server rendering for dynamic content in static |
| Middleware | | Runs before every route, chain with |
| Astro DB table | | Type-safe SQL with column definitions |
| Framework component | Import | Auto-detected by file extension |
| Integration | | CLI to add framework adapters and tools |
| Render content | | Compile Markdown/MDX to component |
| Dynamic routes | | Generate pages from collection entries |
| API endpoint | | Server-rendered REST endpoints |
| Shared island state | | Reactive state across framework boundaries |
| Environment variables | | |
| Transition animation | | |
| Prefetch links | | |
| Collection reference | | Type-safe cross-collection relations |
| Script re-execution | | Re-run |
| Redirect | | Redirect from middleware or server pages |
| 模式 | API / 指令 | 核心要点 |
|---|---|---|
| 内容集合 | | Zod模式、glob/文件加载器、类型安全查询 |
| 查询集合 | | 返回类型化数组,支持过滤回调 |
| 单条条目 | | 按集合名称和条目ID获取内容 |
| 孤岛(立即加载) | | 页面加载时立即进行hydrate |
| 孤岛(空闲时加载) | | 浏览器空闲时进行hydrate |
| 孤岛(可见时加载) | | 组件进入视口时进行hydrate |
| 孤岛(媒体查询触发) | | 匹配媒体查询时进行hydrate |
| 孤岛(仅客户端) | | 跳过SSR,仅在客户端渲染 |
| 视图过渡 | | 添加至 |
| 状态持久化 | | 在导航过程中保持孤岛状态 |
| 编程式导航 | | 从脚本中触发客户端导航 |
| 静态输出 | | 构建时预渲染所有页面(默认设置) |
| 服务端输出 | | 按需服务端渲染所有页面 |
| 混合模式(可选SSR) | | 默认静态渲染,为单个页面开启SSR |
| 混合模式(可选静态) | | 默认SSR,为单个页面开启静态渲染 |
| 服务端孤岛 | | 静态站点中延迟服务端渲染动态内容 |
| 中间件 | | 在每个路由前运行,可通过 |
| Astro DB表 | | 带列定义的类型安全SQL |
| 框架组件 | 导入 | 根据文件扩展名自动检测 |
| 集成 | | 用于添加框架适配器与工具的CLI |
| 渲染内容 | | 将Markdown/MDX编译为组件 |
| 动态路由 | | 从集合条目生成页面 |
| API端点 | | 服务端渲染的REST endpoints |
| 孤岛共享状态 | | 跨框架边界的响应式状态 |
| 环境变量 | | 带 |
| 过渡动画 | | 支持 |
| 预取链接 | | 支持 |
| 集合引用 | | 类型安全的跨集合关联 |
| 脚本重执行 | | 每次视图过渡导航时重新运行 |
| 重定向 | | 从中间件或服务端页面进行重定向 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Adding | Only UI framework components (React, Svelte, Vue) accept |
Using | Default to |
Forgetting framework string with | Must specify framework: |
| Mixing framework components in non-Astro files | Only |
Using | Use |
Omitting | Must be in |
Content config not at | File must be named |
Not awaiting | Always |
Importing from | Content APIs are server-only; pass data as props to client components |
Placing middleware outside | Middleware must be at |
| Passing functions as props to islands | Only serializable data crosses the server/client boundary |
Using | Component must appear on both old and new page with same persist value |
| Missing adapter for server/hybrid mode | Install an adapter: |
| 错误操作 | 正确做法 |
|---|---|
向 | 仅UI框架组件(React、Svelte、Vue)支持 |
全局使用 | 默认使用 |
使用 | 必须指定框架: |
| 在非Astro文件中混合框架组件 | 仅 |
对以静态为主的站点使用 | 对混合模式使用 |
配置视图过渡时遗漏 | 必须在每个页面的 |
内容配置文件未放在 | 文件必须命名为 |
未等待 | 必须在前置代码中始终 |
在客户端脚本中从 | 内容API仅服务端可用;需将数据作为props传递给客户端组件 |
中间件未放在 | 中间件必须置于 |
| 向孤岛传递函数作为props | 仅可序列化数据能跨服务端/客户端边界传递 |
在不匹配的页面上使用 | 组件必须在新旧页面上都存在且使用相同的persist值 |
| 服务端/混合模式下缺少适配器 | 安装适配器: |
Delegation
委托
- Pattern discovery: Use agent
Explore - Build configuration: Use agent
Task - Code review: Delegate to agent
code-reviewer
If theskill is available, delegate styling and Tailwind CSS configuration to it. If thetailwindskill is available, delegate unit testing patterns to it. If thevitestskill is available, delegate end-to-end testing patterns to it. If theplaywrightskill is available, delegate build configuration and Vite plugin setup to it. If theviteskill is available, delegate advanced database query patterns to it. If thedrizzleskill is available, delegate error monitoring and observability setup to it. If thesentryskill is available, delegate server-side logging configuration to it. If thepino-loggingskill is available, delegate client-side data fetching and caching to it.tanstack-query
- 模式探索:使用agent
Explore - 构建配置:使用agent
Task - 代码审查:委托给agent
code-reviewer
若技能可用,将样式与Tailwind CSS配置委托给它。 若tailwind技能可用,将单元测试模式委托给它。 若vitest技能可用,将端到端测试模式委托给它。 若playwright技能可用,将构建配置与Vite插件设置委托给它。 若vite技能可用,将高级数据库查询模式委托给它。 若drizzle技能可用,将错误监控与可观测性设置委托给它。 若sentry技能可用,将服务端日志配置委托给它。 若pino-logging技能可用,将客户端数据获取与缓存委托给它。tanstack-query
References
参考
- Content collections, schemas, loaders, and querying
- Island architecture and hydration directives
- View transitions and client-side navigation
- Rendering modes: static, server, and hybrid
- Middleware patterns and request handling
- Framework component integration (React, Svelte, Vue)
- Astro DB schema, seeding, and queries
- 内容集合、模式、加载器与查询
- 孤岛架构与hydration指令
- 视图过渡与客户端导航
- 渲染模式:静态、服务端与混合
- 中间件模式与请求处理
- 框架组件集成(React、Svelte、Vue)
- Astro DB模式、数据填充与查询