2019-01-24 23:49:21 +00:00
|
|
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
|
2019-01-13 03:27:28 +00:00
|
|
|
|
2019-07-10 00:21:25 +00:00
|
|
|
import MainApp from '../client/main/App.js';
|
2019-01-13 03:27:28 +00:00
|
|
|
|
2019-07-10 00:21:25 +00:00
|
|
|
import MainTemplate from './utils/MainTemplate.js';
|
|
|
|
import getEntryPoint from './utils/getEntryPoint.js';
|
|
|
|
import getGlobalScripts from './utils/getGlobalScripts.js';
|
2019-07-10 05:47:10 +00:00
|
|
|
import {
|
|
|
|
createElement,
|
|
|
|
createHTML,
|
|
|
|
createScript
|
|
|
|
} from './utils/markupHelpers.js';
|
2019-01-24 23:49:21 +00:00
|
|
|
|
|
|
|
const doctype = '<!DOCTYPE html>';
|
2019-01-24 05:27:58 +00:00
|
|
|
const globalURLs =
|
2019-01-24 05:43:52 +00:00
|
|
|
process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging'
|
2019-01-24 05:27:58 +00:00
|
|
|
? {
|
|
|
|
'@emotion/core': '/@emotion/core@10.0.6/dist/core.umd.min.js',
|
|
|
|
react: '/react@16.7.0/umd/react.production.min.js',
|
|
|
|
'react-dom': '/react-dom@16.7.0/umd/react-dom.production.min.js'
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
'@emotion/core': '/@emotion/core@10.0.6/dist/core.umd.min.js',
|
|
|
|
react: '/react@16.7.0/umd/react.development.js',
|
|
|
|
'react-dom': '/react-dom@16.7.0/umd/react-dom.development.js'
|
|
|
|
};
|
|
|
|
|
2019-01-13 03:27:28 +00:00
|
|
|
export default function serveMainPage(req, res) {
|
2019-01-24 23:49:21 +00:00
|
|
|
const content = createHTML(renderToString(createElement(MainApp)));
|
|
|
|
const entryPoint = getEntryPoint('main', 'iife');
|
|
|
|
const elements = getGlobalScripts(entryPoint, globalURLs).concat(
|
|
|
|
createScript(entryPoint.code)
|
|
|
|
);
|
|
|
|
|
|
|
|
const html =
|
|
|
|
doctype +
|
|
|
|
renderToStaticMarkup(createElement(MainTemplate, { content, elements }));
|
2019-01-13 03:27:28 +00:00
|
|
|
|
|
|
|
res
|
|
|
|
.set({
|
2019-01-24 23:49:21 +00:00
|
|
|
'Cache-Control': 'public, max-age=14400', // 4 hours
|
2019-01-13 03:27:28 +00:00
|
|
|
'Cache-Tag': 'main'
|
|
|
|
})
|
|
|
|
.send(html);
|
|
|
|
}
|