From e0959e1d158957d0e54cd4072baa5f0d24e182ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B5=ED=9D=AC=20=EA=B9=80?= Date: Thu, 2 Jan 2025 20:17:40 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B9=8C=EB=93=9C=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[category]/[subCategory]/page.jsx | 141 ++++++++++++------ 1 file changed, 96 insertions(+), 45 deletions(-) diff --git a/src/app/(user)/products/[category]/[subCategory]/page.jsx b/src/app/(user)/products/[category]/[subCategory]/page.jsx index 3a115fb..dbeebd7 100644 --- a/src/app/(user)/products/[category]/[subCategory]/page.jsx +++ b/src/app/(user)/products/[category]/[subCategory]/page.jsx @@ -11,60 +11,111 @@ import * as api from 'src/services'; export const dynamic = 'error'; export const revalidate = 10; +// Static params generation with error handling export async function generateStaticParams() { - const { data } = await api.getSubCategorySlugs(); - return data?.map((cat) => { - return { - subCategory: cat.slug, - category: cat.parentCategory.slug - }; - }); + try { + const { data } = await api.getSubCategorySlugs(); + if (!data) throw new Error('No data returned from getSubCategorySlugs'); + + return data + .filter(cat => cat?.slug && cat?.parentCategory?.slug) // 필터링 추가 + .map((cat) => ({ + subCategory: cat.slug, + category: cat.parentCategory.slug, + })); + } catch (error) { + console.error('Error generating static params:', error.message); + return []; // 빈 배열 반환 + } } +// Metadata generation with error handling export async function generateMetadata({ params }) { - const { data: response } = await api.getSubCategoryBySlug(params.subCategory); + try { + const { data: response } = await api.getSubCategoryBySlug(params.subCategory); + if (!response) throw new Error('No data returned from getSubCategoryBySlug'); - return { - title: response.metaTitle, - description: response.metaDescription, - title: response.name, - openGraph: { - images: [response.cover.url] - } - }; + return { + title: response.metaTitle || 'Default Title', // 기본값 추가 + description: response.metaDescription || 'Default Description', + openGraph: { + images: [response.cover?.url || '/default-image.jpg'], // 기본 이미지 추가 + }, + }; + } catch (error) { + console.error('Error generating metadata:', error.message); + return { + title: 'Error', + description: 'An error occurred while generating metadata.', + openGraph: { + images: ['/default-image.jpg'], // 기본 이미지 + }, + }; + } } +// Main component with error handling export default async function Listing({ params }) { const { category, subCategory } = params; - const { data: subCategoryData } = await api.getSubCategoryTitle(subCategory); - return ( - - - - - - + try { + const { data: subCategoryData } = await api.getSubCategoryTitle(subCategory); + + if (!subCategoryData) throw new Error('No data found for the given subCategory'); + + return ( + + + + + + + - - ); + ); + } catch (error) { + console.error('Error rendering Listing component:', error.message); + + return ( + + + + + Error loading subcategory. Please try again later. + + + + ); + } }