1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.js 16 App Routerで構造化データ(JSON-LD)を入れる — SoftwareApplication / FAQPage / Article の実装

1
Posted at

この記事の要点(TL;DR)

  • 個人開発の無料ツールサイト(Next.js 16 / App Router)に、schema.org の構造化データ(JSON-LD)を1件も入れていなかったことに気づいて実装した。
  • サイト全体に Organization / WebSite、ツールページに BreadcrumbList / SoftwareApplication / FAQPage、記事ページに BreadcrumbList / Article を入れた。
  • 実装は lib/jsonld.ts に生成関数を集約し、各ページで <script type="application/ld+json"> として出力する形にした。
  • 評価・レビュー(aggregateRating)のような実在しないデータは捏造しない。Googleのガイドライン違反になるため。

App Router での JSON-LD の入れ方と、ツールサイトで使える型の選び方をまとめます。

前提:なぜ構造化データを入れるのか

構造化データは、ページの内容を検索エンジンに機械可読な形で伝えるマークアップです。順位を直接上げる魔法ではありませんが、

  • パンくず表示、FAQのリッチリザルトなど検索結果での見え方が変わる可能性がある
  • サイトの構造をエンジンが理解しやすくなる

ので、コンテンツ型のサイトなら入れておいて損はありません。うちのサイトは新規ドメインで順位が伸び悩んでいたので、「コンテンツ以外でやれることを全部やる」の一環として実装しました。

設計:生成関数を1ファイルに集約する

型ごとにバラバラに書くと散らかるので、lib/jsonld.ts にオブジェクトを返す関数を集めました。

// lib/jsonld.ts
const SITE_URL = "https://shibahub.jp";
const SITE_NAME = "ShibaHub";

// JSON文字列化 + XSS対策で "<" をエスケープ
export function jsonLdScript(data: unknown) {
  return JSON.stringify(data).replace(/</g, "\\u003c");
}

export function organizationJsonLd() {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    name: SITE_NAME,
    url: SITE_URL,
    logo: `${SITE_URL}/icon.svg`,
  };
}

export function websiteJsonLd() {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: SITE_NAME,
    url: SITE_URL,
    inLanguage: "ja",
  };
}

ポイントは jsonLdScript() で < を < にエスケープしていること。JSON-LD を dangerouslySetInnerHTML で埋め込むとき、データ内に </script> のような文字列があるとタグが閉じてしまうリスクがあるので、< を潰しておくのが定番の防御です。

ツールページ:SoftwareApplication と FAQPage

計算ツールのような「Webアプリ」は SoftwareApplication が合います。パンくずと、FAQがあるページだけ FAQPage も足します。

export function toolJsonLd(tool: ToolConfig) {
  const url = `${SITE_URL}/tools/${tool.slug}`;
  const cat = CATEGORIES[tool.category];

  const breadcrumb = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: [
      { name: "ホーム", url: SITE_URL },
      { name: cat.label, url: `${SITE_URL}/category/${tool.category}` },
      { name: tool.title, url },
    ].map((item, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: item.name,
      item: item.url,
    })),
  };

  const softwareApplication = {
    "@context": "https://schema.org",
    "@type": "SoftwareApplication",
    name: tool.title,
    description: tool.description,
    url,
    applicationCategory: "UtilitiesApplication",
    operatingSystem: "Any",
    offers: { "@type": "Offer", price: "0", priceCurrency: "JPY" }, // 無料なので price: "0"
  };

  // FAQは「あるページだけ」入れる。無いのに空で入れない
  const faqPage =
    tool.faq.length > 0
      ? {
          "@context": "https://schema.org",
          "@type": "FAQPage",
          mainEntity: tool.faq.map((item) => ({
            "@type": "Question",
            name: item.question,
            acceptedAnswer: { "@type": "Answer", text: item.answer },
          })),
        }
      : null;

  return [breadcrumb, softwareApplication, faqPage].filter((x) => x !== null);
}

配列で返して null を除外しているので、FAQがあるツールは3つ、ないツールは2つのJSON-LDが出力されます。

記事ページ:Article

解説記事には Article を使います。著者・発行者はサイト運営者(Organization)にしています。

export function articleJsonLd(article: ArticleConfig) {
  const url = `${SITE_URL}/articles/${article.slug}`;
  const cat = CATEGORIES[article.category];

  const articleSchema = {
    "@context": "https://schema.org",
    "@type": "Article",
    headline: article.title,
    description: article.description,
    url,
    datePublished: article.publishedAt,
    dateModified: article.publishedAt,
    articleSection: cat.label,
    author: { "@type": "Organization", name: SITE_NAME },
    publisher: {
      "@type": "Organization",
      name: SITE_NAME,
      logo: { "@type": "ImageObject", url: `${SITE_URL}/icon.svg` },
    },
    mainEntityOfPage: { "@type": "WebPage", "@id": url },
  };
  // breadcrumb は省略(ツールと同様に作る)
  return [articleSchema];
}

App Routerでの出力方法

App Router では、<script type="application/ld+json"> をそのままJSXで書けます。next/head は不要です。

サイト全体(app/layout.tsx)に入れる分:

// app/layout.tsx
import { jsonLdScript, organizationJsonLd, websiteJsonLd } from "@/lib/jsonld";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="ja">
      <body>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: jsonLdScript(organizationJsonLd()) }}
        />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: jsonLdScript(websiteJsonLd()) }}
        />
        {children}
      </body>
    </html>
  );
}

ページ側は、配列で返ってくるので .map() で複数の <script> を出します。

// components/ToolPageLayout.tsx(抜粋)
import { jsonLdScript, toolJsonLd } from "@/lib/jsonld";

export function ToolPageLayout({ tool, calculator }: Props) {
  return (
    <>
      {toolJsonLd(tool).map((data, i) => (
        <script
          key={i}
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: jsonLdScript(data) }}
        />
      ))}
      {/* 本体 */}
    </>
  );
}

これらのページはサーバーコンポーネント(静的生成)なので、JSON-LD はビルド時にHTMLへ焼き込まれます。クライアントJSに依存しないので、クローラーが確実に読めます。実際に本番の /tools/* /articles/* を curl してJSON-LDが出力されていることも確認しました。

やってはいけないこと:存在しないデータの捏造

SoftwareApplication には aggregateRating(★評価)を付けられます。付けると検索結果に星が出て目立つので魅力的ですが、実際にレビュー機能もレビューデータも無いのに書くのはガイドライン違反です。最悪、手動対策(ペナルティ)の対象になります。

なので、うちでは aggregateRating や review は一切入れていません。「あると得だが実体がないもの」は入れない、が鉄則です。

まとめ

  • JSON-LD の生成は lib/jsonld.ts に集約し、各ページで <script type="application/ld+json"> として出力。App Router なら next/head 不要でJSXにそのまま書ける。
  • ツールは SoftwareApplication、FAQがあるページだけ FAQPage、記事は Article。パンくずは全ページ BreadcrumbList。
  • jsonLdScript() で < をエスケープして </script> 事故を防ぐ。
  • aggregateRating など実在しないデータは絶対に捏造しない。

題材にした個人開発サイトはこちら → https://shibahub.jp

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?