Reputation: 4748
My website is written in PHP. Node.js is also set up for socket.io. I have been thinking about server-side rendering with Reactjs so I can reuse the views. Planning to use v8js extension, but then I came across this article talking about two possible solutions:
1.Use v8js extension and React-PHP-V8Js
2.Send data from php to node.js to render views, example:
node.js
require("babel/register");
var React = require('react');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use('/', function(req, res) {
try {
var view = path.resolve('./views/' + req.query.module);
var component = require(view);
var props = req.body || null;
res.status(200).send(
React.renderToString(
React.createElement(component, props)
)
);
} catch (err) {
res.status(500).send(err.message);
}
});
php
use GuzzleHttp\Client;
$app->get('/{name:.*?}', function($name) use ($app) {
$client = new Client(['base_url' => 'http://localhost:3000']);
$response = $client->post('/', [
'json' => ['name' => ucfirst($name ?: 'World')],
'query' => ['module' => 'hello'],
]);
$contents = $response->getBody()->getContents();
return response($contents, 200);
});
The author says he would prefer the second method because the first one:
Unless you're comfortable installing (and updating) libv8 and the V8Js PECL extension on your production machines, this is not an option. Personally, I wouldn't go this route. Installing the dependencies is cumbersome, dependency management is tricky, and there aren't many resources to guide you along the way. In addition, you'll need to account for the fact that your javascript builds should not be bundled with react if you want to re-use them.
Could you tell me what he means by "javascript builds not being able to bundle with react"? I have been thinking about using v8js
+ React-PHP-V8Js
so I would like to understand some of the drawbacks of it. Also, would you prefer the second method (node.js as template rendering service)? I have node.js running already so this seems to be a good suggestion.
Upvotes: 5
Views: 1017
Reputation: 64
The second option is a better idea in my opinion in a long run compared to the first one in a more short term project do to having a higher amount of variables and more handling required to process the variables and handling the JavaScript. You will have a lot less maintenance and easier time solving errors you might run into if you use the second option. Also easier to integrate in my opinion if it is just PHP on PHP instead of JavaScript handling those responses.
Upvotes: 1
Reputation: 20641
I think the second option "running template engine as service" is a better option as it has less complexity and fewer dependencies.
I think server side rendering is an interesting idea, and It has many great benefits.
The performance of the server is more predictable than the many different device configurations of your users. So there is a little thinking here what is the target audience for your app. ( if a lot of users with crappy mobile then extra point for server-side rendering )
The increase in perceived performance. Users are avoiding the blank page and loading spinner. ( Perceived performance - because the user still must wait for attaching event handlers )
You make your web available to crawlers which are usually less javascript-able.
I think correctly executed server side rendering could provide less complexity and less maintenance overhead. But that's more of benefits for bigger teams. For a single developer structuring and abstracting the code to work as one will take many times longer than separating views for the client and server.
So, in short, I wouldn't do it before putting tonnes of research into it and my advice is "keep things simple". For every joy, there is a price to be paid.
Some problems to overcome:
To render templates correctly server side, and client side would need to have access to the same application state. ( check Redux https://github.com/reactjs/redux )
Real web apps often need to interact with the server and load data from it. So how you are going handle this with the current setup.
You may have asynchronous actions in your views and Server-side rendering is synchronous.
Extra security implications.
Upvotes: 1
Reputation: 20520
This is an opinion question, so any answer you get will be an opinion.
I'll say no, it's not a good idea, and explain my reasoning.
Every software project has a complexity that can be roughly measured by the number of buzzwords used. That is, "a high-availability, multi-lingual, React website backed by AWS Lambda services" sounds inherently hairy. Each buzzword multiplies the efforts of the buzzwords before. You are suggesting building "a site using PHP using ReactJS for server-side rendering with an underlying node V8JS rendering..." and you run out of complexity before you hit the problem domain.
You will hit issues with React having a totally different rendering model. You will hit caching issues. You will hit sysadmin issues.
Don't do it.
Upvotes: 2