summaryrefslogtreecommitdiff
path: root/src-migrate/components/seo.tsx
blob: 0b522859b665b98f310c3fd2ce9669c4f5e6c501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 {
    canonical,
    description,
    additionalMetaTags: userMetaTags,
    openGraph: userOpenGraph,
    ...restProps
  } = props;

  // 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 (
    <NextSeo
      defaultTitle='Indoteknik.com: B2B Industrial Supply & Solution'
      canonical={resolvedCanonical}
      description={description}
      {...restProps}
      openGraph={{
        siteName: 'Indoteknik.com',
        ...userOpenGraph,
      }}
      additionalMetaTags={mergedMetaTags}
    />
  );
};

export default Seo;