forked from HorNorin/spa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (38 loc) · 1.03 KB
/
server.js
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
import React from 'react';
import App from './app/app';
import express from 'express';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import appReducer from './app/reducer';
import { loadAnimes } from './app/action';
import { renderToString } from 'react-dom/server';
const app = express();
app.use(express.static(`${__dirname}/public`));
app.use('*', (req, res, next) => {
const animes = require('./animes.json');
const store = createStore(appReducer);
store.dispatch(loadAnimes(animes));
const body = renderToString(
<Provider store={store}>
<App />
</Provider>
);
const preloadState = store.getState();
const html = `
<!doctype html>
<html>
<head>
<title>SEO Friendly App</title>
</head>
<body>
<div id="root">${ body }</div>
<script>
window.__PRELOAD_STATE__ = ${JSON.stringify(preloadState)};
</script>
<script src="/assets/app.js"></script>
</body>
</html>
`
res.send(html);
});
app.listen(8080);