Forked from koistya/ReactJS-Server-Side-Rendering.md
Last active
August 29, 2015 14:17
-
-
Save a-tarasyuk/0c4e5c242f08d4e5cd19 to your computer and use it in GitHub Desktop.
Revisions
-
koistya revised this gist
Jan 14, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -96,7 +96,7 @@ server.get('*', function(req, res) { }); server.listen(server.get('port'), function() { console.log('HTTP server is running at http://localhost:' + server.get('port')); }); ``` -
koistya revised this gist
Jan 14, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -70,9 +70,9 @@ Dispatcher.register((payload) => { ```javascript // src/server.js var _ = require('lodash'); var express = require('express'); var React = require('react'); // The top-level React component + HTML template for it var Application = React.createFactory(require('./components/Application')); -
koistya revised this gist
Jan 13, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -75,8 +75,8 @@ var express = require('express'); var _ = require('lodash'); // The top-level React component + HTML template for it var Application = React.createFactory(require('./components/Application')); var template = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8'); var server = express(); -
koistya revised this gist
Jan 13, 2015 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,18 +51,18 @@ var React = require('react'); var Dispatcher = require('./core/Dispatcher'); var Application = require('./components/Application'); var app = React.createElement(Application, { path: window.location.pathname, onSetTitle: (title) => document.title = title })); app = React.render(app, document.body); Dispatcher.register((payload) => { if (payload.action.actionType === ActionTypes.CHANGE_LOCATION) { app.setProps({path: payload.action.path}); } }); ``` ### Server-side Startup Script (Node.js/Express) -
koistya revised this gist
Jan 10, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -86,7 +86,7 @@ server.set('port', (process.env.PORT || 5000)); server.get('*', function(req, res) { var data = {}; var component = Application({ path: req.path, onSetTitle: (title) => data.title = title, onPageNotFound: () => res.status(404) }); -
koistya revised this gist
Jan 10, 2015 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,9 +58,8 @@ Dispatcher.register((payload) => { } }); var app = React.createElement(Application, { path: window.location.pathname, onSetTitle: (title) => document.title = title })); app = React.render(app, document.body); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,7 +54,6 @@ var Application = require('./components/Application'); Dispatcher.register((payload) => { var action = payload.action; if (action.actionType === ActionTypes.CHANGE_LOCATION) { app.setProps({path: action.path}); } }); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ The basic structure of a React+Flux application (see [other examples](https://gi - /src/server.js - Server-side startup script ``` ### The Top-level React Component ```javascript // src/components/Application.js -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -124,6 +124,7 @@ server.listen(server.get('port'), function() { to improve your experience. </p> <![endif]--> <%= body %> <script src="app.js"></script> </body> </html> -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -104,7 +104,7 @@ server.listen(server.get('port'), function() { ### HTML Template for Server-side Rendering ```html // src/index.html <!doctype html> @@ -118,7 +118,11 @@ server.listen(server.get('port'), function() { </head> <body> <!--[if lt IE 8]> <p class="browsehappy"> You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience. </p> <![endif]--> <script src="app.js"></script> </body> -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -102,6 +102,29 @@ server.listen(server.get('port'), function() { }); ``` ### HTML Template for Server-side Rendering ``` // src/index.html <!doctype html> <html class="no-js" lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title><%- title %></title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="css/app.css"> </head> <body> <!--[if lt IE 8]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <script src="app.js"></script> </body> </html> ``` ### Example ##### https://github.com/kriasoft/react-starter-kit -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ### Files The basic structure of a React+Flux application (see [other examples](https://github.com/facebook/flux/tree/master/examples)) @@ -70,6 +70,8 @@ app = React.render(app, document.body); ### Server-side Startup Script (Node.js/Express) ```javascript // src/server.js var React = require('react'); var express = require('express'); var _ = require('lodash'); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,8 +7,8 @@ The basic structure of a React+Flux application (see [other examples](https://gi - /src/components/Application.js - The top-level React component - /src/constants/ActionTypes.js - Action types (Flux) - /src/core/Dispatcher.js - Dispatcher (Flux) - /src/stores/AppStore.js - The main store (Flux) - /src/app.js - Client-side startup script - /src/server.js - Server-side startup script ``` -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,8 +8,7 @@ The basic structure of a React+Flux application (see [other examples](https://gi - /src/constants/ActionTypes.js - Action types (Flux) - /src/core/Dispatcher.js - Dispatcher (Flux) - /src/stores/AppStore.js - The primary store (Flux) - /src/app.js - Client-side startup script - /src/server.js - Server-side startup script ``` -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,6 +50,7 @@ module.exports = Application; var React = require('react'); var Dispatcher = require('./core/Dispatcher'); var Application = require('./components/Application'); Dispatcher.register((payload) => { var action = payload.action; @@ -60,7 +61,7 @@ Dispatcher.register((payload) => { }); var path = window.location.pathname + window.location.search + window.location.hash; var app = React.createElement(Application, { path: path, onSetTitle: (title) => document.title = title })); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,7 +54,7 @@ var Dispatcher = require('./core/Dispatcher'); Dispatcher.register((payload) => { var action = payload.action; if (action.actionType === ActionTypes.CHANGE_LOCATION) { history.pushState({}, document.title, action.path); app.setProps({path: action.path}); } }); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -67,7 +67,7 @@ var app = React.createElement(require('./components/Application'), { app = React.render(app, document.body); ``` ### Server-side Startup Script (Node.js/Express) ```javascript var React = require('react'); -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,16 +55,16 @@ Dispatcher.register((payload) => { var action = payload.action; if (action.actionType === ActionTypes.CHANGE_LOCATION) { window.history.pushState({}, document.title, action.path); app.setProps({path: action.path}); } }); var path = window.location.pathname + window.location.search + window.location.hash; var app = React.createElement(require('./components/Application'), { path: path, onSetTitle: (title) => document.title = title })); app = React.render(app, document.body); ``` ### Server-side Startup Script -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -94,6 +94,10 @@ server.get('*', function(req, res) { var html = _.template(template, data); res.send(html); }); server.listen(server.get('port'), function() { console.log('Node app is running at http://localhost:' + server.get('port')); }); ``` ### Example -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -74,7 +74,6 @@ var React = require('react'); var express = require('express'); var _ = require('lodash'); // The top-level React component + HTML template for it var Application = React.createFactory(require('./components/layout/Application')); var template = fs.readFileSync(path.join(__dirname, 'index.html'), {encoding: 'utf8'}); -
koistya renamed this gist
Jan 7, 2015 . 1 changed file with 32 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ### Project Files The basic structure of a React+Flux application (see [other examples](https://github.com/facebook/flux/tree/master/examples)) @@ -13,7 +13,7 @@ The basic structure of a React+Flux application (see [other examples](https://gi - /src/server.js - Server-side startup script ``` ### The Top-level Component ```javascript // src/components/Application.js @@ -67,6 +67,36 @@ var application = React.createElement(require('./components/Application'), { application = React.render(application, document.body); ``` ### Server-side Startup Script ```javascript var React = require('react'); var express = require('express'); var _ = require('lodash'); // The top-level React component + HTML template for it var Application = React.createFactory(require('./components/layout/Application')); var template = fs.readFileSync(path.join(__dirname, 'index.html'), {encoding: 'utf8'}); var server = express(); server.set('port', (process.env.PORT || 5000)); // Server-side rendering (SSR) server.get('*', function(req, res) { var data = {}; var component = Application({ path: req.url, onSetTitle: (title) => data.title = title, onPageNotFound: () => res.status(404) }); data.body = React.renderToString(component); var html = _.template(template, data); res.send(html); }); ``` ### Example ##### https://github.com/kriasoft/react-starter-kit -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,13 +43,13 @@ var Application = React.createClass({ module.exports = Application; ``` ### Client-side Startup Script (aka Bootstrap) ```javascript // src/app.js var React = require('react'); var Dispatcher = require('./core/Dispatcher'); Dispatcher.register((payload) => { var action = payload.action; -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,7 +50,6 @@ module.exports = Application; var React = require('react'); var Application = React.createFactory(require('./components/Application)); Dispatcher.register((payload) => { var action = payload.action; @@ -60,12 +59,12 @@ Dispatcher.register((payload) => { } }); var path = window.location.pathname + window.location.search + window.location.hash; var application = React.createElement(require('./components/Application'), { path: path, onSetTitle: (title) => document.title = title })); application = React.render(application, document.body); ``` ### Example -
koistya revised this gist
Jan 7, 2015 . 1 changed file with 34 additions and 112 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,151 +1,73 @@ ### Files The basic structure of a React+Flux application (see [other examples](https://github.com/facebook/flux/tree/master/examples)) ``` - /src/actions/AppActions.js - Action creators (Flux) - /src/components/Application.js - The top-level React component - /src/constants/ActionTypes.js - Action types (Flux) - /src/core/Dispatcher.js - Dispatcher (Flux) - /src/stores/AppStore.js - The primary store (Flux) - /src/stores/Store.js - Base class for stores (Flux) - /src/app.js - Client-side startup script - /src/server.js - Server-side startup script ``` ### Top-level Component ```javascript // src/components/Application.js var React = require('react'); var Application = React.createClass({ propTypes: { path: React.PropTypes.string.isRequired, onSetTitle: React.PropTypes.func.isRequired }, render() { var page = AppStore.getPage(this.props.path); this.props.onSetTitle(page.title); return ( <div className="container"> <h1>{page.title}</h1> <div>{page.content}</div> </div> ); } }); module.exports = Application; ``` ### Client-side startup script (aka bootstrap) ```javascript // src/app.js var React = require('react'); var Application = React.createFactory(require('./components/Application)); var application; Dispatcher.register((payload) => { var action = payload.action; if (action.actionType === ActionTypes.CHANGE_LOCATION) { window.history.pushState({}, document.title, action.path); application.setProps({path: action.path}); } }); function setTitle(value) { document.title = value; } var path = window.location.pathname + window.location.search + window.location.hash; var application = React.render(new Application({path: path, onSetTitle: setTitle}), document.body); ``` ### Example ##### https://github.com/kriasoft/react-starter-kit -
koistya revised this gist
Nov 17, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -97,7 +97,7 @@ module.exports = UserProfile; ```javascript // src/actions/PageActions.js var Dispatcher = require('../core/Dispatcher'); var ActionTypes = require('../constants/ActionTypes'); var PageActions = { @@ -107,7 +107,7 @@ var PageActions = { * @param {{title: string, description: string}} page */ set(page) { Dispatcher.handleViewAction({ actionType: ActionTypes.SET_CURRENT_PAGE, page: page }); @@ -123,11 +123,11 @@ module.exports = PageActions; ```javascript // src/app.js var Dispatcher = require('./core/Dispatcher'); var ActionTypes = require('./constants/ActionTypes'); var ExecutionEnvironment = require('react/lib/ExecutionEnvironment'); Dispatcher.register((payload) => { switch (payload.action.actionType) { -
koistya revised this gist
Nov 14, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,8 @@ The basic stucture of a React+Flux application (see [other examples](https://git ``` - /src/actions/PageActions.js - Action creator (Flux) - /src/constants/ActionTypes.js - Action types (Flux) - /src/core/Dispatcher.js - Dispatcher (Flux) - /src/core/Store.js - Base class for stores (Flux) - /src/layouts/DefaultLayout.js - Page layout reused accross multiple top-level components - /src/pages/DemoPage.js - Top-level react component (page) - /src/stores/PageStore.js - Store (Flux) -
koistya revised this gist
Nov 14, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ ### Files The basic stucture of a React+Flux application (see [other examples](https://github.com/facebook/flux/tree/master/examples)) ``` - /src/actions/PageActions.js - Action creator (Flux) -
koistya renamed this gist
Nov 14, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
koistya renamed this gist
Nov 13, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
koistya renamed this gist
Nov 13, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.
NewerOlder