From e9c800ab95165084e9cd1da84ea3c8060b147b52 Mon Sep 17 00:00:00 2001 From: RizqiSyahrendra Date: Tue, 11 Mar 2025 16:48:30 +0700 Subject: [PATCH] fix: category pages integration --- src/app/(main)/category/[...path]/page.tsx | 97 ++++++++++++++++++++-- src/components/Blogs/Blogs.tsx | 28 +++++-- src/components/Pagination.tsx | 9 +- src/services/payload/blog.ts | 24 +++++- src/utils/metadata.ts | 1 - 5 files changed, 139 insertions(+), 20 deletions(-) diff --git a/src/app/(main)/category/[...path]/page.tsx b/src/app/(main)/category/[...path]/page.tsx index 5a6b0c5..c1dd242 100644 --- a/src/app/(main)/category/[...path]/page.tsx +++ b/src/app/(main)/category/[...path]/page.tsx @@ -2,14 +2,24 @@ import { BeforeFooterBlock } from "@/components/Blocks/BeforeFooter"; import { BlogCardItemSkeleton } from "@/components/Blogs/BlogCardItem"; import Blogs from "@/components/Blogs/Blogs"; import HeroOther from "@/components/HeroOther"; +import { fetchBlogCategoryBySlug } from "@/services/payload/blog"; import { getDefaultMetadata } from "@/utils/metadata"; import { sanitizePageNumber } from "@/utils/sanitize"; import { Metadata } from "next"; -// import { headers } from "next/headers"; +import { headers } from "next/headers"; +import { notFound } from "next/navigation"; import { Suspense } from "react"; -export async function generateMetadata(): Promise { +export async function generateMetadata({ params }: { params: Promise<{ path: string[0] }> }): Promise { + const path = (await params)?.path; + const paramsCategory = path?.[0] ?? ""; const metadata = await getDefaultMetadata(); + + const category = await fetchBlogCategoryBySlug(paramsCategory); + if (!!category?.data) { + metadata.title = `${category.data.name} Archives - ${metadata.openGraph?.siteName}`; + } + return metadata; } @@ -21,19 +31,92 @@ export default async function CategoryPage({ searchParams?: Promise<{ page?: string; s?: string }>; }) { const path = (await params)?.path; - // const headersList = await headers(); - // const paramsCategory = path?.[0] ?? ""; + const paramsCategory = path?.[0] ?? ""; const paramsPage = path?.[2] ?? ""; const paramsSearch = (await searchParams)?.s; const page = sanitizePageNumber(paramsPage); + const headersList = await headers(); + const mainUrl = headersList.get("x-main-url"); + const fullUrl = headersList.get("x-full-url"); + const siteName = headersList.get("x-site-name"); + + const category = await fetchBlogCategoryBySlug(paramsCategory); + if (!category) { + notFound(); + } + + const jsonLd = { + "@context": "https://schema.org", + "@graph": [ + { + "@type": "CollectionPage", + "@id": fullUrl, + url: fullUrl, + name: `${category.data.name} - ${siteName}`, + isPartOf: { + "@id": `${mainUrl}/#website`, + }, + primaryImageOfPage: { + "@id": `${fullUrl}#primaryimage`, + }, + image: { + "@id": `${fullUrl}#primaryimage`, + }, + inLanguage: "en-US", + }, + { + "@type": "WebSite", + "@id": `${mainUrl}/#website`, + url: `${mainUrl}/`, + name: siteName, + description: "", + publisher: { + "@id": `${mainUrl}/#organization`, + }, + potentialAction: [ + { + "@type": "SearchAction", + target: { + "@type": "EntryPoint", + urlTemplate: `${mainUrl}/?s={search_term_string}`, + }, + "query-input": { + "@type": "PropertyValueSpecification", + valueRequired: true, + valueName: "search_term_string", + }, + }, + ], + inLanguage: "en-US", + }, + { + "@type": "Organization", + "@id": `${mainUrl}/#organization`, + name: siteName, + url: `${mainUrl}/`, + logo: { + "@type": "ImageObject", + inLanguage: "en-US", + "@id": `${mainUrl}/#/schema/logo/image/`, + url: `${mainUrl}/assets/images/logo-dark.webp`, + contentUrl: `${mainUrl}/assets/images/logo-dark.webp`, + caption: siteName, + }, + image: { + "@id": `${mainUrl}/#/schema/logo/image/`, + }, + }, + ], + }; return ( <> - +