2016-11-07 85 views
1

我正在使用反應Js,並且我想在我的網站中爲SEO目的插入腳本標記。請參考下面的代碼和成就的可能性:使用反應插入腳本標記,腳本不包含javascript代碼

custom Script

<script type='application/ld+json'> 
    { 
     "@context": "http://www.sample.org", 
     "@type": "sampleBusiness", 
     "name": "test", 
     "url": "https://www.example.in", 
     "logo": "https://www.example.in/img/logo1.png", 
     "image": "https://www.example.in/img/logo1.png", 
     "description": "This is for test purpose only and shall not be used elsewhere!", 
     "address": { 
      "@type": "sampleAddress", 
      "streetAddress": "John Doe, #27, common street, developer house", 
      "addressLocality": "Test/Development", 
      "addressRegion": "TestRegion", 
      "postalCode": "testData", 
      "addressCountry": "DeveloperContry" 
     }, 
     "email": "hop(at)example.in", 
     "telephone": "someexample" 

    } 
</script> 

我想是這樣的補充,但它不會工作:

render : function() { 
    return (
     <script> 
      {{ 
     "@context": "http://www.sample.org", 
     "@type": "sampleBusiness", 
     "name": "test", 
     "url": "https://www.example.in", 
     "logo": "https://www.example.in/img/logo1.png", 
     "image": "https://www.example.in/img/logo1.png", 
     "description": "This is for test purpose only and shall not be used elsewhere!", 
     "address": { 
      "@type": "sampleAddress", 
      "streetAddress": "John Doe, #27, common street, developer house", 
      "addressLocality": "Test/Development", 
      "addressRegion": "TestRegion", 
      "postalCode": "testData", 
      "addressCountry": "DeveloperContry" 
     }, 
     "email": "hop(at)example.in", 
     "telephone": "someexample" 

    }} 
     </script> 
    ) 
} 
+0

難道你不能只是把它放在你的HTML文件?包含應用程序的根目錄元素的文件。 –

回答

0

這是不常見在React中包含腳本標記 - 在反應渲染時,腳本不會執行,因爲它會動態放置到dom中。

你可以試試反應頭盔庫這可能是做你需要的東西:https://github.com/nfl/react-helmet

有可能通過包括頭盔和使用片段,包括它:

script={[ 
    {"type": "application/ld+json", "innerHTML": `{ "@context": http://schema.org" }`} 
]} 
+0

我對StackOv頗爲陌生。並且無法理解我的答案出了什麼問題?你甚至不能嘗試提出的解決方案 - 或者它有什麼問題? – Jurosh

+0

這對我有效 –

0

如果您的應用呈現在客戶端,將這個腳本添加到客戶端上的dom,對於SEO來說無能爲力。因爲搜索引擎抓取工具不會看到它。因此,不要在應用程序中呈現它,而是將其添加到應用程序外的HTML頁面。

我猜你有一個.html文件,就像你的應用程序的root dom元素一樣。像這樣包含你的腳本。

<!doctype html> 
<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Your app</title> 
    </head> 
    <body> 
    <script> 
     // Content of your script here 
    </script> 
    <div id="root"></div> 
    <script src="/static/bundle.js"></script> <> 
    </body> 
</html> 
相關問題