简体   繁体   English

使用 PHP 进行 React.js 服务器端渲染

[英]React.js server side rendering with PHP

I would like to develop themes/plugins for WordPress based on React .我想基于ReactWordPress开发主题/插件。 To make it search engine friendly, I need it to be rendered initially on the server (serverside-rendering).为了使它对搜索引擎友好,我需要它最初在服务器上呈现(服务器端呈现)。

The only way to do this, as far as I know, is to use react-php-v8js , which requires the PECL V8js extension .据我所知,唯一的方法是使用react-php-v8js ,这需要PECL V8js 扩展 This is a problem since I have no control over the platform on which these themes/plugins will be run.这是一个问题,因为我无法控制运行这些主题/插件的平台。

Is there a way to make React and WordPress work together without having to install additional extensions?有没有办法让ReactWordPress一起工作而无需安装额外的扩展? Perhaps by building/compiling React files into PHP ?也许通过构建/编译React文件到PHP

There's an article that describes how to do this:有一篇文章描述了如何做到这一点:

https://sebastiandedeyne.com/server-side-rendering-javascript-from-php/ https://sebastiandedeyne.com/server-side-rendering-javascript-from-php/

But it's a fairly complex setup and it requires using composer.但这是一个相当复杂的设置,需要使用作曲家。 That can be difficult in Wordpress projects since Wordpress tends to completely eschew the modern php architecture.这在 Wordpress 项目中可能很困难,因为 Wordpress 倾向于完全避开现代 php 架构。

If you're looking for a library to help with SSR in PHP:如果您正在寻找一个库来帮助 PHP 中的 SSR:

https://github.com/spatie/server-side-rendering https://github.com/spatie/server-side-rendering

Best of luck on it.祝你好运。

If you want your content to be indexed by search engine without js, you can print your minimal content using Wordpress, just the bare minimum + crucial meta tags, maybe localize some initial state for your react app to boot.如果你希望你的内容在没有 js 的情况下被搜索引擎索引,你可以使用 Wordpress 打印你的最小内容,只是最基本的 + 关键元标签,也许本地化一些初始状态让你的反应应用程序启动。 A bare bone theme such http://underscores.me/ would be sufficient.http://underscores.me/这样的裸骨主题就足够了。 When js is available, you can replace your whole WordPress generated content with React ones.当 js 可用时,您可以用 React 替换整个 WordPress 生成的内容。

The ideal one is to have React generate the content for you.理想的情况是让 React 为您生成内容。 But it's hard until we can see that nodejs / PECL V8js extension available everywhere.但是直到我们看到 nodejs / PECL V8js 扩展随处可用之前,这很难。

If you can at least install nodejs and launch a node process then it should be ok, although not so simple.如果您至少可以安装 nodejs 并启动一个节点进程,那么它应该没问题,虽然不是那么简单。

You would need to generate a ssr version of your assets and use it in a node process that would listen on a socket to write the html result..您需要生成资产的 ssr 版本,并在节点进程中使用它,该进程将侦听套接字以写入 html 结果..

In your controller you can create a socket to your node process (something like stream_socket_client(...)) and then you can send a dummy function written as a javascript string to that socket (something like stream_socket_sendto($sock, "getResultForMyWidget(someParams){...}")).在您的控制器中,您可以为您的节点进程创建一个套接字(类似于 stream_socket_client(...) ){...}"))。 That function would be evaluated in the node process that would return the response to the controller (the html response as a ReactDOMServer.renderToString from the component you want to render).该函数将在节点进程中进行评估,该进程将返回响应给控制器(html 响应作为来自您要呈现的组件的 ReactDOMServer.renderToString)。

That's it for the big picture.这就是大局。

There is a symfony plugin that illustates it very clearly (see this github ) and comes with a dummy server node process to illustrate how it handles the socket listening and eval of the incoming function and returns the html result.有一个 symfony 插件非常清楚地说明了它(参见这个 github )并带有一个虚拟服务器节点进程来说明它如何处理传入函数的套接字侦听和 eval 并返回 html 结果。 See also the example in the sandbox for a bigger picture and in depth implementation.另请参阅沙箱中的示例以获得更大的图景和深入的实现。 You should be able to adapt it to wordpress.您应该能够使其适应 wordpress。

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

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