Claude CodeでContentful CMS統合を実装する方法
Claude Codeを活用したContentful CMSのWeb統合ガイド。コンテンツモデル設計、API連携、Next.jsでのISR/SSG実装まで実践的に解説します。
Contentful CMS統合をClaude Codeで効率化する
Contentfulはヘッドレス CMS の代表格で、APIファーストなコンテンツ管理を実現します。構造化されたコンテンツモデルとリッチな配信APIにより、あらゆるフロントエンドからコンテンツを取得できます。Claude Codeを使えば、コンテンツモデルの設計からフロントエンド統合まで効率的に進められます。
コンテンツモデルの設計
> Contentfulでブログサイトのコンテンツモデルを設計して。
> 記事、カテゴリ、著者の3つのコンテンツタイプで。
// scripts/setup-content-model.ts
import contentful from 'contentful-management';
const client = contentful.createClient({
accessToken: process.env.CONTENTFUL_MANAGEMENT_TOKEN!,
});
async function setupContentModel() {
const space = await client.getSpace(process.env.CONTENTFUL_SPACE_ID!);
const env = await space.getEnvironment('master');
// 著者コンテンツタイプ
const author = await env.createContentTypeWithId('author', {
name: 'Author',
fields: [
{ id: 'name', name: 'Name', type: 'Symbol', required: true },
{ id: 'slug', name: 'Slug', type: 'Symbol', required: true },
{ id: 'bio', name: 'Bio', type: 'Text' },
{ id: 'avatar', name: 'Avatar', type: 'Link', linkType: 'Asset' },
],
});
await author.publish();
// カテゴリ
const category = await env.createContentTypeWithId('category', {
name: 'Category',
fields: [
{ id: 'name', name: 'Name', type: 'Symbol', required: true },
{ id: 'slug', name: 'Slug', type: 'Symbol', required: true },
{ id: 'description', name: 'Description', type: 'Text' },
],
});
await category.publish();
// ブログ記事
const blogPost = await env.createContentTypeWithId('blogPost', {
name: 'Blog Post',
fields: [
{ id: 'title', name: 'Title', type: 'Symbol', required: true },
{ id: 'slug', name: 'Slug', type: 'Symbol', required: true },
{ id: 'excerpt', name: 'Excerpt', type: 'Text' },
{ id: 'body', name: 'Body', type: 'RichText' },
{ id: 'featuredImage', name: 'Featured Image', type: 'Link', linkType: 'Asset' },
{ id: 'author', name: 'Author', type: 'Link', linkType: 'Entry' },
{ id: 'category', name: 'Category', type: 'Link', linkType: 'Entry' },
{ id: 'tags', name: 'Tags', type: 'Array', items: { type: 'Symbol' } },
{ id: 'publishedAt', name: 'Published At', type: 'Date' },
],
});
await blogPost.publish();
}
コンテンツの取得
> ContentfulのContent Delivery APIでブログ記事を
> 取得するクライアントを作って。TypeScript型も生成して。
// src/lib/contentful.ts
import { createClient, type Entry, type Asset } from 'contentful';
const client = createClient({
space: process.env.CONTENTFUL_SPACE_ID!,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN!,
});
// プレビュー用クライアント
const previewClient = createClient({
space: process.env.CONTENTFUL_SPACE_ID!,
accessToken: process.env.CONTENTFUL_PREVIEW_TOKEN!,
host: 'preview.contentful.com',
});
function getClient(preview = false) {
return preview ? previewClient : client;
}
// 型定義
interface BlogPostFields {
title: string;
slug: string;
excerpt: string;
body: any; // Rich Text Document
featuredImage: Asset;
author: Entry<AuthorFields>;
category: Entry<CategoryFields>;
tags: string[];
publishedAt: string;
}
interface AuthorFields {
name: string;
slug: string;
bio: string;
avatar: Asset;
}
interface CategoryFields {
name: string;
slug: string;
description: string;
}
// 記事一覧の取得
export async function getBlogPosts(options?: {
limit?: number;
skip?: number;
category?: string;
preview?: boolean;
}) {
const { limit = 10, skip = 0, category, preview = false } = options || {};
const query: any = {
content_type: 'blogPost',
order: ['-fields.publishedAt'],
limit,
skip,
include: 2,
};
if (category) {
query['fields.category.sys.contentType.sys.id'] = 'category';
query['fields.category.fields.slug'] = category;
}
const entries = await getClient(preview).getEntries<BlogPostFields>(query);
return {
posts: entries.items,
total: entries.total,
};
}
// 個別記事の取得
export async function getBlogPost(slug: string, preview = false) {
const entries = await getClient(preview).getEntries<BlogPostFields>({
content_type: 'blogPost',
'fields.slug': slug,
include: 2,
limit: 1,
});
return entries.items[0] || null;
}
Next.jsでのISR実装
// app/blog/[slug]/page.tsx
import { getBlogPost, getBlogPosts } from '@/lib/contentful';
import { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import { notFound } from 'next/navigation';
export const revalidate = 60; // 60秒ごとに再検証
export async function generateStaticParams() {
const { posts } = await getBlogPosts({ limit: 100 });
return posts.map((post) => ({ slug: post.fields.slug }));
}
export default async function BlogPostPage({
params,
}: {
params: { slug: string };
}) {
const post = await getBlogPost(params.slug);
if (!post) notFound();
return (
<article className="max-w-3xl mx-auto p-6">
<h1 className="text-4xl font-bold">{post.fields.title}</h1>
<p className="text-gray-500 mt-2">
{new Date(post.fields.publishedAt).toLocaleDateString('ja-JP')}
</p>
<div className="prose mt-8">
{documentToReactComponents(post.fields.body)}
</div>
</article>
);
}
まとめ
ContentfulのAPIファーストなアプローチとClaude Codeを組み合わせれば、柔軟なコンテンツ管理システムを効率的に構築できます。ブログCMS構築ガイドやSSR/SSG比較も参考にしてください。
Contentfulの詳細はContentful公式ドキュメントを参照してください。
#Claude Code
#Contentful
#CMS
#ヘッドレスCMS
#コンテンツ管理
関連記事
Use Cases
Use Cases
Claude CodeでCORS設定完全ガイド:クロスオリジン通信の実践解説
Claude Codeを活用したCORS設定の完全ガイド。プリフライトリクエスト、認証付きリクエスト、トラブルシューティングまで実践的に解説します。
Use Cases
Use Cases
Claude Codeで通貨フォーマットを正しく実装する
Claude Codeを使って、多通貨対応・ロケール別フォーマット・為替レート変換など、通貨の表示と処理を正しく実装する方法を解説します。
Use Cases
Use Cases
Claude CodeでDiscord Botを開発する実践ガイド
Claude Codeを活用したDiscord Botの開発方法を解説。discord.js、スラッシュコマンド、ボタン操作、Embed、音声チャンネル連携まで実践的に紹介します。