This repository has been archived by the owner on Mar 5, 2020. It is now read-only.
/
page-generator.jsx
111 lines (92 loc) · 3.66 KB
/
page-generator.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var React = require('react');
var ReactDOM = require('react-dom');
var ReactDOMServer = require('react-dom/server');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var RoutingContext = ReactRouter.RoutingContext;
var match = ReactRouter.match;
var routeData = require('./routes.jsx');
var Page = require('../components/page.jsx');
var locales = require('../dist/locales.json');
var ReactIntl = require('react-intl');
var IntlProvider = ReactIntl.IntlProvider;
var addLocaleData = ReactIntl.addLocaleData;
var currentLocale;
var ga = require('react-ga');
var assign = require('object-assign');
// utility function: create element wrapped in React localisation
function createElement(Component, props) {
var locale = this.locale;
var messages = assign({}, locales["en-US"], locales[locale]);
// make sure you pass all the props in!
return (
<IntlProvider locale={locale} messages={messages}>
<Component {...props} />
</IntlProvider>
);
}
/**
* Module return is an object with some of the internval values and
* functions for generating the static site files.
*/
module.exports = {
URLS: routeData.URLS,
REDIRECTS: routeData.REDIRECTS,
routes: routeData.routes,
/**
* Static function for resolving redirects for site content
*/
generateStaticRedirect: function generateStaticRedirect(fromURL, toURL, next) {
match({ routes: routeData.routes, location: fromURL }, function(error, redirectLocation, renderProps) {
if (error) {
return next(new Error("Error in redirect from '" + fromURL + "' to '" + toURL + "'"));
}
var redirectHTML = (<p>The URL of this page has changed to <a href={toURL}>{toURL}</a>.</p>);
var html = ReactDOMServer.renderToStaticMarkup(redirectHTML);
next(null, html, { title: "Redirect to " + toURL });
});
},
/**
* Static function for generating the site as static html files
*/
generateStatic: function generateStatic(url, locale, next) {
if (url in routeData.REDIRECTS) {
return this.generateStaticRedirect(url, redirects[url], next);
}
match({ routes: routeData.routes, location: url }, function(error, redirectLocation, renderProps) {
if (error) {
return next(new Error("Error on route '" + url + "'"));
}
if (!renderProps) {
return next(new Error("No render properties for '"+url+"'"));
}
var Component = renderProps.routes.slice(-1)[0].component;
var title = Page.titleForHandler(Component);
var html = ReactDOMServer.renderToString(<RoutingContext locale={locale} createElement={createElement} {...renderProps} />);
next(null, html, { title: title });
});
},
/**
* Static wrapper function for GA events
*/
run: function run(location, el) {
var createBrowserHistory = require('history/lib/createBrowserHistory');
var history = createBrowserHistory();
// emit a GA page view event on location changes
history.listen(function(location) {
ga.pageview(location.pathname);
});
// Get locale from URL, use it to pass messages in to IntlProvider,
// but not before adding appropriate locale data
// (see ./lib/build/server-library.jsx for how that gets in here)
var currentLocale = window.location.pathname.split('/')[1];
var messages = assign({}, locales["en-US"], locales[currentLocale]);
// Keys are languages, not locales, so we just need the first part
addLocaleData(window.ReactIntlLocaleData[currentLocale.split('-')[0]]);
ReactDOM.render(
<IntlProvider locale={currentLocale} messages={messages}>
<Router history={history}>{routeData.routes}</Router>
</IntlProvider>, el
);
}
};