-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathserver.tsx
191 lines (176 loc) · 4.8 KB
/
server.tsx
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import express from "express"
import ReactDOMServer from "react-dom/server"
import React from "react"
import chalk from "chalk"
import webpack from "webpack"
import webpackDevMiddleware from "webpack-dev-middleware"
import webpackConfig from "./webpack.config"
import { findDevice } from "@artsy/detect-responsive-traits"
import {
mediaStyle,
findBreakpointsForWidths,
findBreakpointAtWidth,
MediaContextProvider,
SortedBreakpoints,
SSRStyleID,
} from "./src/Media"
import { App } from "./src/App"
const compiler = webpack(webpackConfig)
const app = express()
app.use(
webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
serverSideRender: true,
stats: "errors-only",
})
)
/**
* Find the breakpoints and interactions that the server should render
*/
function onlyMatchListForUserAgent(userAgent: string): OnlyMatchList {
const device = findDevice(userAgent)
if (!device) {
log(userAgent)
return undefined
} else {
const supportsHover = device.touch ? "notHover" : "hover"
const onlyMatch: any = device.resizable
? [
supportsHover,
...(findBreakpointsForWidths(device.minWidth, device.maxWidth) as []),
]
: [
supportsHover,
findBreakpointAtWidth(device.minWidth),
findBreakpointAtWidth(device.maxWidth),
]
log(userAgent, onlyMatch, device.description)
return onlyMatch
}
}
/**
* Demonstrate server-side _only_ rendering
*/
app.get("/ssr-only", (req, res) => {
res.send(
template({
includeCSS: true,
body: `
<div id="react-root">
${ReactDOMServer.renderToString(
<MediaContextProvider
onlyMatch={onlyMatchListForUserAgent(req.header(
"User-Agent"
) as string)}
>
<App />
</MediaContextProvider>
)}
</div>
`,
})
)
})
/**
* Demonstrate server-side rendering _with_ client-side JS rehydration
*/
app.get("/rehydration", (req, res) => {
res.send(
template({
includeCSS: true,
body: `
<div id="loading-indicator">Loading…</div>
<div id="react-root">
${ReactDOMServer.renderToString(
<MediaContextProvider
onlyMatch={onlyMatchListForUserAgent(req.header(
"User-Agent"
) as string)}
>
<App />
</MediaContextProvider>
)}
</div>
<script>
// This is to show that the layout already looks good while still
// loading the JS bundle.
setTimeout(function () {
var script = document.createElement("script")
script.src = "/assets/app.js"
document.getElementsByTagName("head")[0].appendChild(script);
document.getElementById("loading-indicator").remove();
}, 1000)
</script>
`,
})
)
})
/**
* Demonstrate client-side JS _only_ rendering
*/
app.get("/client-only", (_req, res) => {
res.send(
template({
includeCSS: false,
body: `
<div id="react-root">Loading…</div>
<script>
setTimeout(function () {
var script = document.createElement("script")
script.src = "/assets/app.js"
document.getElementsByTagName("head")[0].appendChild(script);
}, 1000)
</script>
`,
})
)
})
/**
* Misc things that are not of interest for demonstrating fresnel
*/
// TODO: Simplify this hideous typing.
type OnlyMatchList =
| Array<"hover" | "notHover" | (typeof SortedBreakpoints)[0]>
| undefined
app.get("/", (_req, res) => {
res.send(
template({
includeCSS: false,
body: `
<ul>
<li><a href="/ssr-only">server-side rendering <em>only</em></a></li>
<li><a href="/client-only">client-side rendering <em>only</em></a></li>
<li><a href="/rehydration">server-side rendering <em>and</em> client-side rehydration</a></li>
</ul>
`,
})
)
})
const template = ({ includeCSS, body }) => `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
${
includeCSS
? `<style type="text/css" id="${SSRStyleID}">${mediaStyle}</style>`
: ""
}
</head>
<body>
${body}
</body>
</html>
`
function log(userAgent: string, onlyMatch?: string[], device?: string) {
// tslint:disable-next-line:no-console
console.log(
`Render: ${chalk.green(onlyMatch ? onlyMatch.join(", ") : "ALL")}\n` +
`Device: ${device ? chalk.green(device) : chalk.red("n/a")}\n` +
`User-Agent: ${chalk.yellow(userAgent)}\n`
)
}
app.listen(3000, () => {
console.warn("\nApp started at http://localhost:3000 \n")
})