Created
November 30, 2017 23:21
-
-
Save clucasalcantara/246bff02d50accbb57c8d56e515aedbc to your computer and use it in GitHub Desktop.
Preciso setar maAge: '5d' ou 432000 pras imagens e 120 pra css js
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
/* eslint-disable no-console */ | |
import 'babel-polyfill' | |
import path from 'path' | |
import express from 'express' | |
import compression from 'compression' | |
import cache from 'cache-control' | |
import React from 'react' | |
import serialize from 'serialize-javascript' | |
import { renderToStaticMarkup } from 'react-dom/server' | |
import { Provider } from 'react-redux' | |
import { StaticRouter } from 'react-router' | |
import { renderToString } from 'react-router-server' | |
import cookiesMiddleware from 'universal-cookie-express' | |
import { api, configureStore } from '@components' | |
import { port, host, basename } from './config' | |
import App from './App' | |
import Html from './Html' | |
import Error from './Error' | |
import getProductFromLocation from './HandleLocation' | |
const renderApp = (req, { store, context, location }, region) => { | |
const serverLocation = getProductFromLocation(location) | |
const app = ( | |
<Provider store={store}> | |
<StaticRouter basename={basename} context={context} location={location}> | |
<App serverLocation={serverLocation} region={region} /> | |
</StaticRouter> | |
</Provider> | |
) | |
return renderToString(app) | |
} | |
const renderHtml = ({ serverState, initialState, content }) => { | |
const assets = global.assets | |
const state = ` | |
window.__SERVER_STATE__ = ${serialize(serverState)}; | |
window.__INITIAL_STATE__ = ${serialize(initialState)}; | |
` | |
const html = <Html {...{ assets, state, content }} /> | |
return `<!doctype html>\n${renderToStaticMarkup(html)}` | |
} | |
const app = express() | |
app.use(basename, express.static(path.resolve(process.cwd(), 'dist/public'), { maxAge: '5d' })) | |
app.use(compression()) | |
app.use(cache({ '/**': 120 })) | |
app.use((req, res, next) => { | |
if (req.url.match(/^\/(js)\/.+/)) { | |
res.setHeader('Cache-Control', 'public, max-age=120') | |
} | |
next() | |
}) | |
app.use(cookiesMiddleware()) | |
app.use((req, res, next) => { | |
const location = req.url | |
const store = configureStore({}, { api: api.create() }) | |
const context = {} | |
let region = { | |
cidade: '4203', | |
'cidade-nome': 'Rio de Janeiro', | |
ddd: '21', | |
estado: 'RJ', | |
} | |
if (req.universalCookies.get('cidade')) { | |
region = { | |
cidade: req.universalCookies.get('cidade'), | |
'cidade-nome': req.universalCookies.get('cidade-nome'), | |
ddd: req.universalCookies.get('ddd'), | |
estado: req.universalCookies.get('cidade'), | |
} | |
} | |
renderApp(req, { store, context, location }, region) | |
.then(({ state: serverState, html: content }) => { | |
if (context.status) { | |
res.status(context.status) | |
} | |
if (context.url) { | |
res.redirect(context.url) | |
} else { | |
const initialState = store.getState() | |
res.send(renderHtml({ | |
serverState, | |
initialState, | |
content, | |
})) | |
} | |
}) | |
.catch(next) | |
}) | |
app.use((err, req, res, next) => { | |
const content = (<Error />) | |
res.status(500).send(renderHtml({ content })) | |
console.error(err) | |
next(err) | |
}) | |
app.listen(port, (error) => { | |
const boldBlue = text => `\u001b[1m\u001b[34m${text}\u001b[39m\u001b[22m` | |
if (error) { | |
console.error(error) | |
} else { | |
console.info(`Server is running at ${boldBlue(`http://${host}:${port}${basename}/`)}`) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment