From 7f71d52e2e6e6e8ffb5ea2837be84c800d04ef95 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 28 Oct 2025 12:26:03 +0700 Subject: canonical product detail --- src-migrate/components/seo.tsx | 87 +++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 22 deletions(-) (limited to 'src-migrate/components') diff --git a/src-migrate/components/seo.tsx b/src-migrate/components/seo.tsx index 1e78ed4d..0b522859 100644 --- a/src-migrate/components/seo.tsx +++ b/src-migrate/components/seo.tsx @@ -1,32 +1,75 @@ -import { useRouter } from 'next/router' -import React from 'react' -import { NextSeo } from "next-seo" +import React from 'react'; +import { useRouter } from 'next/router'; +import { NextSeo } from 'next-seo'; import { MetaTag, NextSeoProps } from 'next-seo/lib/types'; export const Seo = (props: NextSeoProps) => { - const router = useRouter() + const router = useRouter(); - const additionalMetaTags: MetaTag[] = [ - { - property: 'fb:app_id', - content: '270830718811' - }, - { - property: 'fb:page_id', - content: '101759953569' - }, - ] + const { + canonical, + description, + additionalMetaTags: userMetaTags, + openGraph: userOpenGraph, + ...restProps + } = props; - if (!!props.additionalMetaTags) additionalMetaTags.push(...props.additionalMetaTags) + // base domain dari env, buang trailing slash biar rapi + const origin = (process.env.NEXT_PUBLIC_SELF_HOST || '').replace(/\/+$/, ''); + + // path sekarang, contoh: + // "/shop/category/bor?page=2&sort=popular" + const asPath = router.asPath || ''; + const [cleanPath] = asPath.split('?'); // "/shop/category/bor" + + const queryObj = router.query || {}; + + // deteksi kalo ini halaman search + // kalau route search lo beda, ganti prefix ini + const isSearchPage = cleanPath.startsWith('/search'); + + // fallback canonical buat halaman yang TIDAK ngasih canonical prop sendiri + const buildFallbackCanonical = () => { + if (isSearchPage) { + const q = queryObj.q; + if (q) { + // search punya intent unik per q, tapi kita buang param lain kayak page/sort + return origin + cleanPath + `?q=${encodeURIComponent(String(q))}`; + } + return origin + cleanPath; + } + + // kategori/brand/listing biasa -> tanpa query sama sekali + return origin + cleanPath; + }; + + // Prioritas final: + // 1. kalau page ngasih props.canonical -> pakai itu + // 2. kalau gak -> pakai fallback + const resolvedCanonical = canonical || buildFallbackCanonical(); + + // gabung meta default FB + custom + const mergedMetaTags: MetaTag[] = [ + { property: 'fb:app_id', content: '270830718811' }, + { property: 'fb:page_id', content: '101759953569' }, + ]; + if (userMetaTags && Array.isArray(userMetaTags)) { + mergedMetaTags.push(...userMetaTags); + } return ( - ) -} \ No newline at end of file + ); +}; + +export default Seo; -- cgit v1.2.3