2

I am still new programmer but I have an idea about project. I think about architecture for now. So my question is "Which is the best way to use react on server side render in PHP backend". I have researched and I`ve found two decision about that.

====================================================

First is to use React.js SSR with a Node.js service.

Advantages:

  1. Easy to install and update

  2. Standart enviorment for running react.js

Disadvantages:

  1. Maintain separate server

====================================================

Second is to use React.js SSR with PHP`s V8Js.

Advantages:

  1. Write js in PHP.

Disadvantages:

  1. Complicate install and update process

  2. Maybe V8Js is not scalable

I also find "react-php-v8js" library but maybe it`s still experimental. I want to know:

  1. Is there any other ways to render React.js in PHP?

  2. Which way gives better performance?

  3. Which way is better in production?

  4. What do you think about SSR React.js with PHP?

My backend will be in PHP. And I will use custom framework if it does matter. :)

Davosss
  • 61
  • 1
  • 7
  • "Write js in PHP" ...why is that an advantage? JS is JS. You can just create a .js file by itself. Why do you need PHP to write it? – ADyson Jul 04 '19 at 10:54
  • And anyway if you plan to use React, why not just write the whole backend in NodeJS? I don't see why you would want to complicate things by involving PHP. Why do you think you want to use PHP? – ADyson Jul 04 '19 at 10:56
  • This is actually several questions - some of which are completely subjective - so this is kinda *too broad* for SO's simple QA format. – CD001 Jul 04 '19 at 11:00
  • ADyson thanks for answer. I want to use PHP because I don`t know Node.js very well and I think PHP is faster than Node.js – Davosss Jul 04 '19 at 11:15
  • "I think PHP is faster than Node.js" ...you mean to execute the code? If so where's your evidence? Or you mean faster for you to write it because you understand it? That might be true...but if you try something new in Node you'll soon get the hang of it. But either way actually I think Josep's idea below is better anyway. – ADyson Jul 04 '19 at 11:32

2 Answers2

4

You are making it too complicated.

Just make a backend with PHP and an API REST.

Then you can create a frontend with pure React that just comunicates with the backend through fetchs to the API.

Josep Vidal
  • 2,580
  • 2
  • 16
  • 27
  • Josep thanks for answer. Maybe it`s wrong but I think when my app become biger and bigger it will be very hard for the server to return it. And Server site render is more secure. – Davosss Jul 04 '19 at 11:19
  • @Davosss rendering isn't something which needs security, you can render on the frontend quite easily - look at the thousands of single-page-applications out there. What needs security is data (e.g. your information) and actions (e.g. "show this data, delete this data) - in this architecture that's what the API does. It just returns data, not HTML or CSS etc. If the user is allowed to see the data, then the frontend can render it. – ADyson Jul 04 '19 at 11:31
  • 1
    @Davosss "when my app become biger and bigger it will be very hard for the server to return it" ...actually if the server is just returning data (e.g. JSON format) then that will place a lot less load on it than if you made the server create all of the HTML as well. – ADyson Jul 04 '19 at 11:31
  • 2
    This doesn't answer the question. There are perfectly valid reason to render serverside such as SEO and avoiding an initial loading screen – Guerrilla Aug 01 '21 at 00:57
  • You right @Guerrilla when I wrote this I didn't have the experience I have now. – Josep Vidal Sep 20 '21 at 12:54
  • @Guerrilla or SEO. – Robo Robok Jan 08 '23 at 14:18
  • This answer is not false. it's 100% correct. But the missing part is that the question says it has to be a server-side rendered react application. Not just a client-side rendered application. People in Nodejs are using NextJs for that for example – Normal Apr 12 '23 at 13:04
0

You can add react js as script tags in existing website https://reactjs.org/docs/add-react-to-a-website.html

Vyas Reddy
  • 1,226
  • 1
  • 13
  • 24