提示:本网站现在为维护调试状态

Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション

Choyeon
Choyeon
2026年9月2日 1 分で読了 379 閲覧
309 字 2,204 文字 更新日 2026年11月8日
Astro 静的サイト:アイランドアーキテクチャ & コンテンツコレクション
目次

Astro Deep Dive: Islands Architecture & Content Collections

Astro ships zero JS by default, powered by Islands Architecture (selective hydration) and type-safe Content Collections, delivering peak performance with modern DX.

Islands Architecture and Hydration Strategies

Non-interactive content renders as pure static HTML. Only components with client:load/visible/idle/only fetch JS and hydrate. React/Svelte/Vue components coexist on same page independently.

// src/content/config.ts
import { defineCollection, z } from 'astro:content';

const blog = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string().max(80),
    description: z.string().max(200),
    pubDate: z.coerce.date(),
    tags: z.array(z.string()).default([]),
    cover: z.object({ src: z.string(), alt: z.string() }).optional(),
    draft: z.boolean().default(false),
    category: z.enum(['tutorial','technology','essays','translation']),
  }),
});
const projects = defineCollection({
  type: 'content',
  schema: ({ image }) => z.object({
    name: z.string(), summary: z.string(),
    hero: image(), stack: z.array(z.string()),
    repo: z.string().url().optional(), live: z.string().url().optional(),
    featured: z.boolean().default(false),
  }),
});
export const collections = { blog, projects };

// src/pages/blog/[...slug].astro
/* ---
import { getCollection, type CollectionEntry } from 'astro:content';
import { Image } from 'astro:assets';
import LikeCounter from '../../components/LikeCounter.svelte';

export async function getStaticPaths() {
  const posts = await getCollection('blog', ({ data }) => import.meta.env.DEV || !data.draft);
  return posts.map(p => ({ params: { slug: p.slug }, props: { post: p } }));
}
const { post } = Astro.props as { post: CollectionEntry<'blog'> };
const { Content } = await post.render();
--- ---
<article>
  <header>
    <h1>{post.data.title}</h1>
    {post.data.cover && <Image src={post.data.cover.src} alt={post.data.cover.alt} widths={[400,800,1200]} />}
  </header>
  <Content />
  <LikeCounter client:idle postSlug={post.slug} />
</article>
*/

Type-Safe Content Collections

Define Zod schemas per collection in content/config.ts. When calling getCollection, Astro validates frontmatter and throws at build time on invalid data. Built-in image optimization works through schemas.

Directive Trigger Timing Overhead Best For
Default Never hydrates Zero JS Static cards, body
client:load Immediate on load Highest Nav, core UI
client:idle After first idle Medium-low Likes, secondary
client:visible Enters viewport Very Low Footer, late items

Best Practices

Start without client:* directives - add only after proving interactivity needed. Enforce strict collection schemas to avoid data drift. Pre-render every dynamic route via getStaticPaths + slug pattern.

X微博Telegram

筆者について

投票

你最希望 Rosetta 优先加入哪项功能?

社区投票(演示数据,可后台删除)

コメント (5)

0 / 3000
机器学习萌新
机器学习萌新2026年10月1日

看到博主推荐的这个 UI 组件库了,之前一直在纠结选哪个,试了一下确实不错,文档也很友好!

测试小瓢虫
测试小瓢虫2026年9月30日

补充:用 CSS container query 实现响应式其实比媒体查询更灵活,组件级别的适配体验会好很多。

自由撰稿人阿秋
自由撰稿人阿秋qiuwrites.blog2026年9月30日

博主请问一下,Next.js 15 里这个写法在 App Router 下会有 hydration 不匹配的问题吗?我用的时候偶尔报错。

安全小白白
安全小白白seclab.digital2026年9月30日

终于有人把 CSS Grid 的各种布局技巧讲明白了,我之前看 MDN 文档看半天没搞懂,看完这篇直接上手了!

运维老司机
运维老司机ops-notes.cn2026年9月29日

示例代码里那个 useEffect 的依赖数组好像少了个参数,严格模式下会触发两次,建议修正一下。

関連記事

フロントエンド性能:Core Web Vitals 実践

フロントエンド性能:Core Web Vitals 実践

Google検索順位のCore Web Vitals完全実践:LCP最適化(事前接続・画像優先度・CDN)、CLS除去(画像寸法・フォント・プレースホルダ)、INP改善(長タスク分割・デバウンス)。

1 分で読了
PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

PWA オフラインアプリ実践:Service Worker + IndexedDB + Background Sync

インストール可能・オフラインファーストなPWAを段階的構築:Service Worker キャッシュ戦略(Cache First / Stale-While-Revalidate)、構造化ローカルデータ用IndexedDBラッパー、ネットワーク復旧時にオフライン操作を自動リプレイするBackground Syncキュー。完全TypeScriptコード付き。

1 分で読了
React 18 Hooks ベストプラクティス

React 18 Hooks ベストプラクティス

React 18 は、コンカレントレンダリング、自動バッチング、useTransition、useDeferredValue などの革新的な機能を導入しました。この記事では、useState/useEffect の正しい使い方、useMemo/useCallback の最適化境界、カスタム Hook の設計パターン、コンカレントモードでの古いクロージャや競合状態を回避するための完全なガイドを提供します。

1 分で読了
Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数

Tailwind CSS v4 新機能:Zero-config + CSS ネイティブ変数

Tailwind CSS v4はアーキテクチャ級アップデート:tailwind.config.js を廃止し、ネイティブCSS内の@themeブロックでデザイントークン宣言。@import "tailwindcss" 単一エントリでpostcss設定不要。CSS変数内蔵、Container Queriesネイティブ対応、oxcエンジンで10-40倍高速化、カラー関数簡略化。

1 分で読了
Vue 3 コンポジション API 実践

Vue 3 コンポジション API 実践

Vue 3 のコンポジション API は、Vue コンポーネントの設計手法を根本的に変えました。複雑な EC サイトのフィルターコンポーネントの構築を通じて、ref/reactive の使い分け、computed/watch の適切な使用方法、カスタム Composable の抽出原則、ライフサイクルフックのタイミング、provide/inject による階層を超えた状態受け渡しの正しい方法を解説します。

1 分で読了
CSS Container Queries 完全ガイド

CSS Container Queries 完全ガイド

モダンCSSの2大レスポンシブ機能:親コンテナ寸法に応じるContainer Queries、子孫要素の状態で親をスタイルする:has()セレクタ。

1 分で読了