Post Schema

Post Schema

【免费下载链接】sanity Sanity Studio – Rapidly configure content workspaces powered by structured content 【免费下载链接】sanity 项目地址: https://gitcode.com/GitHub_Trending/sa/sanity

描述

博客文章内容模型,包含标题、内容、作者等信息。

字段说明

字段名类型必选描述
titlestring文章标题,5-100个字符
slugslugURL友好标识符
bodyarray文章内容,使用Portable Text

使用示例

// 查询最新文章
*[_type == "post"] | order(publishedAt desc) [0..4] {
  title,
  slug,
  publishedAt,
  author -> {name}
}

注意事项

  • 避免在body字段存储超过10,000字的内容
  • slug字段生成后不可修改

## 5. 性能优化策略

### 5.1 Schema 性能优化

通过以下方式优化 Schema 性能:

```typescript
// 优化前
defineType({
  name: 'product',
  type: 'document',
  fields: [
    // 包含大量嵌套字段和引用...
  ]
})

// 优化后 - 拆分复杂模型
defineType({
  name: 'product',
  type: 'document',
  fields: [
    // 核心字段...
    defineField({
      name: 'specifications',
      type: 'reference',
      to: [{type: 'productSpecifications'}]
    }),
    defineField({
      name: 'reviews',
      type: 'reference',
      to: [{type: 'productReviews'}]
    })
  ]
})

5.2 查询性能优化

GROQ 查询优化技巧:

// 优化前 - 过度获取
*[_type == "post"]{
  *,
  author->*,
  comments[]->{
    *,
    author->*
  }
}

// 优化后 - 仅获取需要的字段
*[_type == "post"]{
  _id,
  title,
  slug,
  publishedAt,
  "authorName": author->name,
  "authorAvatar": author->avatar,
  "commentCount": count(comments)
}[0..10] | order(publishedAt desc)

性能提升策略

  • 限制返回字段数量
  • 使用分页减少结果集大小
  • 避免深层嵌套引用
  • 使用计数替代完整列表

6. 部署与持续集成

6.1 CI/CD 管道配置

使用 GitHub Actions 实现自动化部署:

# .github/workflows/deploy.yml
name: Deploy Sanity Studio

on:
  push:
    branches: [ develop, main ]
    paths:
      - 'sanity-studio/**'
      - '.github/workflows/deploy.yml'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
          cache-dependency-path: sanity-studio/package-lock.json
      
      - name: Install dependencies
        run: cd sanity-studio && npm ci
      
      - name: Lint code
        run: cd sanity-studio && npm run lint
      
      - name: Type check
        run: cd sanity-studio && npm run type-check
      
      - name: Deploy to Sanity
        run: cd sanity-studio && npx sanity deploy
        env:
          SANITY_AUTH_TOKEN: ${{ secrets.SANITY_AUTH_TOKEN }}

6.2 环境配置管理

使用 .env 文件和环境变量管理不同环境配置:

// sanity.cli.ts
import {defineCliConfig} from 'sanity/cli'
import dotenv from 'dotenv'

// 加载环境变量
dotenv.config()

export default defineCliConfig({
  api: {
    projectId: process.env.SANITY_PROJECT_ID,
    dataset: process.env.SANITY_DATASET || 'production'
  }
})

环境变量分类

  • SANITY_PROJECT_ID: 项目 ID
  • SANITY_DATASET: 数据集名称
  • SANITY_API_VERSION: API 版本
  • SANITY_STUDIO_TITLE: 工作室标题
  • FEATURE_COMMENTS: 功能标志

7. 扩展与插件开发

7.1 自定义插件结构

Sanity 插件开发规范:

// plugins/comments/index.ts
import {definePlugin} from 'sanity'
import {CommentsTool} from './CommentsTool'
import {CommentsIcon} from './CommentsIcon'
import {defineType} from 'sanity'

export const commentsPlugin = definePlugin({
  name: 'comments-plugin',
  title: 'Comments',
  icon: CommentsIcon,
  tools: [
    {
      name: 'comments',
      title: 'Comments',
      component: CommentsTool,
    },
  ],
  schema: {
    types: [
      defineType({
        name: 'comment',
        title: 'Comment',
        type: 'document',
        fields: [
          // 字段定义...
        ],
      }),
    ],
  },
})

7.2 第三方集成最佳实践

外部服务集成模式:

// plugins/algolia/index.ts
import {definePlugin} from 'sanity'
import {AlgoliaSettings} from './components/AlgoliaSettings'
import {algoliaSyncAction} from './actions/algoliaSyncAction'

export const algoliaPlugin = definePlugin((options) => {
  const {appId, apiKey, indexName} = options
  
  if (!appId || !apiKey || !indexName) {
    throw new Error('Algolia plugin requires appId, apiKey and indexName')
  }
  
  return {
    name: 'sanity-plugin-algolia',
    document: {
      actions: (prev) => {
        // 添加自定义文档操作
        return [...prev, algoliaSyncAction(options)]
      },
    },
    studio: {
      components: {
        // 添加设置页面
        settings: (prev) => [...prev, AlgoliaSettings],
      },
    },
  }
})

8. 故障排除与调试

8.1 常见问题解决

问题原因解决方案
Schema 验证失败字段类型不匹配检查控制台错误,验证字段定义
预览不显示select 配置错误确保 preview.select 包含正确字段
引用无法解析文档不存在或权限问题使用 weak() 或检查引用 ID
性能缓慢大型数组或深层嵌套优化 Schema 和 GROQ 查询
部署失败环境变量缺失检查 CI 配置和密钥

8.2 调试工具与技术

Sanity 开发调试技巧:

// sanity.config.ts - 添加调试工具
import {visionTool} from '@sanity/vision'

export default defineConfig({
  // ...其他配置
  plugins: [
    // ...其他插件
    visionTool({
      defaultApiVersion: '2024-01-01',
      // 预定义查询
      defaultQuery: `*[_type == "post"]{
        _id,
        title,
        publishedAt
      }[0..5] | order(publishedAt desc)`
    })
  ]
})

【免费下载链接】sanity Sanity Studio – Rapidly configure content workspaces powered by structured content 【免费下载链接】sanity 项目地址: https://gitcode.com/GitHub_Trending/sa/sanity

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值