2016-11-07 61 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>