This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
/
server-render.jsx
341 lines (311 loc) · 10.7 KB
/
server-render.jsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// @flow
import newrelic from 'newrelic';
import type { Reducer } from 'redux';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import Helmet from 'react-helmet';
import MobileDetect from 'mobile-detect';
import isBot from 'isbot';
import { API_ROUTE_PATH } from 'mwp-api-proxy-plugin';
import { Forbidden, NotFound, Redirect, SERVER_RENDER } from 'mwp-router';
import { getFindMatches, resolveAllRoutes } from 'mwp-router/lib/util';
import { getServerCreateStore } from 'mwp-store/lib/server';
import Dom from 'mwp-app-render/lib/components/Dom';
import ServerApp from 'mwp-app-render/lib/components/ServerApp';
import { parseMemberCookie } from 'mwp-core/lib/util/cookieUtils';
import {
getVariants,
parseBrowserIdCookie,
parseSiftSessionCookie,
} from '../util/cookieUtils';
import { getLaunchDarklyUser } from '../util/launchDarkly';
const DOCTYPE = '<!DOCTYPE html>';
const DUMMY_DOMAIN = 'http://mwp-dummy-domain.com';
/**
* An async module that renders the full app markup for a particular URL/location
* using [ReactDOMServer]{@link https://facebook.github.io/react/docs/top-level-api.html#reactdomserver}
*
* @module ServerRender
*/
function getHtml(el) {
const htmlMarkup = ReactDOMServer.renderToString(el);
return `${DOCTYPE}${htmlMarkup}`;
}
export function getRedirect(context: { url?: string, permanent?: boolean }) {
if (!context || !context.url) {
return;
}
// use `URL` to ensure valid character encoding (e.g. escaped emoji)
const url: string = context.url;
const isFragment = url.startsWith('/');
const urlToFormat = isFragment ? `${DUMMY_DOMAIN}${url}` : url;
const formattedUrl = new URL(urlToFormat).toString();
return {
redirect: {
url: formattedUrl.replace(DUMMY_DOMAIN, ''),
permanent: context.permanent,
},
};
}
/*
* A helper to collect react-side-effect data from all components in the
* application that utilize side effects
*
* Any component class that uses react-side-effect should be 'rewound' in this
* function to prevent memory leaks and invalid state carrying over into other
* requests
*/
const resolveSideEffects = () => ({
head: Helmet.rewind(),
redirect: Redirect.rewind(),
forbidden: Forbidden.rewind(),
notFound: NotFound.rewind(),
});
/**
* Get media from X-UA-Device header set by Fastly which parses the user agent string
*/
const getMedia = (userAgent: string, userAgentDevice: string) => {
const isAtSmallUp = true;
let isMobile = true;
let isTablet = false;
if (userAgentDevice) {
isMobile =
userAgentDevice === 'smartphone' ||
userAgentDevice === 'mobilebot' ||
userAgentDevice === 'mobile';
isTablet = userAgentDevice === 'tablet';
} else {
const mobileDetect = new MobileDetect(userAgent);
isMobile = Boolean(mobileDetect.phone());
isTablet = Boolean(mobileDetect.tablet());
}
return {
isAtSmallUp,
isAtMediumUp: isTablet || !isMobile,
isAtLargeUp: !isMobile && !isTablet,
};
};
/**
* Using the current route information and Redux store, render the app to an
* HTML string and server response code, with optional cookies to write
*/
const getRouterRenderer = ({
request,
h,
appContext,
routes,
store,
scripts,
cssLinks,
}): RenderResult => {
// pre-render the app-specific markup, this is the string of markup that will
// be managed by React on the client.
//
// **IMPORTANT**: this string is built separately from `<Dom />` because it
// initializes page-specific state that `<Dom />` needs to render, e.g.
// `<head>` contents
const initialState = store.getState();
let appMarkup;
const routerContext: { url?: string, permanent?: boolean } = {};
try {
appMarkup = ReactDOMServer.renderToString(
<ServerApp
request={request}
h={h}
appContext={appContext}
routerContext={routerContext}
store={store}
routes={routes}
/>
);
} catch (err) {
// cleanup all react-side-effect components to prevent error/memory leaks
resolveSideEffects();
// now we can re-throw and let the caller handle the error
throw err;
}
const sideEffects = resolveSideEffects();
const externalRedirect = getRedirect(sideEffects.redirect);
const internalRedirect = getRedirect(routerContext);
const redirect = internalRedirect || externalRedirect;
if (redirect) {
return redirect;
}
// cssLinks can be an Array or a Function that returns an array
if (typeof cssLinks === 'function') {
// invoke function and provide initialState
cssLinks = cssLinks(initialState);
}
// all the data for the full `<html>` element has been initialized by the app
// so go ahead and assemble the full response body
const result = getHtml(
<Dom
head={sideEffects.head}
initialState={initialState}
appContext={appContext}
appMarkup={appMarkup}
scripts={scripts}
cssLinks={cssLinks}
/>
);
// prioritized status code fallbacks
const statusCode = sideEffects.forbidden || sideEffects.notFound || 200;
return {
statusCode,
result,
};
};
// get initial server-rendered app metadata that can be consumed by the application
// from mwp-app-render/src/components/AppContext.Consumer
const getAppContext = (request: HapiRequest, enableServiceWorker: boolean) => {
const { url, headers, info, server, state } = request;
// request protocol and host might be different from original request that hit proxy
// we want to use the proxy's protocol and host
const requestProtocol = headers['x-forwarded-proto'] || server.info.protocol;
const domain: string =
headers['x-forwarded-host'] || headers['x-meetup-host'] || info.host;
const clientIp =
request.query.__set_geoip ||
headers['fastly-client-ip'] ||
info.remoteAddress;
const host = `${requestProtocol}://${domain}`;
const userAgent = headers['user-agent'];
const userAgentDevice = headers['x-ua-device'] || ''; // set by fastly
const requestLanguage = request.getLanguage();
return {
apiUrl: API_ROUTE_PATH,
baseUrl: host,
basename: requestLanguage === 'en-US' ? '' : `/${requestLanguage}`, // basename is the 'base path' for the application - usually a localeCode
enableServiceWorker,
requestLanguage,
supportedLangs: server.settings.app.supportedLangs,
initialNow: new Date().getTime(),
isProdApi: server.settings.app.api.isProd,
isQL: parseMemberCookie(state).ql === 'true',
memberId: parseMemberCookie(state).id, // deprecated, use member.id
// the member cookie is not structured the same way as the member object returned from /member/self
// be careful relying on it to have the same properties downstream
member: parseMemberCookie(state),
variants: getVariants(state),
entryPath: url.pathname, // the path that the user entered the app on
media: getMedia(userAgent, userAgentDevice),
browserId: parseBrowserIdCookie(state),
clientIp,
siftSessionId: parseSiftSessionCookie(state),
isBot: isBot(request.headers['user-agent']),
};
};
/**
* Curry a function that takes a Hapi request and returns a Promise
* that will emit the rendered HTML
*
* The outer function takes app-specific information about the routes,
* reducer, and optional additional middleware
*/
const makeRenderer = (renderConfig: {
routes: Array<Object>,
reducer: Reducer<MWPState, FluxStandardAction>,
middleware: Array<Function>,
scripts: Array<string>,
enableServiceWorker: boolean,
cssLinks: ?(Array<string> | (MWPState => Array<string>)),
}) => {
const {
routes,
reducer,
middleware,
scripts,
cssLinks,
enableServiceWorker,
} = renderConfig;
// set up a Promise that emits the resolved routes - this single Promise will
// be reused for all subsequent requests, so we're not resolving the routes repeatedly
// hooray performance
const routesPromise = resolveAllRoutes(routes);
return (request: HapiRequest, h: HapiResponseToolkit): Promise<RenderResult> => {
if (!scripts.length) {
throw new Error('No client script assets specified');
}
const appContext = getAppContext(request, enableServiceWorker);
// create the store with populated `config`
const initializeStore = resolvedRoutes => {
const createStore = getServerCreateStore(
getFindMatches(resolvedRoutes, appContext.basename),
middleware || [],
request
);
const initialState = { config: appContext };
return Promise.resolve(createStore(reducer, initialState));
};
// otherwise render using the API and React router
// addFlags is called twice in order to ensure that
// there is a full member object available in state
// feature flags can be selected based on member id,
// email, and other properties.
// feature flags based on member id are available before the store is populated.
const addFlags = (populatedStore, member) => {
// Populate a LaunchDarklyUser object from member and request details
const launchDarklyUser = getLaunchDarklyUser(member, request);
return request.server.plugins['mwp-app-route']
.getFlags(launchDarklyUser)
.then(flags =>
populatedStore.dispatch({
type: 'UPDATE_FLAGS',
payload: flags,
})
);
};
const checkReady = state =>
state.preRenderChecklist.every(isReady => isReady);
const populateStore = store =>
new Promise((resolve, reject) => {
// dispatch SERVER_RENDER to kick off API middleware
const { pathname, search, hash } = request.url;
const location = { pathname, search, hash };
store.dispatch({ type: SERVER_RENDER, payload: location });
if (checkReady(store.getState())) {
// we need to use the _latest_ version of the member object
// which is why memberObj is defined after the checkReady call.
const memberObj = (store.getState().api.self || {}).value || {};
addFlags(store, memberObj).then(() => {
resolve(store);
});
return;
}
const unsubscribe = store.subscribe(() => {
if (checkReady(store.getState())) {
// we need to use the _latest_ version of the member object
// which is why memberObj is defined after the checkReady call.
const memberObj =
(store.getState().api.self || {}).value || {};
addFlags(store, memberObj).then(() => {
resolve(store);
unsubscribe();
});
}
});
});
return routesPromise.then(resolvedRoutes =>
initializeStore(resolvedRoutes).then(store => {
// the initial addFlags call will only be key'd by member ID
return addFlags(store, { id: parseMemberCookie(request.state).id })
.then(() => populateStore(store))
.then(
store =>
// create tracer and immediately invoke the resulting function.
// trace should start before rendering, finish after rendering
newrelic.createTracer('serverRender', getRouterRenderer)({
request,
h,
appContext,
routes: resolvedRoutes,
store,
scripts,
cssLinks,
}) // immediately invoke callback
);
})
);
};
};
export default makeRenderer;