Sending URL from Sanity Studio Schema causes error in Sanity Studio

I am creating a Website that uses Next.js as my framework and Sanity Studio using the SanityPress template. I am trying to create a module that plays a background video from a Video URL that is received by Sanity Studio. When I pass the video url from a Sanity Studio Schema to my Next.js website it causes an error in Sanity Studio.

I tried sanitizing the URL from the module and that works and occupies the video but error the same!

Sanity Studio Error

Schema Module:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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: [
// other fields
}),
defineField({
name: 'bgVideo', // New field for video background
type: 'url',
title: 'Background Video URL',
description: 'URL of the background video',
options: {
hotspot: true,
},
}),
],
preview: {
select: {
content: 'content',
media: 'bgVideo',
},
prepare: ({ content, media }) => ({
title: getBlockText(content),
subtitle: 'Hero (Video)',
media,
}),
},
})
</code>
<code>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: [ // other fields }), defineField({ name: 'bgVideo', // New field for video background type: 'url', title: 'Background Video URL', description: 'URL of the background video', options: { hotspot: true, }, }), ], preview: { select: { content: 'content', media: 'bgVideo', }, prepare: ({ content, media }) => ({ title: getBlockText(content), subtitle: 'Hero (Video)', media, }), }, }) </code>
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: [
        // other fields
        }),
        defineField({
            name: 'bgVideo', // New field for video background
            type: 'url',
            title: 'Background Video URL',
            description: 'URL of the background video',
            options: {
                hotspot: true,
            },
        }),
    ],
    preview: {
        select: {
            content: 'content',
            media: 'bgVideo',
        },
        prepare: ({ content, media }) => ({
            title: getBlockText(content),
            subtitle: 'Hero (Video)',
            media,
        }),
    },
})

Next.js Module:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// other imports
import { stegaClean } from '@sanity/client/stega'
import Video from 'next-video' // Import the Video component
export default function Hero({
pretitle,
content,
ctas,
bgImage,
bgImageMobile,
bgVideo,
textAlign = 'center',
alignItems,
}: Partial<{
pretitle: string
content: any
ctas: Sanity.CTA[]
bgImage: Sanity.Image
bgImageMobile: Sanity.Image
bgVideo: string
textAlign: React.CSSProperties['textAlign']
alignItems: React.CSSProperties['alignItems']
}>) {
const hasImage = !!bgImage?.asset
const sanitizedUrl = bgVideo ? encodeURI(bgVideo) : '' // Ensure URL is sanitized
const hasVideo = !!sanitizedUrl // Ensure this is correctly set
console.log('Video URL:', sanitizedUrl)
console.log('Has Video:', hasVideo)
return (
<section
className={cn(
(hasImage || hasVideo) &&
'grid overflow-hidden bg-ink text-canvas *:col-span-full *:row-span-full',
)}
>
{/* Background Video */}
{hasVideo && (
<Video
src={sanitizedUrl} // Provide the sanitized video source URL
autoPlay
loop
muted
playsInline
className="absolute inset-0 h-full w-full object-cover"
onError={(e) => console.error('Video error:', e)}
/>
)}
{/* Background Image */}
{bgImage?.asset && !hasVideo && (
<picture>
<Source image={bgImageMobile} imageWidth={1200} />
<Img
className="size-full max-h-fold object-cover"
image={bgImage}
imageWidth={1800}
draggable={false}
/>
</picture>
)}
{content && (
<div className="section relative z-10 flex w-full flex-col">
<div
className={cn(
'richtext relative isolate max-w-xl [&_:is(h1,h2)]:text-balance',
(hasImage || hasVideo) && 'text-shadow',
hasImage && css.txt,
{
'mb-8': stegaClean(alignItems) === 'start',
'my-auto': stegaClean(alignItems) === 'center',
'mt-auto': stegaClean(alignItems) === 'end',
},
{
'mr-auto': stegaClean(textAlign) === 'left',
'mx-auto': stegaClean(textAlign) === 'center',
'ml-auto': stegaClean(textAlign) === 'right',
},
)}
style={{ textAlign: stegaClean(textAlign) }}
>
<Pretitle
className={cn((hasImage || hasVideo) && 'text-canvas/70')}
>
{pretitle}
</Pretitle>
<PortableText value={content} />
<CTAList
ctas={ctas}
className={cn('!mt-4', {
'justify-start': stegaClean(textAlign) === 'left',
'justify-center': stegaClean(textAlign) === 'center',
'justify-end': stegaClean(textAlign) === 'right',
})}
/>
</div>
</div>
)}
</section>
)
}
</code>
<code>// other imports import { stegaClean } from '@sanity/client/stega' import Video from 'next-video' // Import the Video component export default function Hero({ pretitle, content, ctas, bgImage, bgImageMobile, bgVideo, textAlign = 'center', alignItems, }: Partial<{ pretitle: string content: any ctas: Sanity.CTA[] bgImage: Sanity.Image bgImageMobile: Sanity.Image bgVideo: string textAlign: React.CSSProperties['textAlign'] alignItems: React.CSSProperties['alignItems'] }>) { const hasImage = !!bgImage?.asset const sanitizedUrl = bgVideo ? encodeURI(bgVideo) : '' // Ensure URL is sanitized const hasVideo = !!sanitizedUrl // Ensure this is correctly set console.log('Video URL:', sanitizedUrl) console.log('Has Video:', hasVideo) return ( <section className={cn( (hasImage || hasVideo) && 'grid overflow-hidden bg-ink text-canvas *:col-span-full *:row-span-full', )} > {/* Background Video */} {hasVideo && ( <Video src={sanitizedUrl} // Provide the sanitized video source URL autoPlay loop muted playsInline className="absolute inset-0 h-full w-full object-cover" onError={(e) => console.error('Video error:', e)} /> )} {/* Background Image */} {bgImage?.asset && !hasVideo && ( <picture> <Source image={bgImageMobile} imageWidth={1200} /> <Img className="size-full max-h-fold object-cover" image={bgImage} imageWidth={1800} draggable={false} /> </picture> )} {content && ( <div className="section relative z-10 flex w-full flex-col"> <div className={cn( 'richtext relative isolate max-w-xl [&_:is(h1,h2)]:text-balance', (hasImage || hasVideo) && 'text-shadow', hasImage && css.txt, { 'mb-8': stegaClean(alignItems) === 'start', 'my-auto': stegaClean(alignItems) === 'center', 'mt-auto': stegaClean(alignItems) === 'end', }, { 'mr-auto': stegaClean(textAlign) === 'left', 'mx-auto': stegaClean(textAlign) === 'center', 'ml-auto': stegaClean(textAlign) === 'right', }, )} style={{ textAlign: stegaClean(textAlign) }} > <Pretitle className={cn((hasImage || hasVideo) && 'text-canvas/70')} > {pretitle} </Pretitle> <PortableText value={content} /> <CTAList ctas={ctas} className={cn('!mt-4', { 'justify-start': stegaClean(textAlign) === 'left', 'justify-center': stegaClean(textAlign) === 'center', 'justify-end': stegaClean(textAlign) === 'right', })} /> </div> </div> )} </section> ) } </code>
// other imports
import { stegaClean } from '@sanity/client/stega'
import Video from 'next-video' // Import the Video component

export default function Hero({
    pretitle,
    content,
    ctas,
    bgImage,
    bgImageMobile,
    bgVideo,
    textAlign = 'center',
    alignItems,
}: Partial<{
    pretitle: string
    content: any
    ctas: Sanity.CTA[]
    bgImage: Sanity.Image
    bgImageMobile: Sanity.Image
    bgVideo: string
    textAlign: React.CSSProperties['textAlign']
    alignItems: React.CSSProperties['alignItems']
}>) {
    const hasImage = !!bgImage?.asset
    const sanitizedUrl = bgVideo ? encodeURI(bgVideo) : '' // Ensure URL is sanitized
    const hasVideo = !!sanitizedUrl // Ensure this is correctly set

    console.log('Video URL:', sanitizedUrl)
    console.log('Has Video:', hasVideo)

    return (
        <section
            className={cn(
                (hasImage || hasVideo) &&
                    'grid overflow-hidden bg-ink text-canvas *:col-span-full *:row-span-full',
            )}
        >
            {/* Background Video */}
            {hasVideo && (
                <Video
                    src={sanitizedUrl} // Provide the sanitized video source URL
                    autoPlay
                    loop
                    muted
                    playsInline
                    className="absolute inset-0 h-full w-full object-cover"
                    onError={(e) => console.error('Video error:', e)}
                />
            )}

            {/* Background Image */}
            {bgImage?.asset && !hasVideo && (
                <picture>
                    <Source image={bgImageMobile} imageWidth={1200} />
                    <Img
                        className="size-full max-h-fold object-cover"
                        image={bgImage}
                        imageWidth={1800}
                        draggable={false}
                    />
                </picture>
            )}

            {content && (
                <div className="section relative z-10 flex w-full flex-col">
                    <div
                        className={cn(
                            'richtext relative isolate max-w-xl [&_:is(h1,h2)]:text-balance',
                            (hasImage || hasVideo) && 'text-shadow',
                            hasImage && css.txt,
                            {
                                'mb-8': stegaClean(alignItems) === 'start',
                                'my-auto': stegaClean(alignItems) === 'center',
                                'mt-auto': stegaClean(alignItems) === 'end',
                            },
                            {
                                'mr-auto': stegaClean(textAlign) === 'left',
                                'mx-auto': stegaClean(textAlign) === 'center',
                                'ml-auto': stegaClean(textAlign) === 'right',
                            },
                        )}
                        style={{ textAlign: stegaClean(textAlign) }}
                    >
                        <Pretitle
                            className={cn((hasImage || hasVideo) && 'text-canvas/70')}
                        >
                            {pretitle}
                        </Pretitle>
                        <PortableText value={content} />
                        <CTAList
                            ctas={ctas}
                            className={cn('!mt-4', {
                                'justify-start': stegaClean(textAlign) === 'left',
                                'justify-center': stegaClean(textAlign) === 'center',
                                'justify-end': stegaClean(textAlign) === 'right',
                            })}
                        />
                    </div>
                </div>
            )}
        </section>
    )
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật