【问题标题】:Is there anyway to use JSON-LD Schema not inlined无论如何要使用未内联的 JSON-LD Schema
【发布时间】:2016-05-23 01:00:33
【问题描述】:

无论如何使用 JSON-LD 而不在 HTML 中包含 script 内联,但仍然让谷歌(和其他)蜘蛛找到它?环顾四周,我看到了一些相互矛盾的信息。

如果这是 JSON-LD 文件:

    <script type="application/ld+json">
    {
      "@context" : "http://schema.org",
      "@type" : "WebSite",
      "name" : "Example Site",
      "alternateName" : "example",
      "description" : "Welcome to this WebSite",
      "headline" : "Welcome to Website",
      "logo" : "https://example.com/public/images/logo.png",
      "url" : "https://example.com/"
    }
    </script>

我在 HTML 的 head 中有这个:

<script src="/public/json-ld.json" type="application/ld+json"></script>

编辑:我也试过:

<link href="/public/json-ld.json" rel="alternate" type="application/ld+" />

Google Spiders 似乎错过了它,测试工具也是如此,除非我将它直接指向文件。我正在尝试解决 CSP 中的 unsafe-inline 问题。还有only thing I can find is this,它可以在 Chrome 中工作,但不想在其他所有浏览器上触发控制台错误。另外,我只是喜欢将 Schema.org 数据从页面结构中抽象出来的想法。将 JSON-LD 添加到 Google 网站管理员工具的站点地图会有帮助吗?

抱歉,对 JSON-lD 完全陌生,并且不断出现在电子邮件文档(这将是针对网站)或旧文档中。

【问题讨论】:

标签: seo schema.org content-security-policy json-ld


【解决方案1】:

没错,它不能被外部化,也不支持内联,但你仍然可以通过 JavaScript 文件将它注入到 DOM 中来实现你想要的。

注意:为了整洁,我使用了一个数组,因此我可以分割所有结构化数据元素并添加无限数量的它们。我的网站上有这个代码的更复杂版本,实际上有一个伪装成 JavaScript 文件的外部服务器端渲染文件。

是的,Google 搜索机器人确实理解它。注册可能需要几天时间,使用网站管理员工具强制重新抓取似乎不会强制刷新 JSON-LD 数据 - 似乎您只需要等待。

var structuredData = {
    schema: {
        corporation: {
            '@context':         'http://schema.org',
            '@type':            'Corporation',
            'name':             'Acme',
            'url':              'https://acme.com',
            'contactPoint':
            {
                '@type':        'ContactPoint',
                'telephone':    '+1-1234-567-890',
                'contactType':  'customer service',
                'areaServed':   'US'
            }
        },
        service: {
            '@context':         'http://schema.org/',
            '@type':            'Service',
            'name':             'Code optimization',
            'serviceOutput' :   'Externalized json',
            'description':      'Inline json to externalized json'
        },
    },
    init: function () {
        var g = [];
        var sd = structuredData;
        g.push(sd.schema.corporation);
        g.push(sd.schema.service);
        //etc.

        var o = document.createElement('script');
        o.type = 'application/ld+json';
        o.innerHTML = JSON.stringify(g);
        var d = document; (d.head || d.body).appendChild(o);
    }
}
structuredData.init();

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 2017-03-01
    • 1970-01-01
    • 2019-12-30
    • 2023-03-16
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多