diff --git a/src/app/blog/[slug]/page.tsx b/src/app/blog/[slug]/page.tsx index b0d760e..0ce4d38 100644 --- a/src/app/blog/[slug]/page.tsx +++ b/src/app/blog/[slug]/page.tsx @@ -5,7 +5,6 @@ import { client, sanityFetch } from "../../../sanity/client"; import Link from "next/link"; import Image from "next/image"; import { Post, SanityImageAsset } from "@/sanity/sanity.types"; -import urlBuilder from "@sanity/image-url"; import {getImageDimensions} from '@sanity/asset-utils' const POST_QUERY = defineQuery(`*[_type == "post" && slug.current == $slug][0]`); @@ -29,7 +28,7 @@ export async function generateStaticParams() { export default async function PostPage(props: { params: PageParams }) { const { slug } = await props.params; - const post: Post = (await sanityFetch({ query: POST_QUERY, params: { slug }, stega: true })).data; + const post: Post = (await sanityFetch({ query: POST_QUERY, params: { slug } })).data; if (!post) { return ( @@ -41,29 +40,41 @@ export default async function PostPage(props: { params: PageParams }) { const postImageUrl = post.mainImage ? urlFor(post.mainImage)?.width(550).height(310).url() : null; - function PortableImage({ value, isInline }: { value: SanityImageAsset; isInline: boolean }) { + function dynamicHeight(originalHeight: number, originalWidth: number, isInline: boolean) { + const targetWidth = isInline ? 100 : 768; + return (targetWidth * originalHeight) / originalWidth; +} + + function PortableImage({ value, isInline }: { value: SanityImageAsset, isInline: boolean } ) { const {width, height} = getImageDimensions(value) - return ; + console.log(isInline, width, height); + return ( + = 100 ? 100 : width) : (width >= 768 ? 768 : width)} + height={dynamicHeight(height, width, isInline)} + alt={value.altText || ' '} + loading="lazy" + className="border rounded-lg shadow-md" + style={{ + display: isInline ? 'inline-block' : 'block', + aspectRatio: width / height, + }} + /> + ) + } + + const components = { + types: { + image: PortableImage + } } return ( @@ -83,7 +94,7 @@ export default async function PostPage(props: { params: PageParams }) { {post.title} Published: {new Date(post.publishedAt ?? "").toISOString().substring(0, 10)} - {Array.isArray(post.body) && } + {Array.isArray(post.body) && } );
Published: {new Date(post.publishedAt ?? "").toISOString().substring(0, 10)}