SEO.jsx 2.51 KB
Newer Older
fisherdaddy's avatar
fisherdaddy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// src/components/SEO.jsx
import React from 'react';
import { Helmet } from 'react-helmet';
import { useTranslation } from '../js/i18n';

function SEO({ title, description, lang = 'en', meta = [] }) {
  const { t } = useTranslation();

  const defaultTitle = t('title');
  const defaultDescription = t('description'); // 确保在i18n配置中添加'description'

  const languages = ['en', 'zh', 'ja', 'ko'];
  const hostname = 'https://fishersama.com'; // 替换为您的网站域名

  const links = languages.map((language) => ({
    rel: 'alternate',
    hrefLang: language,
    href: `${hostname}/${language === 'en' ? '' : language}`,
  }));

  const structuredData = {
    "@context": "https://schema.org",
fisherdaddy's avatar
fisherdaddy committed
23
    "@type": "SoftwareApplication",
fisherdaddy's avatar
fisherdaddy committed
24 25 26 27 28 29 30
    "name": defaultTitle,
    "url": "https://fishersama.com/", // 请替换为您的网站URL
    "description": defaultDescription,
    "potentialAction": {
      "@type": "SearchAction",
      "target": "https://fishersama.com/search?q={search_term}",
      "query-input": "required name=search_term"
fisherdaddy's avatar
fisherdaddy committed
31 32 33 34 35
    },
    "offers": {
      "@type": "Offer",
      "price": "0",
      "priceCurrency": "USD"
fisherdaddy's avatar
fisherdaddy committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    }
  };

  return (
    <Helmet
      htmlAttributes={{
        lang,
      }}
      title={title || defaultTitle}
      titleTemplate={`%s | ${defaultTitle}`}
      meta={[
        {
          name: 'description',
          content: description || defaultDescription,
        },
fisherdaddy's avatar
fisherdaddy committed
51 52 53 54 55 56 57 58
        {
          name: 'keywords',
          content: t('keywords'), // 确保在 i18n 配置中添加 'keywords'
        },
        {
          name: 'viewport',
          content: 'width=device-width, initial-scale=1',
        },
fisherdaddy's avatar
fisherdaddy committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        {
          property: 'og:title',
          content: title || defaultTitle,
        },
        {
          property: 'og:description',
          content: description || defaultDescription,
        },
        {
          property: 'og:type',
          content: 'website',
        },
        {
          name: 'twitter:card',
          content: 'summary',
        },
        {
          name: 'twitter:title',
          content: title || defaultTitle,
        },
        {
          name: 'twitter:description',
          content: description || defaultDescription,
        },
fisherdaddy's avatar
fisherdaddy committed
83 84 85 86
        {
          name: 'robots',
          content: 'index,follow',
        },
fisherdaddy's avatar
fisherdaddy committed
87 88 89 90 91 92 93 94 95 96 97 98
        // 可以根据需要添加更多元数据
      ].concat(meta)}
      link={links}
    >
      <script type="application/ld+json">
        {JSON.stringify(structuredData)}
      </script>
    </Helmet>
  );
}

export default SEO;