-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (68 loc) · 2.06 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
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
import express from "express";
import React from "react";
import { renderToString } from "react-dom/server";
import { StaticRouter } from "react-router-dom/server";
import { ServerStyleSheet } from "styled-components";
import path from "path";
import fs from "fs";
import "isomorphic-fetch";
import App from "./src/App";
import { articles } from "./data/data";
import InitialDataContext from "./src/context/InitialDataContext";
global.window = {}; // to remove 'window does not exist' error
const app = express();
app.use(express.static("./build", { index: false }));
/* Data Loading */
app.get("/api/articles", (req, res) => {
const loadedArticles = articles;
res.json(loadedArticles);
});
/* Rendering app from server */
app.get("/*", async (req, res) => {
const sheet = new ServerStyleSheet();
const contextObj = {
_isServerSide: true,
_requests: [],
_data: {},
};
// Render #1: get data requests, collect styles
renderToString(
sheet.collectStyles(
<InitialDataContext.Provider value={contextObj}>
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</InitialDataContext.Provider>
)
);
// After 1st render is finished
await Promise.all(contextObj._requests);
contextObj._isServerSide = false;
delete contextObj._requests;
// Render #2
const reactApp = renderToString(
<InitialDataContext.Provider value={contextObj}>
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</InitialDataContext.Provider>
);
const templateFile = path.resolve("./build/index.html");
fs.readFile(templateFile, "utf8", (err, data) => {
if (err) {
return res.status(500).send(err);
}
const loadedData = JSON.stringify(contextObj);
res.send(
data
.replace(
'<div id="root"></div>',
`<script>window.preloadedData = ${loadedData}</script><div id="root">${reactApp}</div>`
)
.replace("{{ styles }}", sheet.getStyleTags())
);
});
});
app.listen(8080, () => {
console.log("Server is listening on port 8080.");
});