-
-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commits: [a3fac01] Experimental: NodeJS server rendering support [679f88b] Experimental: NodeJS server rendering support [3e8268d] Experimental: NodeJS server rendering support
- Loading branch information
Showing
16 changed files
with
404 additions
and
157 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Node Server Rendering | ||
|
||
### Warning: this is an experimental feature | ||
|
||
The default server rendering exploits ExecJS to render react components. | ||
Node server rendering allows you to use separate NodeJS process as a renderer. The process loads server-bundle.js and | ||
then executes javascript to render the component inside its environment. The communication between rails and node occurs | ||
via socket (`client/node/node.sock`) | ||
|
||
### Getting started | ||
|
||
To use node process just set `server_render_method = "NodeJS"` in `config/initializers/react_on_rails.rb`. To change back | ||
to ExecJS set `server_render_method = "ExecJS"` | ||
|
||
### Configuration | ||
|
||
To change the name of server bundle adjust npm start script in `client/node/package.json` |
This file contains 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 characters
5 changes: 4 additions & 1 deletion
5
lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
web: rails s | ||
client: sh -c 'rm app/assets/webpack/* || true && cd client && npm run build:dev:client' | ||
<%- if options.server_rendering? %>server: sh -c 'cd client && npm run build:dev:server'<%- end %> | ||
<%- if options.server_rendering? %> | ||
server: sh -c 'cd client && npm run build:dev:server' | ||
node: sh -c 'cd client/node && npm start' | ||
<%- end %> |
This file contains 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 characters
8 changes: 8 additions & 0 deletions
8
lib/generators/react_on_rails/templates/base/base/lib/tasks/load_test.rake
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace :load_test do | ||
desc "Load test with apache benchmark" | ||
task :run, [:url, :count] do |_, args| | ||
url = args[:url] || "http://localhost:3000/hello_world" | ||
count = args[:count] || 500 | ||
system("ab -c 10 -n #{count} #{url}") | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
lib/generators/react_on_rails/templates/base/server_rendering/client/node/package.json
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "react_on_rails_node", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./server.js -s server-bundle.js" | ||
}, | ||
"dependencies": { | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
lib/generators/react_on_rails/templates/base/server_rendering/client/node/server.js
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
var net = require('net'); | ||
var fs = require('fs'); | ||
|
||
var bundlePath = '../../app/assets/webpack/'; | ||
var bundleFileName = 'server-bundle.js'; | ||
|
||
var currentArg; | ||
|
||
function Handler() { | ||
this.queue = []; | ||
this.initialized = false; | ||
} | ||
|
||
Handler.prototype.handle = function (connection) { | ||
var callback = function () { | ||
connection.setEncoding('utf8'); | ||
connection.on('data', (data)=> { | ||
console.log('Processing request: ' + data); | ||
var result = eval(data); | ||
connection.write(result); | ||
}); | ||
}; | ||
|
||
if (this.initialized) { | ||
callback(); | ||
} else { | ||
this.queue.push(callback); | ||
} | ||
}; | ||
|
||
Handler.prototype.initialize = function () { | ||
console.log('Processing ' + this.queue.length + ' pending requests'); | ||
var callback; | ||
while (callback = this.queue.pop()) { | ||
callback(); | ||
} | ||
|
||
this.initialized = true; | ||
}; | ||
|
||
var handler = new Handler(); | ||
|
||
process.argv.forEach((val) => { | ||
if (val[0] == '-') { | ||
currentArg = val.slice(1); | ||
return; | ||
} | ||
|
||
if (currentArg == 's') { | ||
bundleFileName = val; | ||
} | ||
}); | ||
|
||
try { | ||
fs.mkdirSync(bundlePath); | ||
} catch (e) { | ||
if (e.code != 'EEXIST') throw e; | ||
} | ||
|
||
fs.watchFile(bundlePath + bundleFileName, (curr) => { | ||
if (curr && curr.blocks && curr.blocks > 0) { | ||
if (handler.initialized) { | ||
console.log('Reloading server bundle must be implemented by restarting the node process!'); | ||
return; | ||
} | ||
|
||
require(bundlePath + bundleFileName); | ||
console.log('Loaded server bundle: ' + bundlePath + bundleFileName); | ||
handler.initialize(); | ||
} | ||
}); | ||
|
||
var unixServer = net.createServer(function (connection) { | ||
handler.handle(connection); | ||
}); | ||
|
||
unixServer.listen('node.sock'); | ||
|
||
process.on('SIGINT', () => { | ||
unixServer.close(); | ||
process.exit(); | ||
}); |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Oops, something went wrong.