GithubHelp home page GithubHelp logo

isomorphic-rails's Introduction

React server rendering example

This example demonstrates how React's server rendering works. Rather than demonstrate a pure-Node solution, this example shows how an app not written in JavaScript (in this case, PHP) can utilize React's server rendering capabilities.

Overview

You generally start a React app by doing something like this:

React.render(<MyComponent someData={...} />, document.getElementById('someContainer'));

The problem is that someContainer will be an empty HTML element until the JavaScript downloads and executes. This is bad for page load performance (since the user can't see anything until the JS downloads and executes) and is bad for SEO (since the Googlebot can't see any content). React's server rendering solves this problem -- it lets you fill someContainer with static HTML on the server and "bring it to life" on the client without throwing out and re-creating the HTML.

In order to do this, you need to do a few things. You need to be able to run JavaScript on the server, and you need to be able to bundle that JavaScript code and send it down to the browser. This example provides one architecture, but there are many ways to do it.

Architecture overview

Let's augment an existing PHP app to support server rendering. This architecture runs an Express-based Node web service to run the JavaScript server side. PHP simply uses file_get_contents() to send an HTTP request to this service to get the static HTML string.

Browserify is used to run the same code that Node.js is running inside of the browser (aka "isomorphic" JavaScript).

+-------------+       +------------------+                                              +----------------------+
|             |       |                  |  ---- HTTP request (module, props JSON) ---> |                      |
| The browser | <---> | Existing PHP App |                                              | Node.js react server |
|             |       |                  |  <--- HTTP response (static HTML string) --- |                      |
+------+------+       +------------------+                                              +----------------------+
       ^                                                                                            ^
       |                                                                                            |
       |              +------------------+                                              +-----------+----------+
       |              |                  |                                              |                      |
       +--------------+    Browserify    | <--------------------------------------------+  App code (CommonJS) |
                      |                  |                                              |                      |
                      +------------------+                                              +----------------------+

How to run the demo

  • npm install from jsapp/ and reactserver/.
  • Run browserify by doing npm run build inside jsapp/. This will generate a webapp/static/bundle.js file.
  • Run the node server by doing npm start inside reactserver/.
  • Finally, open index.php in your browser (on a webserver running PHP, of course). View-source to see the rendered markup.
  • Kill the reactserver and reload index.php. You'll notice that the app still works! View-source and you'll see no rendered markup. React is smart enough to recover if server rendering doesn't work.

isomorphic-rails's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.