unpkg/modules/server/MainController.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-05-16 22:55:24 +00:00
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
2016-05-20 18:58:58 +00:00
import { getZones, getZoneAnalyticsDashboard } from './CloudFlare'
2016-05-16 22:55:24 +00:00
import HomePage from './components/HomePage'
2016-05-20 18:58:58 +00:00
const OneMinute = 1000 * 60
const ThirtyDays = OneMinute * 60 * 24 * 30
2016-05-16 22:55:24 +00:00
const DOCTYPE = '<!DOCTYPE html>'
2016-05-20 18:58:58 +00:00
const fetchStats = (callback) => {
if (process.env.NODE_ENV === 'development') {
2016-06-13 06:19:38 +00:00
callback(null, require('./CloudFlareStats.json'))
2016-05-20 18:58:58 +00:00
} else {
getZones('npmcdn.com')
.then(zones => {
const zone = zones[0]
const since = new Date(Date.now() - ThirtyDays)
const until = new Date(Date.now() - OneMinute)
return getZoneAnalyticsDashboard(zone, since, until).then(result => {
callback(null, result)
})
})
.catch(callback)
}
}
export const sendHomePage = (req, res, next) => {
2016-07-20 19:26:15 +00:00
const chunks = [ 'vendor', 'home' ]
2016-05-16 22:55:24 +00:00
const props = {
2016-07-20 19:26:15 +00:00
styles: req.bundle.getStyleAssets(chunks),
scripts: req.bundle.getScriptAssets(chunks)
2016-05-16 22:55:24 +00:00
}
2016-07-20 19:26:15 +00:00
if (req.manifest)
props.webpackManifest = req.manifest
2016-05-20 18:58:58 +00:00
fetchStats((error, stats) => {
if (error) {
next(error)
} else {
2016-08-30 00:33:23 +00:00
res.set('Cache-Control', 'public, max-age=60')
2016-05-20 18:58:58 +00:00
res.send(
DOCTYPE + renderToStaticMarkup(<HomePage {...props} stats={stats}/>)
)
}
})
2016-05-16 22:55:24 +00:00
}