You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google富媒体结果测试工具解析错误排查(Next.js站点)

问题分析与解决方案

核心问题根源

  1. 双引号被重复转义:你提供的Schema代码里的"是HTML转义后的双引号,而Google工具解析出的"是二次转义的结果——这说明在Next.js中输出Schema时,JSON字符串被HTML自动转义了,导致Google拿到的不是合法的JSON结构,进而引发解析错误。
  2. Schema冲突/重复输出:Google解析出了Corporation类型的Schema,而你提交的是WebSite类型,说明你的页面上可能已经存在另一份Corporation类型的Schema,或者输出逻辑导致Schema被重复渲染,进一步打乱了JSON结构。

解决方案

1. 在Next.js中正确输出JSON-LD Schema

不要直接在JSX中输出JSON字符串,而是用dangerouslySetInnerHTML避免HTML转义。示例代码如下:

import Head from 'next/head';

export default function YourPageOrAppComponent({ Component, pageProps }) {
  // 定义原始Schema对象
  const websiteSchema = {
    "@context": "https://schema.org/",
    "@type": "WebSite",
    "name": "Bharat Pharmatech",
    "url": "https://bharatpharmatech.com",
    "potentialAction": {
      "@type": "SearchAction",
      "target": "https://bharatpharmatech.com/search/{search_term_string}",
      "query-input": "required name=search_term_string"
    }
  };

  return (
    <>
      <Head>
        {/* 用dangerouslySetInnerHTML直接输出未转义的JSON */}
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(websiteSchema) }}
        />
      </Head>
      <Component {...pageProps} />
    </>
  );
}

2. 排查页面中的重复Schema

  • 部署后查看页面源码,搜索所有<script type="application/ld+json">标签,确认是否同时存在WebSite和Corporation类型的Schema。
  • 如果需要同时使用两种类型,可以将它们合并到一个JSON数组中,示例:
[
  {
    "@context": "https://schema.org/",
    "@type": "WebSite",
    "name": "Bharat Pharmatech",
    "url": "https://bharatpharmatech.com",
    "potentialAction": {
      "@type": "SearchAction",
      "target": "https://bharatpharmatech.com/search/{search_term_string}",
      "query-input": "required name=search_term_string"
    }
  },
  {
    "@context": "https://schema.org/",
    "@type": "Corporation",
    "name": "Bharat Pharmatech",
    "alternateName": "Pharma Machinery & Spares",
    "url": "https://bharatpharmatech.com",
    "logo": "https://res.cloudinary.com/dzainnrtc/image/upload/v1677398892/apple-touch-icon_cvh6w8.png",
    "contactPoint": [
      {
        "@type": "ContactPoint",
        "telephone": "9687849837",
        "contactType": "customer service",
        "contactOption": "TollFree",
        "areaServed": "IN",
        "availableLanguage": ["en", "Hindi", "Marathi", "Gujarati"]
      },
      {
        "@type": "ContactPoint",
        "telephone": "0901507751",
        "contactType": "technical support",
        "contactOption": "TollFree",
        "areaServed": "IN",
        "availableLanguage": ["en", "Hindi", "Marathi", "Gujarati"]
      }
    ],
    "sameAs": [
      "www.bharatpharmatech.com",
      "https://youtube.com/@pharmamachineryspares",
      "https://instagram.com/bharatpharmatech"
    ]
  }
]

将这个合并后的对象用同样的dangerouslySetInnerHTML方式输出,确保JSON结构合法无转义。

3. 验证修复结果

  • 部署修改后的代码,查看页面源码,确认application/ld+json脚本中的内容是原始JSON(没有&quot;或&amp;quot;转义字符)。
  • 再用Google富媒体结果测试工具验证,此时解析错误应该会消失。

内容的提问来源于stack exchange,提问作者Awesh Choudhary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:21:28