From 1ed72b28c0ce32ba9b0e4d9af2430da7900ae96f Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Wed, 29 Oct 2025 11:16:41 +0700
Subject: <MIqdad> fix canonical path brands

---
 src/core/components/Seo.jsx | 78 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 55 insertions(+), 23 deletions(-)

(limited to 'src/core/components/Seo.jsx')

diff --git a/src/core/components/Seo.jsx b/src/core/components/Seo.jsx
index 0e224c19..66da395b 100644
--- a/src/core/components/Seo.jsx
+++ b/src/core/components/Seo.jsx
@@ -1,34 +1,66 @@
-import { NextSeo } from 'next-seo'
-import { useRouter } from 'next/router'
+import { useRouter } from 'next/router';
+import { NextSeo } from 'next-seo';
 
-const Seo = ({ ...props }) => {
-  const router = useRouter()
+const Seo = (props) => {
+  const router = useRouter();
 
-  const { additionalMetaTags = [], openGraph = [], title = '' } = props
+  const {
+    canonical,
+    description,
+    additionalMetaTags = [],
+    openGraph = {},
+    ...restProps
+  } = props;
+
+  const origin = (process.env.NEXT_PUBLIC_SELF_HOST || '').replace(/\/+$/, '');
+
+  const asPath = router.asPath || '';
+
+  const [cleanPath] = asPath.split('?');
+
+  const queryObj = router.query || {};
+
+  const isSearchPage = cleanPath.startsWith('/search');
+
+  const buildFallbackCanonical = () => {
+    if (isSearchPage) {
+      const q = queryObj.q;
+      if (q) {
+        return origin + cleanPath + `?q=${encodeURIComponent(String(q))}`;
+      }
+      return origin + cleanPath;
+    }
+
+    return origin + cleanPath;
+  };
+
+  const resolvedCanonical = canonical || buildFallbackCanonical();
+
+  const mergedAdditionalMetaTags = [
+    {
+      property: 'fb:app_id',
+      content: '270830718811',
+    },
+    {
+      property: 'fb:page_id',
+      content: '101759953569',
+    },
+    ...additionalMetaTags,
+  ];
 
   return (
     <NextSeo
       defaultTitle='Indoteknik.com: B2B Industrial Supply & Solution'
-      canonical={process.env.NEXT_PUBLIC_SELF_HOST + router.asPath}
-      description={title}
-      {...props}
+      canonical={resolvedCanonical}
+      description={description}
+      {...restProps}
       openGraph={{
         siteName: 'Indoteknik.com',
-        ...openGraph
+        ...openGraph,
       }}
-      additionalMetaTags={[
-        {
-          property: 'fb:app_id',
-          content: '270830718811'
-        },
-        {
-          property: 'fb:page_id',
-          content: '101759953569'
-        },
-        ...additionalMetaTags
-      ]}
+      additionalMetaTags={mergedAdditionalMetaTags}
     />
-  )
-}
+  );
+};
 
-export default Seo
+export default Seo;
-- 
cgit v1.2.3

