Created
May 16, 2015 00:39
-
-
Save Tivoli/1fa1020617198549309c to your computer and use it in GitHub Desktop.
Isomorphic React
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
'use strict'; | |
import React from 'react'; | |
import Router from 'react-router'; | |
import { canUseDOM } from 'react/lib/ExecutionEnvironment'; | |
import routes from './routes'; | |
let app = { | |
start(bootstrap) { | |
return canUseDOM ? this.webStart() : this.serverStart(bootstrap); | |
}, | |
webStart() { | |
Router.run(routes, Router.HistoryLocation, (Handler) => { | |
React.render(<Handler/>, document.body); | |
}); | |
}, | |
serverStart(bootstrap) { | |
return (fn) => { | |
Router.run(routes, bootstrap.path, (Handler) => { | |
let | |
component = React.createFactory(Handler), | |
html = React.renderToString(component(bootstrap)); | |
fn(null, html); | |
}); | |
}; | |
} | |
}; | |
export default app; |
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> | |
<title><%- title %></title> | |
<link rel="stylesheet" href="<%= stylesheet %>"> | |
</head> | |
<body> | |
<%= rootHTML %> | |
<script src="<%= javascript %>"></script> | |
<script><%= appStart %></script> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', '<%- analytics %>', 'auto'); | |
ga('send', 'pageview'); | |
</script> | |
</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
.get('/:path*', function *() { | |
let | |
theme = this.query.theme || config.layout.theme, | |
appFile = path.join(config.layout.publicDir, 'themes', theme, `${theme}.js`); | |
let App = require(appFile); | |
let bootstrap = { | |
path: this.path | |
}; | |
let layoutData = _.defaults({ | |
stylesheet: `/themes/${theme}/${theme}.css`, | |
javascript: `/themes/${theme}/${theme}.js`, | |
rootHTML: yield App.start(bootstrap), | |
appStart: `App.start(${htmlescape(bootstrap)});` | |
}, config.layout.pageConstants); | |
this.body = app.layout(layoutData); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment