高力堡科技有限公司的報價系統網站。使用 Next.js 與 Prisma 和 MariaDB。通過 SSR 增強 SEO,並包括管理報價的管理頁面。
挑戰:頁面編譯速度緩慢
挑戰:可重用數據與數據抓取緩存
generateMetadata 函數和主 Page 函數是異步運行的,這意味著我需要調用資料庫兩次來獲取項目數據:一次用於元數據,一次用於頁面。因此,我學會了使用緩存來存儲數據,並在兩個函數中重用它,這也加快了頁面編譯時間!const getData = cache(async (productName: string) => {
const product = await prisma.product.findFirst({
where: { item_code: productName },
select: {
id: true,
item_code: true,
name: true,
youtube_link: true,
features: true,
spec: true,
product_types: { select: { id: true, name: true } },
categories: { select: { id: true, name: true } },
images: {
select: { id: true, url: true, order: true },
orderBy: { order: 'asc' },
},
attachments: {
select: { id: true, file_path: true, type: true },
orderBy: { order: 'asc' },
},
// attachments: true, // To select all fields
},
})
// Check if product is not found
if (!product) {
redirect(`/products/1`)
}
return { product_data: product }
})