Sanity CMS dengan Claude Code
Pelajari tentang sanity cms menggunakan Claude Code. Dilengkapi tips praktis dan contoh kode.
Sanity CMSpengembangan dengan Claude Code: efisiensi
Sanity リアルタイムコラボレーション dan カスタマイズ性 高いスタジオ fitur headlessCMS.GROQ dan いう独自query言語 dan 、Reactベース manajemen画面 自由 カスタマイズ きる点 魅力.Claude Code 使えば、Sanity スキーマ設計やGROQquery juga efisien pembangunan bisa dilakukan.
Setup Proyek
> Sanityproyek buatkan.
> Next.js App Router dan integrasiする構成 dengan 。
npm create sanity@latest -- --project-id=xxxxx --dataset=production
# Next.jsプロジェクトにSanityクライアントを追加
npm install next-sanity @sanity/image-url
Definisi Skema
> ブログartikel dan penulis Sanityスキーマ buatkan.
> リッチテキスト、gambar、参照フィールド 含めて。
// sanity/schemas/post.ts
import { defineType, defineField } from 'sanity';
export const postType = defineType({
name: 'post',
title: 'Post',
type: 'document',
fields: [
defineField({
name: 'title',
title: 'Title',
type: 'string',
validation: (rule) => rule.required().max(100),
}),
defineField({
name: 'slug',
title: 'Slug',
type: 'slug',
options: { source: 'title', maxLength: 96 },
validation: (rule) => rule.required(),
}),
defineField({
name: 'author',
title: 'Author',
type: 'reference',
to: [{ type: 'author' }],
}),
defineField({
name: 'mainImage',
title: 'Main Image',
type: 'image',
options: { hotspot: true },
fields: [
defineField({
name: 'alt',
title: 'Alt Text',
type: 'string',
}),
],
}),
defineField({
name: 'publishedAt',
title: 'Published At',
type: 'datetime',
}),
defineField({
name: 'categories',
title: 'Categories',
type: 'array',
of: [{ type: 'reference', to: [{ type: 'category' }] }],
}),
defineField({
name: 'body',
title: 'Body',
type: 'blockContent',
}),
],
preview: {
select: {
title: 'title',
author: 'author.name',
media: 'mainImage',
},
prepare({ title, author, media }) {
return { title, subtitle: author, media };
},
},
});
設計 GROQquery
> ブログartikeldaftar dan 詳細halaman用 GROQquery 書いて。
> penulis情報 dan kategori juga 展開してpengambilanして。
// src/lib/sanity/queries.ts
import { groq } from 'next-sanity';
// artikeldaftar
export const postsQuery = groq`
*[_type == "post" && defined(slug.current)] | order(publishedAt desc) {
_id,
title,
"slug": slug.current,
publishedAt,
excerpt,
mainImage {
asset-> {
_id,
url,
metadata { dimensions, lqip }
},
alt
},
author-> {
name,
"slug": slug.current,
image { asset-> { url } }
},
categories[]-> {
title,
"slug": slug.current
}
}[0...$limit]
`;
// 個別artikel
export const postBySlugQuery = groq`
*[_type == "post" && slug.current == $slug][0] {
_id,
title,
"slug": slug.current,
publishedAt,
body[] {
...,
_type == "image" => {
...,
asset-> { url, metadata { dimensions } }
}
},
mainImage {
asset-> { url, metadata { dimensions, lqip } },
alt
},
author-> {
name,
bio,
image { asset-> { url } }
},
categories[]-> {
title,
"slug": slug.current
},
"relatedPosts": *[
_type == "post" &&
slug.current != $slug &&
count(categories[@._ref in ^.^.categories[]._ref]) > 0
] | order(publishedAt desc) [0...3] {
title,
"slug": slug.current,
publishedAt,
mainImage { asset-> { url } }
}
}
`;
データpengambilan Next.js
// src/lib/sanity/client.ts
import { createClient } from 'next-sanity';
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
apiVersion: '2024-01-01',
useCdn: process.env.NODE_ENV === 'production',
});
// app/blog/page.tsx
import { client } from '@/lib/sanity/client';
import { postsQuery } from '@/lib/sanity/queries';
export default async function BlogPage() {
const posts = await client.fetch(postsQuery, { limit: 20 });
return (
<div className="max-w-4xl mx-auto p-6">
<h1 className="text-3xl font-bold mb-8">ブログ</h1>
{posts.map((post: any) => (
<article key={post._id} className="mb-8 border-b pb-6">
<h2 className="text-xl font-bold">{post.title}</h2>
<p className="text-gray-500 text-sm">{post.author?.name}</p>
</article>
))}
</div>
);
}
Summary
Sanity GROQquery dan カスタマイズdimungkinkanなスタジオ 、fleksibelなkontenmanajemen 実現.Claude Code pemanfaatanすれば、スキーマ設計やGROQ 記述 juga efisien 進められ.ブログCMSpembangunanpanduanやNext.jsfull-stackpengembangan juga bisa dijadikan referensi.
Untuk Sanityの詳細, lihat Sanity公式ドキュメント.
Related Posts
Cara Mempercepat Side Project dengan Claude Code [Dengan Contoh]
Pelajari cara mempercepat project development personal secara drastis menggunakan Claude Code. Dilengkapi contoh nyata dan workflow praktis dari ide hingga deployment.
Cara Mengotomatisasi Refactoring dengan Claude Code
Pelajari cara mengotomatisasi code refactoring secara efisien menggunakan Claude Code. Dilengkapi prompt praktis dan pola refactoring konkret untuk project nyata.
Panduan Lengkap Konfigurasi CORS dengan Claude Code
Pelajari tentang panduan lengkap konfigurasi CORS menggunakan Claude Code. Dilengkapi tips praktis dan contoh kode.