2016-02-12 18 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();