Skip to content

Instantly share code, notes, and snippets.

@reduxdj
Forked from Tivoli/app.js
Last active August 29, 2015 14:22
Show Gist options
  • Save reduxdj/e786f8ada3bc34a386f4 to your computer and use it in GitHub Desktop.
Save reduxdj/e786f8ada3bc34a386f4 to your computer and use it in GitHub Desktop.
'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;
<!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>
.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