From 1baa3c1f8ddced9422ed8f4217cb78bc9a7aa760 Mon Sep 17 00:00:00 2001 From: Henrik Larsson Date: Mon, 21 Aug 2023 08:13:13 +0200 Subject: [PATCH] Sanity fixes --- app/(site)/[locale]/page.tsx | 3 ++ app/api/preview/route.ts | 29 +++++++++++++--- components/modules/hero/hero.tsx | 4 +-- .../reusable-section/reusable-section.tsx | 34 ++++++++----------- components/modules/slider/slider.tsx | 2 +- components/ui/category-card/category-card.tsx | 6 ++-- components/ui/product-card/product-card.tsx | 6 ++-- components/ui/sanity-image/sanity-image.tsx | 18 ++++++---- 8 files changed, 64 insertions(+), 38 deletions(-) diff --git a/app/(site)/[locale]/page.tsx b/app/(site)/[locale]/page.tsx index 786bccd44..23d19eee4 100644 --- a/app/(site)/[locale]/page.tsx +++ b/app/(site)/[locale]/page.tsx @@ -39,10 +39,13 @@ export default async function IndexPage({ params }: HomePageParams) { notFound(); } + console.log('Preview:', draftMode().isEnabled); + return ( diff --git a/app/api/preview/route.ts b/app/api/preview/route.ts index 5f1dce999..92ef94ef3 100644 --- a/app/api/preview/route.ts +++ b/app/api/preview/route.ts @@ -1,4 +1,10 @@ +import { previewSecretId } from '@/lib/sanity/sanity.api' +import { client } from '@/lib/sanity/sanity.client' +import { token } from '@/lib/sanity/sanity.fetch' import { draftMode } from 'next/headers' +import { isValidSecret } from 'sanity-plugin-iframe-pane/is-valid-secret' + +export const runtime = 'edge' export async function GET(request: Request) { const { searchParams } = new URL(request.url) @@ -7,10 +13,25 @@ export async function GET(request: Request) { const type = searchParams.get('type') const locale = searchParams.get('locale') - // Check the secret and next parameters - // This secret should only be known to this route handler and the CMS - if (secret !== process.env.SANITY_API_READ_TOKEN) { - return new Response('Invalid token', { status: 401 }) + if (!token) { + throw new Error( + 'The `SANITY_API_READ_TOKEN` environment variable is required.', + ) + } + if (!secret) { + return new Response('Invalid secret', { status: 401 }) + } + + const authenticatedClient = client.withConfig({ token }) + + const validSecret = await isValidSecret( + authenticatedClient, + previewSecretId, + secret, + ) + + if (!validSecret) { + return new Response('Invalid secret', { status: 401 }) } draftMode().enable() diff --git a/components/modules/hero/hero.tsx b/components/modules/hero/hero.tsx index 4cfb2beb7..f3bf3ee8d 100644 --- a/components/modules/hero/hero.tsx +++ b/components/modules/hero/hero.tsx @@ -31,8 +31,6 @@ const heroSize = { const Hero = ({ variant, title, text, label, image, link, color, overlay }: HeroProps) => { const heroClass = heroSize[variant as HeroSize] || heroSize.fullScreen; - console.log(image); - return (
)} - {overlay &&
} + {overlay &&
}
{ + const data = section.existingSection.section.sectionType; + + if (!data) { + return; } -} -const ReusableSection = ({ section }:ReusableSectionProps) => { -const data = section.existingSection.section.sectionType; + return ; +}; -if (!data) { - return; -} - - return ( - - ) -} - -export default ReusableSection; \ No newline at end of file +export default ReusableSection; diff --git a/components/modules/slider/slider.tsx b/components/modules/slider/slider.tsx index 7c0194d6a..5f7427574 100644 --- a/components/modules/slider/slider.tsx +++ b/components/modules/slider/slider.tsx @@ -3,9 +3,9 @@ import Text from 'components/ui/text'; import { useEffect, useState } from 'react'; +import { Carousel, CarouselItem } from '@/components/modules/carousel/carousel'; import CategoryCard from '@/components/ui/category-card/category-card'; import ProductCard from '@/components/ui/product-card/product-card'; -import { Carousel, CarouselItem } from '@/components/modules/carousel/carousel'; interface SliderProps { products: [] | any; diff --git a/components/ui/category-card/category-card.tsx b/components/ui/category-card/category-card.tsx index 33ba36794..9909ac7a1 100644 --- a/components/ui/category-card/category-card.tsx +++ b/components/ui/category-card/category-card.tsx @@ -20,9 +20,9 @@ const CategoryCard: FC = ({ category, className }) => {
{category.title} diff --git a/components/ui/product-card/product-card.tsx b/components/ui/product-card/product-card.tsx index e87d096a4..861e59b69 100644 --- a/components/ui/product-card/product-card.tsx +++ b/components/ui/product-card/product-card.tsx @@ -25,12 +25,14 @@ const ProductCard: FC = ({ product, className, variant = 'default' }) => > {variant === 'default' && (
-
+
{product?.images && ( )}
diff --git a/components/ui/sanity-image/sanity-image.tsx b/components/ui/sanity-image/sanity-image.tsx index c6bfeb174..4594d7ac2 100644 --- a/components/ui/sanity-image/sanity-image.tsx +++ b/components/ui/sanity-image/sanity-image.tsx @@ -1,3 +1,4 @@ +import { cn } from '@/lib/utils'; import { urlForImage } from 'lib/sanity/sanity.image'; import Image from 'next/image'; @@ -16,37 +17,42 @@ interface SanityImageProps { export default function SanityImage({ image, - alt = 'Cover image', + alt = '', width = 3500, height = 2000, size = '100vw', fill = false, + priority = false, className }: SanityImageProps) { const imageUrl = image && urlForImage(image)?.height(height).width(width).fit('crop').url(); - console.log(imageUrl); - return ( -
+
{fill && imageUrl && ( {alt )} {imageUrl && ( {alt )}