2016-02-12 49 views
7

有没有反正使用JSON-LD而不包括script内嵌在HTML中,但还是得到Google(&其他)的蜘蛛才能找到它?环顾四周,我看到了一些矛盾的信息。有没有反正使用JSON-LD Schema没有内联

如果这是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+" /> 

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

道歉,JSON-ID的总诺贝尔,并保持最终在电子邮件文档(这将为一个网站)或旧的文档。

+0

的可能的复制[不JSON-LD必须嵌入?](http://stackoverflow.com/questions/30864619/does-json-ld-have-to-be-embedded) – unor

+0

试过这个,它仍然没有被拾起。我最好的猜测是JSON-LD网站在外部文件中还不被支持。 – Cynic

回答

0

确实,它不能在外部制作,并且不支持内联,但仍然可以通过JavaScript文件将其注入到DOM中,从而实现您想要的效果。

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

是的谷歌搜索机器人确实了解它。可能需要几天时间才能注册并使用网站管理员工具来强制重新抓取似乎不会强制刷新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();