简体   繁体   English

你如何使用React.js进行搜索引擎优化?

[英]How do you use React.js for SEO?

Articles on React.js like to point out, that React.js is great for SEO purposes. 关于React.js的文章喜欢指出,React.js非常适合SEO目的。 Unfortunately, I've never read, how you actually do it. 不幸的是,我从未读过,你是怎么做到的。 Do you simply implement _escaped_fragment_ as in https://developers.google.com/webmasters/ajax-crawling/docs/getting-started and let React render the page on the server, when the url contains _escaped_fragment_ , or is there more to it? 你是否只是在https://developers.google.com/webmasters/ajax-crawling/docs/getting-started中实现了_escaped_fragment_ ,当网址包含_escaped_fragment_时,让React在服务器上呈现页面,或者还有更多内容?

Being able not to rely on _escaped_fragment_ would be great, as probably not all potentially crawling sites (eg in sharing functionalities) implement _escaped_fragment_ . 能够不依赖_escaped_fragment_会很棒,因为可能不是所有潜在的爬网站点(例如共享功能)都实现了_escaped_fragment_

I'm pretty sure anything you've seen promoting React as being good for SEO has to do with being able to render the requested page on the server, before sending it to the client. 我很确定你所看到的任何促使React对SEO有好处的事情都与能够在服务器上呈现所请求的页面有关,然后再将其发送到客户端。 So it will be indexed just like any other static page, as far as search engines are concerned. 因此,就搜索引擎而言,它将像任何其他静态页面一样被编入索引。

Server rendering made possible via ReactDOMServer.renderToString . 通过ReactDOMServer.renderToString实现服务器渲染。 The visitor will receive the already rendered page of markup, which the React application will detect once it has downloaded and run. 访问者将收到已经呈现的标记页面,React应用程序将在下载并运行后检测到该页面。 Instead of replacing the content when ReactDOM.render is called, it will just add the event bindings. 它不是在ReactDOM.render时替换内容,而只是添加事件绑定。 For the rest of the visit, the React application will take over and further pages will be rendered on the client. 对于访问的其余部分,React应用程序将接管并在客户端上呈现更多页面。

If you are interested in learning more about this, I suggest searching for "Universal JavaScript" or "Universal React" (formerly known as "isomorphic react"), as this is becoming the term for JavaScript applications that use a single code base to render on both the server and client. 如果您有兴趣了解更多相关信息,我建议您搜索“Universal JavaScript”或“Universal React”(以前称为“isomorphic react”),因为这将成为使用单个代码库渲染的JavaScript应用程序的术语在服务器和客户端上。

As the other responder said, what you are looking for is an Isomorphic approach. 正如另一位回应者所说,你所寻找的是一种同构方法。 This allows the page to come from the server with the rendered content that will be parsed by search engines. 这允许页面来自服务器,其中包含将由搜索引擎解析的呈现内容。 As another commenter mentioned, this might make it seem like you are stuck using node.js as your server-side language. 正如另一位评论者所提到的,这可能会使您看起来像使用node.js作为服务器端语言。 While it is true that have javascript run on the server is needed to make this work, you do not have to do everything in node. 虽然确实需要在服务器上运行javascript才能使其工作,但您不必在节点中执行所有操作。 For example, this article discusses how to achieve an isomorphic page using Scala and react: 例如,本文讨论如何使用Scala实现同构页面并做出反应:

Isomorphic Web Design with React and Scala 使用React和Scala进行同构Web设计

That article also outlines the UX and SEO benefits of this sort of isomorphic approach. 该文章还概述了这种同构方法的用户体验和搜索引擎优化的好处。

Two nice example implementations: 两个很好的示例实现:

Try visiting https://react-redux.herokuapp.com/ with javascript turned on and off, and watch the network in the browser dev tools to see the difference… 尝试访问https://react-redux.herokuapp.com/并打开和关闭javascript,并在浏览器开发工具中查看网络以查看差异...

It is also possible via ReactDOMServer.renderToStaticMarkup : 也可以通过ReactDOMServer.renderToStaticMarkup

Similar to renderToString , except this doesn't create extra DOM attributes such as data-react-id , that React uses internally. renderToString类似, renderToString不会创建额外的DOM属性,例如data-react-id内部使用的data-react-id This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save lots of bytes. 如果你想将React用作一个简单的静态页面生成器,这很有用,因为剥离额外的属性可以节省大量的字节。

Going to have to disagree with a lot of the answers here since I managed to get my client-side React App working with googlebot with absolutely no SSR. 由于我设法让我的客户端React App与googlebot完全没有SSR,所以不得不在这里不同意这里的答案。

Have a look at the SO answer here . 看看这里的答案 I only managed to get it working recently but I can confirm that there are no problems so far and googlebot can actually perform the API calls and index the returned content. 我最近设法让它工作,但我可以确认到目前为止没有问题,googlebot可以实际执行API调用并索引返回的内容。

There is nothing you need to do if you care about your site's rank on Google, because Google's crawler could handle JavaScript very well! 如果您关心自己网站在Google上的排名,则无需做任何事情,因为Google的抓取工具可以很好地处理JavaScript! You can check your site's SEO result by search site:your-site-url . 您可以通过搜索site:your-site-url检查您网站的搜索引擎优化结果site:your-site-url

If you also care about your site's rank such as Baidu , and your sever side implemented by PHP , maybe you need this: react-php-v8js . 如果你也关心你的网站的排名,如百度 ,你的服务器端由 PHP实现,也许你需要这个: react-php-v8js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM