1 import type { Metadata } from 'next'; 2 import { notFound } from 'next/navigation'; 3 import { db } from '@/lib/db'; 4 import { getStock } from '@/lib/inventory'; 5 import { AddToCart } from './add-to-cart'; 6 7 type Props = { params: Promise<{ slug: string }> }; 8 9 export const dynamicParams = false; 10 11 const getProduct = (slug: string) => db.product.findUnique({ where: { slug } }); 12 13 export async function generateStaticParams() { 14 const products = await db.product.findMany({ select: { slug: true } }); 15 return products.map((product) => ({ slug: product.slug })); 16 } 17 18 export async function generateMetadata({ params }: Props): Promise<Metadata> { 19 const { slug } = await params; 20 const product = await getProduct(slug); 21 if (!product) notFound(); 22 23 return { title: product.name, description: product.summary }; 24 } 25 26 export default async function ProductPage({ params }: Props) { 27 const { slug } = await params; 28 const product = await getProduct(slug); 29 if (!product) notFound(); 30 31 const stock = await getStock(product.id); 32 const jsonLd = { '@context': 'https://schema.org', '@type': 'Product', name: product.name, description: product.summary }; 33 34 return ( 35 <article> 36 <script 37 type="application/ld+json" 38 dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} 39 /> 40 <h1>{product.name}</h1> 41 <p>{product.summary}</p> 42 <p>{stock > 0 ? `${stock} in stock` : 'Out of stock'}</p> 43 <AddToCart productId={product.id} disabled={stock === 0} /> 44 </article> 45 ); 46 }