You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Sanity Studio Schema传递视频URL引发Studio报错

Sanity Studio 配置视频URL字段后报错的解决方法

问题根源

你的Sanity Studio报错核心原因是预览配置(preview)里的media字段直接使用了URL类型的bgVideo。Sanity的preview.media仅支持图像资产(Image Asset)或React图标组件,直接传入URL字符串会触发渲染错误。另外,URL字段配置里的hotspot是图像字段专属的属性,属于无效配置,也可能引发警告。

修复步骤

1. 修正Sanity Schema配置

修改预览模块的media为图标组件,并移除URL字段的无效配置:

import { defineField, defineType } from 'sanity'
import { TfiLayoutCtaCenter } from 'react-icons/tfi'
import { getBlockText } from '../../src/utils'

export default defineType({
    name: 'hero.video',
    title: 'Hero (Video)',
    icon: TfiLayoutCtaCenter,
    type: 'object',
    fields: [
        // 保留原有其他字段
        defineField({
            name: 'bgVideo',
            type: 'url',
            title: 'Background Video URL',
            description: 'URL of the background video',
            // 移除URL字段不需要的hotspot配置
        }),
    ],
    preview: {
        select: {
            content: 'content',
        },
        prepare: ({ content }) => ({
            title: getBlockText(content),
            subtitle: 'Hero (Video)',
            media: TfiLayoutCtaCenter, // 用图标替代URL作为预览媒体
        }),
    },
})

2. 简化Next.js组件的URL处理

Sanity的URL字段本身会做格式校验,无需额外转义,可简化视频URL的处理逻辑:

// 替换原有的sanitizedUrl和hasVideo逻辑
const hasVideo = !!bgVideo

效果验证

修改后,Sanity Studio的报错会消失,同时Next.js端的视频播放功能不受影响。预览面板会显示你指定的图标,保持界面一致性。

内容的提问来源于stack exchange,提问作者oneadam007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:37:09