Last active
February 8, 2017 08:16
-
-
Save nukosuke/0770d835a6e9d16b6891d09ef97765c0 to your computer and use it in GitHub Desktop.
react_on_railsとreact-routerでシングルページアプリケーションをサーバサイドレンダリングする ref: http://qiita.com/nukosuke/items/2769b9505617c4b6ee07
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<%= server_render_js("Helmet.rewind().title.toString()") %> | |
<%= csrf_meta_tags %> | |
<%= stylesheet_link_tag 'application', media: 'all' %> | |
</head> | |
<body> | |
<%= yield %> | |
<%= javascript_include_tag 'application' %> | |
</body> | |
</html> |
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
//= require vendor-bundle | |
//= require app-bundle | |
//= require jquery | |
//= require jquery_ujs |
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
import React from 'react'; | |
import { Router as ReactRouter, browserHistory } from 'react-router'; | |
import ReactOnRails from 'react-on-rails'; | |
import routes from '../routes/routes'; | |
const Router = (props, railsContext) => { | |
const history = browserHistory; | |
return ( | |
<ReactRouter history={history}> | |
{routes} | |
</ReactRouter> | |
); | |
}; | |
// This is how react_on_rails can see the Router in the browser. | |
ReactOnRails.register({ | |
Router, | |
}); |
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
//= require react | |
//= require react_ujs | |
//= require components |
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
gem 'react_on_rails', '~> 6' |
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
# in app root dir | |
foreman start -f Procfile.dev |
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
bundle exec rails g react_on_rails:install |
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
class ApplicationController < ActionController::Base | |
protect_from_forgery with: :exception | |
def home | |
render_for_react() | |
end | |
private | |
# ref: http://r7kamura.hatenablog.com/entry/2016/10/10/173610 | |
def common_props | |
{ | |
currentUser: current_user, | |
} | |
end | |
def render_for_react(props: {}, status: 200) | |
if request.format.json? | |
response.headers["Cache-Control"] = "no-cache, no-store" | |
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT" | |
response.headers["Pragma"] = "no-cache" | |
render( | |
json: common_props.merge(props), | |
status: status, | |
) | |
else | |
render( | |
html: view_context.react_component( | |
"Router", | |
prerender: true, | |
props: common_props.merge(props).as_json, | |
), | |
layout: true, | |
status: status, | |
) | |
end | |
end | |
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
# in client/ | |
yarn add react-router react-helmet |
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
# from | |
config.webpack_generated_files = %w( webpack-bundle.js ) | |
# to | |
config.webpack_generated_files = %w( app-bundle.js server-bundle.js vendor-bundle.js ) | |
# from | |
config.server_bundle_js_file = "webpack-bundle.js" | |
# to | |
config.server_bundle_js_file = "server-bundle.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
import React from 'react'; | |
import { match, RouterContext } from 'react-router'; | |
import ReactOnRails from 'react-on-rails'; | |
import Helmet from 'react-helmet'; | |
import routes from '../routes/routes'; | |
// for header title server side rendeting on first load | |
// ref: http://r7kamura.hatenablog.com/entry/2016/10/10/173610 | |
global.Helmet = Helmet; | |
const Router = (props, railsContext) => { | |
let error; | |
let redirectLocation; | |
let routeProps; | |
const { location } = railsContext; | |
match({ routes, location }, (_error, _redirectLocation, _routeProps) => { | |
error = _error; | |
redirectLocation = _redirectLocation; | |
routeProps = _routeProps; | |
}); | |
if (error || redirectLocation) { | |
return { error, redirectLocation }; | |
} | |
return ( | |
<RouterContext {...routeProps} /> | |
); | |
}; | |
ReactOnRails.register({ | |
Router, | |
}); |
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
/** | |
* from | |
*/ | |
entry: [ | |
'es5-shim/es5-shim', | |
'es5-shim/es5-sham', | |
'babel-polyfill', | |
'./app/bundles/Application/startup/registration', | |
], | |
output: { | |
filename: 'webpack-bundle.js', | |
path: '../app/assets/webpack', | |
}, | |
/** | |
* to | |
*/ | |
entry: { | |
vendor: [ | |
'es5-shim/es5-shim', | |
'es5-shim/es5-sham', | |
'babel-polyfill', | |
], | |
app: [ | |
'./app/bundles/Application/startup/clientRegistration', | |
], | |
server: [ | |
'./app/bundles/Application/startup/serverRegistration', | |
] | |
}, | |
output: { | |
filename: '[name]-bundle.js', | |
path: '../app/assets/webpack', | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment