DoSomething / voting-app

☑ Voting app for DoSomething.org campaigns.
MIT License
6 stars 4 forks source link

Server-side rendering of React views #347

Closed DFurnes closed 9 years ago

DFurnes commented 9 years ago

This is more of a nice-to-have than a necessity. Right now, we're rendering server-side views using Blade, and then wiping the DOM and re-rendering a nice client-side interface in React on page load. Ideally, we can use React's renderComponentToString functionality to pre-render the interface on the server. Then all React has to do is attach event listeners on page load.

DFurnes commented 9 years ago

Two potential ways of handling this –

  1. PHP's v8 extension, which allows PHP code to execute JavaScript through V8. Facebook has an (experimental) library for doing this, and there's an (also experimental) Laravel package for this. The downside is that installation of the v8 extension is a pretty monstrous undertaking, and all of this stuff is very "beta" and potentially buggy. The upside is that it's super elegant once it's running.
  2. Run a separate NodeJS process just for rendering React components, and offload rendering to that as a service. This is a little bit less elegant, and requires us to keep two processes running on the server. However, it should be way easier to set up & we're much less likely to run into bugs and strange issues.
mshmsh5000 commented 9 years ago

I'm in favor of the dedicated Node process for now. There's nothing inherently inelegant about having multiple, purpose-dedicated processes on the back end. And I'd much rather we reuse existing elements in our tech stack (NodeJS) than add in experimental components (PHP v8 support).

In the future, I'm sure something will standardize and stabilize for Laravel-React support, and we can reconsider.

DFurnes commented 9 years ago

Agreed! Morgan & I were looking at the PHP v8 extension the other day and it looked pretty crazy. I just pushed up a PR with a proof-of-concept of Option 2 for review/discussion.

DFurnes commented 9 years ago

Merged in an initial stab at this in #348, and deployed to www.catsgonegood.com. Seems to be working pretty nicely when I tried SSH'ing in and manually running the Node renderer process.

DFurnes commented 9 years ago

Just leaving a note here –– manually started a forever process on Cats Gone Good to run the boostrap/react_server.js pre-rendering service for Specialists Guild testing. We'll need to update the Capistrano script to start or restart that process when we deploy in the future.

DFurnes commented 9 years ago

Closing this, since only remaining task is to update deploy so we don't have to manually restart the Node process.