Download the PHP package yilinker/reactjs-serverside-renderer without Composer
On this page you can find all versions of the php package yilinker/reactjs-serverside-renderer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yilinker/reactjs-serverside-renderer
More information about yilinker/reactjs-serverside-renderer
Files in yilinker/reactjs-serverside-renderer
Package reactjs-serverside-renderer
Short Description This Bundle provides Symfony2 with reactjs server side rendering capability
License MIT
Informations about the package reactjs-serverside-renderer
ReactJS server-side renderer
What does this bundle do:
- Allow reactjs usage with symfony twig
- Allow server side reactjs (polymorphic) rendering (OPTIONAL)
Requirements:
- Node.js >= v0.12
- NPM
- Grunt
- Webpack
This bundle allows symfony to utilize server side rendering (for SEO) for reactjs. For this to work, a nodejs express server is used to return a string representing the static HTML of the initial reactjs page. This is achieved by sending an HTTP request to the nodejs express server.
Once the page is loaded into the browser, the client side reactjs kicks in and the javascript magic begins using the available webpack bundled reactjs files.
This bundle also a Twig extension to allow react to be used directly with your twig templates. You need not enable the server side renderer to make use of this feature.
React Tags
There are currently two flavors of custom twig tags for ReactJS, one for with props and another for without.
With Props
Syntax:
Ex:
Note that the entire statement is wrapped around two curly braces, the props with a pair of curly braces. A pipe ('|') is also present between the props and the react function containing the component name as the parameter.
Without Props
Syntax:
Ex:
Syntax is the same as the one before but without the props and the pipe character.
ReactJS in Development Environments
Most of the ReactJS Components that we will be creating would use the JSX sub-language to make the development process painless and fast. Unfortunately, no browser in the world understands what JSX is so we have to transpile (translate + compile) it first to regular Javascript before we can see the component in action. This is where webpack comes in.
Installation
A. Add the react bundle in your AppKernel.php file
B. Configure the bundle by adding the following into app/config/config.yml
of your Symfony2 application.
- renderer.render_server: this is the HTTP URI where the nodejs server resides
- twig_extension.src_path: this is the location of the reactjs source files
C. Navigate to the Nodejs
directory and run npm install
to install the bundle specific dependencies
D. Add a package.json file to the root of your application and add the following dependencies:
Again run, npm install
but this time at the root of the application to install the application node dependencies
E. Create a Gruntfile.js
at the root of the application and add the following webpack block:
Read more about creating a Gruntfile here: http://gruntjs.com/sample-gruntfile
F. Move react templating files from the bundle to the symfony web directory:
- 'Resource/public/js/reactRender.js' -> '[symfony2/assets/directory]/js/reactRender.js'
- 'Resource/public/js/mapTemplate.js' -> '[symfony2/assets/directory]/reactjs/maps/mapTemplate.js'
Usage
Using React with TWIG
All you have to do to reference reactjs in your pages is to:
- Code your reactjs components and put them in your reactjs source directory, for example
web/assets/reactjs/src
- Create a map file in and add it to your reactjs maps directory, for example:
web/assets/reactjs/map
. There is usually one map file per page. This contains the reactjs components used in a particular page.
See the example map file and react component files in https://github.com/nelsoft-easyshop/react-serverside-renderer/tree/master/Resources/public/example
- Build using grunt-webpack by running
grunt
Note that everytime a react component is changed, you will have to rerun grunt. You can skip this by simply runninggrunt --watch
instead - Add your react twig tags into your view file
- Reference the bundled map file in your twig templates as well as the
reactRender.js
file. The reactRender file simply attaches your react client side logic to the server-side-generated react html.
Note that if the nodejs server is not running, then you won't have server side rendering enabled but your app will still work. In other words, your app won't be indexable but you can use it as it is. This is useful when your working on your dev environment.
Server Side Renderer Usage
Navigate to the assets directory of your application and then from here execute server.js
in the bundle
For example:
Note that you don't have to start the SSR server to make the react integration work. You only need it if you want server side rendering, which is usually just in the production environment.
Pro-tip:
You can make the execution step faster by creating an npm run-script for server.js:
A. At the package.json in the root of the application, add the following script commands:
B. At the package.json in yilinker/reactjs-serverside-renderer/Nodejs/
, add a start script that performs the appropriate directory change command and the node initiation command. Note, you will most likely have to modify the commands a bit depending on how your folders are structured.
C. You can then simply run npm run-script react-start
at the root of your symfony application