简体   繁体   English

使用webpack进行服务器端渲染

[英]server side rendering using webpack

I am using Reactjs, webpack along with page.js (routing) to build a webapp. 我正在使用Reactjs,webpack和page.js(路由)来构建一个webapp。 Webpack gives me a bundle.js which is loaded on the client side. Webpack给了我一个在客户端加载的bundle.js。

I found out the og Meta tags can't be crawled if I am loading them on the client so, I have to implement it on the server side (server side rendering). 我发现og Meta标签如果我在客户端上加载它们就无法抓取,所以我必须在服务器端实现它(服务器端渲染)。 Is there any way to implement it ? 有没有办法实现它?

(I didn't find proper documentation about this on net). (我没有在网上找到关于此的适当文件)。

A lot of people are rendering the "shell" of the html with another template language on the server (since only the server needs to render it). 很多人在服务器上使用另一种模板语言渲染html的“shell”(因为只有服务器需要渲染它)。 You shouldn't use React to render stuff outside the body using normal APIs (there are known issues with that). 你不应该使用React使用普通的API在体外渲染东西(已知的问题)。 However you can use renderToStaticMarkup , which I think is a nicer way as you are not introducing another template language on the server just to render the shell. 但是你可以使用renderToStaticMarkup ,我认为这是一种更好的方法,因为你不在服务器上引入另一种模板语言来渲染shell。

Of course this means that you can't update something like the meta description or document.title in a normal React way, you would have to just do it with plain old JavaScript. 当然,这意味着您无法以正常的React方式更新元描述或document.title内容,您只需使用普通的旧JavaScript即可。 Alternatively I recommend react-helmet (which can modify the meta and title when the server renders too). 或者我建议使用react-helmet (它可以在服务器渲染时修改元和标题)。

I made an example here which may be helpful - https://github.com/DominicTobias/universal-react/ 我在这里做了一个可能有用的例子 - https://github.com/DominicTobias/universal-react/

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

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