All files / src/components/SEO index.tsx

84.61% Statements 11/13
37.03% Branches 10/27
100% Functions 2/2
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                                      2x                                             2x     2x                           2x                                   2x                                                                                                                                               2x     2x                           2x         2x           2x       2x                                                                                                                                                                                                    
import React from "react";
import Helmet from "react-helmet";
import config from "@/config/index.json";
 
type Props = {
  url: string;
  title?: string;
  siteTitleAlt: string;
  isPost: boolean;
  image: string;
  tag: string;
  description: string;
  noindex?: boolean;
  datePublished?: string;
  dateModified?: string;
  author?: string;
  keywords?: string[];
};
 
const schemaOrgJSONLD = ({
  url,
  title,
  siteTitleAlt,
  isPost,
  image,
  tag,
  description,
  datePublished,
  dateModified,
  author,
}: {
  url: string;
  title: string;
  siteTitleAlt: string;
  isPost: boolean;
  image: string;
  tag: string;
  description: string;
  datePublished?: string;
  dateModified?: string;
  author?: string;
}) => {
  const returnJson = [];
 
  // WebSite schema
  returnJson.push({
    "@context": "https://schema.org",
    "@type": "WebSite",
    "@id": `${config.siteUrl}/#website`,
    url: config.siteUrl,
    name: config.siteTitle,
    description: config.description,
    publisher: {
      "@id": `${config.siteUrl}/#person`,
    },
    inLanguage: "ja-JP",
  });
 
  // Person schema
  returnJson.push({
    "@context": "https://schema.org",
    "@type": "Person",
    "@id": `${config.siteUrl}/#person`,
    name: author || config.author,
    url: `${config.siteUrl}/about`,
    image: {
      "@type": "ImageObject",
      "@id": `${config.siteUrl}/#personImage`,
      url: `${config.siteUrl}/favicons/android-chrome-512x512.png`,
      width: 512,
      height: 512,
      caption: config.author,
    },
    sameAs: ["https://github.com/tubone24", "https://twitter.com/tubone24"],
    jobTitle: "Software Engineer",
  });
 
  Iif (isPost) {
    // Enhanced BreadcrumbList
    returnJson.push({
      "@context": "https://schema.org",
      "@type": "BreadcrumbList",
      "@id": `${url}#breadcrumb`,
      itemListElement: [
        {
          "@type": "ListItem",
          position: 1,
          item: {
            "@id": config.siteUrl,
            name: "ホーム",
          },
        },
        {
          "@type": "ListItem",
          position: 2,
          item: {
            "@id": `${config.siteUrl}/tags`,
            name: "タグ一覧",
          },
        },
        {
          "@type": "ListItem",
          position: 3,
          item: {
            "@id": `${config.siteUrl}/tag/${tag}`,
            name: tag,
          },
        },
        {
          "@type": "ListItem",
          position: 4,
          name: title,
        },
      ],
    });
 
    // Enhanced BlogPosting
    returnJson.push({
      "@context": "https://schema.org",
      "@type": "BlogPosting",
      "@id": `${url}#article`,
      mainEntityOfPage: {
        "@type": "WebPage",
        "@id": url,
      },
      url,
      name: title,
      headline: title,
      description,
      image: {
        "@type": "ImageObject",
        url: image,
        width: 1200,
        height: 630,
      },
      datePublished: datePublished || new Date().toISOString(),
      dateModified: dateModified || datePublished || new Date().toISOString(),
      author: {
        "@type": "Person",
        "@id": `${config.siteUrl}/#person`,
      },
      publisher: {
        "@type": "Person",
        "@id": `${config.siteUrl}/#person`,
      },
      inLanguage: "ja-JP",
      keywords: tag,
    });
  }
  return returnJson;
};
 
const SEO = ({
  url,
  title = config.siteTitle,
  description,
  image,
  siteTitleAlt,
  isPost,
  tag,
  noindex = false,
  datePublished,
  dateModified,
  author,
  keywords = [],
}: Props) => {
  const ogpImageLink = `https://tubone-project24.xyz/ogp.png?title=${encodeURI(
    title
  )}`;
 
  // 日本語キーワードの自動追加
  const japaneseKeywords = [
    "技術ブログ",
    "プログラミング",
    "開発",
    "エンジニアリング",
  ];
  const allKeywords = [...keywords, ...japaneseKeywords, tag]
    .filter(Boolean)
    .join(", ");
 
  return (
    <Helmet>
      <title>{title}</title>
 
      {/* General tags */}
      <meta name="description" content={description} />
      <meta name="image" content={image} />
      <meta name="keywords" content={allKeywords} />
      <meta name="author" content={author || config.author} />
      {noindex && <meta name="robots" content="noindex, nofollow" />}
 
      {/* カノニカルURL */}
      <link rel="canonical" href={url} />
 
      {/* 電話番号自動認識の無効化 */}
      <meta name="format-detection" content="telephone=no" />
 
      {/* Apple固有のメタタグ */}
      <meta name="apple-mobile-web-app-title" content={title} />
      <meta name="apple-mobile-web-app-capable" content="yes" />
      <meta
        name="apple-mobile-web-app-status-bar-style"
        content="black-translucent"
      />
 
      {/* Microsoft固有のメタタグ */}
      <meta name="application-name" content={title} />
      <meta name="msapplication-TileColor" content="#33b546" />
      <meta
        name="msapplication-TileImage"
        content="/favicons/mstile-150x150.png"
      />
 
      {/* Schema.org tags */}
      <script type="application/ld+json">
        {JSON.stringify(
          schemaOrgJSONLD({
            url,
            title,
            siteTitleAlt,
            isPost,
            image,
            description,
            tag,
            datePublished,
            dateModified,
            author,
          })
        )}
      </script>
 
      {/* OpenGraph tags */}
      <meta property="og:url" content={url} />
      <meta property="og:site_name" content={config.siteTitle} />
      {isPost ? (
        <meta property="og:type" content="article" />
      ) : (
        <meta property="og:type" content="website" />
      )}
      <meta property="og:title" content={title} />
      <meta property="og:description" content={description} />
      <meta property="og:image" content={ogpImageLink} />
      <meta property="og:image:width" content="1200" />
      <meta property="og:image:height" content="630" />
      <meta property="og:image:alt" content={title} />
      <meta property="og:locale" content="ja_JP" />
      <meta property="fb:app_id" content="280941406476272" />
 
      {/* Article specific tags */}
      {isPost && (
        <>
          <meta
            property="article:published_time"
            content={datePublished || new Date().toISOString()}
          />
          <meta
            property="article:modified_time"
            content={dateModified || datePublished || new Date().toISOString()}
          />
          <meta property="article:author" content={author || config.author} />
          <meta property="article:section" content={tag} />
          <meta property="article:tag" content={tag} />
        </>
      )}
 
      {/* Twitter Card tags */}
      <meta name="twitter:card" content="summary_large_image" />
      <meta name="twitter:site" content="@tubone24" />
      <meta name="twitter:creator" content="@tubone24" />
      <meta name="twitter:title" content={title} />
      <meta name="twitter:description" content={description} />
      <meta name="twitter:image" content={ogpImageLink} />
      <meta name="twitter:image:alt" content={title} />
    </Helmet>
  );
};
 
export default SEO;