コンテンツの取得

このページ

EmDash は Astro のページ・コンポーネント向けに、live content collections と同様のパターンで、構造化された結果とエラー処理付きのクエリ関数を提供します。

クエリ関数

関数用途戻り値
getEmDashCollectionコレクションの全エントリ{ entries, error }
getEmDashEntryID または slug で 1 件{ entry, error, isPreview }
import { getEmDashCollection, getEmDashEntry } from "emdash";

全件取得

---
import { getEmDashCollection } from "emdash";

const { entries: posts, error } = await getEmDashCollection("posts");

if (error) {
  console.error("Failed to load posts:", error);
}
---

<ul>
  {posts.map((post) => (
    <li>{post.data.title}</li>
  ))}
</ul>

ロケールで絞り込む

i18n 有効時localestatus を指定します。単一エントリは第 3 引数にオプションで渡します。省略時はリクエストのロケールが使われ、訳がない場合はフォールバック連鎖に従います。

ステータス・件数・タクソノミー

公開ページでは常に status: "published" を推奨。limitwhere(同一タクソノミーに複数値がある場合は OR)の例は英語版と同じコードをそのまま利用できます。

エラー処理

error を確認し、必要なら 500 を返すなどしてください。

1 件取得

英語版 src/pages/posts/[slug].astro の例と同じです。

プレビューモード

_preview トークンはミドルウェアが処理するため、クエリ側で追加引数は不要です。

ビジュアル編集

entry.edit をスプレッドするとインライン編集用の注釈が付きます(本番では出力されません)。

ソート

getEmDashCollection の並び順は保証されないため、テンプレート側で publishedAt などに基づきソートします。

TypeScript

npx emdash types.emdash/types.ts を生成し、ジェネリクスで型付けできます。

静的生成と SSR

getStaticPathsexport const prerender = false のパターンは英語版のコード例と同じです。

パフォーマンス

必要に応じて Cache-Control を設定し、同じページ内で同じコレクションを何度も取らないようにします。

次のステップ