Sanity CMS:Claude Code 实战指南
了解sanity cms:Claude Code 实战. 包含实用技巧和代码示例。
Sanity CMS开发を通过 Claude Code 提高效率
Sanityは实时コラボレーションと自定义性の高いスタジオが特徴のヘッドレスCMSです。GROQという独自查询言語と、Reactベースの管理画面を自由に自定义できる点が魅力です。借助 Claude Code,SanityのSchema设计やGROQ查询も高效地构建可以。
项目のセットアップ
> Sanity项目创建。
> Next.js App Routerと联动する结构で。
npm create sanity@latest -- --project-id=xxxxx --dataset=production
# Next.jsプロジェクトにSanityクライアントを追加
npm install next-sanity @sanity/image-url
Schema定義
> ブ日志文章と著者のSanitySchemaを作って。
> リッチテキスト、图片、参照フィールドを含めて。
// 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 };
},
},
});
GROQ查询の设计
> ブ日志文章列表と详情页面用のGROQ查询を書いて。
> 著者情報と分类も展开して获取して。
// src/lib/sanity/queries.ts
import { groq } from 'next-sanity';
// 文章列表
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]
`;
// 個別文章
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 } }
}
}
`;
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>
);
}
总结
SanityのGROQ查询と自定义可能なスタジオは、柔軟な内容管理を実現します。Claude Codeを活用すれば、Schema设计やGROQの記述も高效地進められます。ブ日志CMS构建指南やNext.js全栈开发也可以参考。
Sanity的详细信息请参阅Sanity官方文档。
#Claude Code
#Sanity
#CMS
#ヘッドレスCMS
#GROQ
免费
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
Use Cases
每天发布多语言 Claude Code 文章前,要先检查的 7 件事
一份实用清单,帮助你每天发布多语言 Claude Code 文章时避免漏语言、CTA 错位和线上内容未更新。
Use Cases
Codex Automations 是什么?让 AI 在你睡觉时完成内容运营
用 Codex Automations 自动查看流量、选择主题、写文章、改善转化路径并部署网站的实用指南。
Use Cases
Claude Code × GCP Cloud Functions 完全指南 | 极速开发无服务器函数
用 Claude Code 高效开发 GCP Cloud Functions。从 HTTP/Pub/Sub/Firestore 触发器实现到本地测试、部署自动化,基于 Masa 的实战经验,附完整可运行代码示例。