@salesforce/pwa-kit-react-sdk 3.8.0-preview.0-basepath → 3.8.0-preview.2-basepath
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/package.json +6 -5
- package/ssr/browser/main.js +136 -0
- package/ssr/browser/main.test.js +54 -0
- package/ssr/server/react-rendering.js +448 -0
- package/ssr/server/react-rendering.test.js +786 -0
- package/ssr/universal/compatibility.js +31 -0
- package/ssr/universal/components/_app/index.js +35 -0
- package/ssr/universal/components/_app/index.test.js +20 -0
- package/ssr/universal/components/_app-config/index.js +104 -0
- package/ssr/universal/components/_app-config/index.test.js +22 -0
- package/ssr/universal/components/_document/index.js +92 -0
- package/ssr/universal/components/_document/index.test.js +58 -0
- package/ssr/universal/components/_error/index.js +55 -0
- package/ssr/universal/components/_error/index.test.js +28 -0
- package/ssr/universal/components/app-error-boundary/index.js +113 -0
- package/ssr/universal/components/app-error-boundary/index.test.js +109 -0
- package/ssr/universal/components/fetch-strategy/index.js +42 -0
- package/ssr/universal/components/refresh/index.js +123 -0
- package/ssr/universal/components/refresh/index.test.js +78 -0
- package/ssr/universal/components/route-component/index.js +415 -0
- package/ssr/universal/components/route-component/index.test.js +378 -0
- package/ssr/universal/components/switch/index.js +62 -0
- package/ssr/universal/components/throw-404/index.js +36 -0
- package/ssr/universal/components/throw-404/index.test.js +26 -0
- package/ssr/universal/components/with-correlation-id/index.js +36 -0
- package/ssr/universal/components/with-legacy-get-props/index.js +100 -0
- package/ssr/universal/components/with-legacy-get-props/index.test.js +35 -0
- package/ssr/universal/components/with-react-query/index.js +130 -0
- package/ssr/universal/components/with-react-query/index.test.js +101 -0
- package/ssr/universal/contexts/index.js +72 -0
- package/ssr/universal/contexts/index.test.js +101 -0
- package/ssr/universal/errors.js +34 -0
- package/ssr/universal/errors.test.js +20 -0
- package/ssr/universal/events.js +38 -0
- package/ssr/universal/events.test.js +39 -0
- package/ssr/universal/hooks/index.js +52 -0
- package/ssr/universal/routes.js +15 -0
- package/ssr/universal/utils.client.test.js +46 -0
- package/ssr/universal/utils.js +61 -0
- package/ssr/universal/utils.server.test.js +24 -0
- package/utils/assets.js +120 -0
- package/utils/assets.test.js +106 -0
- package/utils/logger-instance.js +19 -0
- package/utils/performance.js +126 -0
- package/utils/performance.test.js +50 -0
- package/utils/url.js +39 -0
- package/utils/url.test.js +47 -0
- package/utils/uuidv4.client.js +21 -0
- package/utils/uuidv4.client.test.js +27 -0
- package/utils/warnings.js +81 -0
- package/utils/warnings.test.js +48 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.render = exports.getLocationSearch = exports.default = exports.ALLOWLISTED_INLINE_SCRIPTS = void 0;
|
|
7
|
+
var _path = _interopRequireDefault(require("path"));
|
|
8
|
+
var _react = _interopRequireDefault(require("react"));
|
|
9
|
+
var _server = _interopRequireDefault(require("react-dom/server"));
|
|
10
|
+
var _reactHelmet = require("react-helmet");
|
|
11
|
+
var _server2 = require("@loadable/server");
|
|
12
|
+
var _reactRouterDom = require("react-router-dom");
|
|
13
|
+
var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
|
|
14
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
15
|
+
var _sprite = _interopRequireDefault(require("svg-sprite-loader/runtime/sprite.build"));
|
|
16
|
+
var _ssrServer = require("@salesforce/pwa-kit-runtime/utils/ssr-server");
|
|
17
|
+
var _ssrShared = require("@salesforce/pwa-kit-runtime/utils/ssr-shared");
|
|
18
|
+
var _ssrConfig = require("@salesforce/pwa-kit-runtime/utils/ssr-config");
|
|
19
|
+
var _utils = require("../universal/utils");
|
|
20
|
+
var _contexts = require("../universal/contexts");
|
|
21
|
+
var _document = _interopRequireDefault(require("../universal/components/_document"));
|
|
22
|
+
var _app = _interopRequireDefault(require("../universal/components/_app"));
|
|
23
|
+
var _throw2 = _interopRequireDefault(require("../universal/components/throw-404"));
|
|
24
|
+
var _compatibility = require("../universal/compatibility");
|
|
25
|
+
var _switch = _interopRequireDefault(require("../universal/components/switch"));
|
|
26
|
+
var _routeComponent = require("../universal/components/route-component");
|
|
27
|
+
var errors = _interopRequireWildcard(require("../universal/errors"));
|
|
28
|
+
var _loggerInstance = _interopRequireDefault(require("../../utils/logger-instance"));
|
|
29
|
+
var _performance = _interopRequireWildcard(require("../../utils/performance"));
|
|
30
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
31
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
32
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
33
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
34
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
35
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
36
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
37
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
38
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
39
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } /*
|
|
40
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
41
|
+
* All rights reserved.
|
|
42
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
43
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
44
|
+
*/ /**
|
|
45
|
+
* @module progressive-web-sdk/ssr/server/react-rendering
|
|
46
|
+
*/
|
|
47
|
+
const CWD = process.cwd();
|
|
48
|
+
const BUNDLES_PATH = _path.default.resolve(CWD, 'build/loadable-stats.json');
|
|
49
|
+
const VALID_TAG_NAMES = ['base', 'body', 'head', 'html', 'link', 'meta', 'noscript', 'script', 'style', 'title'];
|
|
50
|
+
const ALLOWLISTED_INLINE_SCRIPTS = exports.ALLOWLISTED_INLINE_SCRIPTS = [];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Convert from thrown Error or String to {message, status} that we need for
|
|
54
|
+
* rendering.
|
|
55
|
+
* @private
|
|
56
|
+
* @param err - Error to be converted
|
|
57
|
+
* @function
|
|
58
|
+
* @return {Object}
|
|
59
|
+
*/
|
|
60
|
+
const logAndFormatError = err => {
|
|
61
|
+
if (err instanceof errors.HTTPError) {
|
|
62
|
+
// These are safe to display – we expect end-users to throw them
|
|
63
|
+
return {
|
|
64
|
+
message: err.message,
|
|
65
|
+
status: err.status,
|
|
66
|
+
stack: err.stack
|
|
67
|
+
};
|
|
68
|
+
} else {
|
|
69
|
+
const cause = err.stack || err.toString();
|
|
70
|
+
_loggerInstance.default.error(cause, {
|
|
71
|
+
namespace: 'react-rendering.render'
|
|
72
|
+
});
|
|
73
|
+
const safeMessage = 'Internal Server Error';
|
|
74
|
+
return {
|
|
75
|
+
message: safeMessage,
|
|
76
|
+
status: 500,
|
|
77
|
+
stack: err.stack
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// Because multi-value params are not supported in `aws-serverless-express` create a proper
|
|
83
|
+
// search string using the `query` property. We pay special attention to the order the params
|
|
84
|
+
// as best as we can.
|
|
85
|
+
const getLocationSearch = (req, opts = {}) => {
|
|
86
|
+
const {
|
|
87
|
+
interpretPlusSignAsSpace = false
|
|
88
|
+
} = opts;
|
|
89
|
+
const [_, search] = req.originalUrl.split('?');
|
|
90
|
+
const params = new URLSearchParams(search);
|
|
91
|
+
const newParams = new URLSearchParams();
|
|
92
|
+
const orderedKeys = [...new Set(params.keys())];
|
|
93
|
+
|
|
94
|
+
// Maintain the original order of the parameters by iterating the
|
|
95
|
+
// ordered list of keys, and using the `req.query` object as the source of values.
|
|
96
|
+
orderedKeys.forEach(key => {
|
|
97
|
+
const value = req.query[key];
|
|
98
|
+
const values = Array.isArray(value) ? value : [value];
|
|
99
|
+
values.forEach(v => {
|
|
100
|
+
// To have feature parity to SFRA, the + sign can be treated as space
|
|
101
|
+
// However, this could potential a breaking change since not all users want to treat it as such
|
|
102
|
+
// Therefore, we create a flag for it via the app configuration
|
|
103
|
+
newParams.append(key, interpretPlusSignAsSpace ? decodeURIComponent(v).replace(/\+/, ' ') : v);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
const searchString = newParams.toString();
|
|
107
|
+
// Update the location objects reference.
|
|
108
|
+
return searchString ? `?${searchString}` : '';
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* This is the main react-rendering function for SSR. It is an Express handler.
|
|
113
|
+
*
|
|
114
|
+
* @param req - Request
|
|
115
|
+
* @param res - Response
|
|
116
|
+
*
|
|
117
|
+
* @function
|
|
118
|
+
*
|
|
119
|
+
* @return {Promise}
|
|
120
|
+
*/
|
|
121
|
+
exports.getLocationSearch = getLocationSearch;
|
|
122
|
+
const render = exports.render = /*#__PURE__*/function () {
|
|
123
|
+
var _ref = _asyncToGenerator(function* (req, res, next) {
|
|
124
|
+
var _config$app, _config$app$url, _AppConfig$getBasePat, _AppConfig$getBasePat2;
|
|
125
|
+
const includeServerTimingHeader = ('__server_timing' in req.query);
|
|
126
|
+
const shouldTrackPerformance = includeServerTimingHeader || process.env.SERVER_TIMING;
|
|
127
|
+
res.__performanceTimer = new _performance.default({
|
|
128
|
+
enabled: shouldTrackPerformance
|
|
129
|
+
});
|
|
130
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.total, 'start');
|
|
131
|
+
const AppConfig = (0, _compatibility.getAppConfig)();
|
|
132
|
+
// Get the application config which should have been stored at this point.
|
|
133
|
+
const config = (0, _ssrConfig.getConfig)();
|
|
134
|
+
AppConfig.restore(res.locals);
|
|
135
|
+
const routes = (0, _routeComponent.getRoutes)(res.locals);
|
|
136
|
+
const WrappedApp = (0, _routeComponent.routeComponent)(_app.default, false, res.locals);
|
|
137
|
+
const [pathname] = req.originalUrl.split('?');
|
|
138
|
+
const location = {
|
|
139
|
+
pathname,
|
|
140
|
+
search: getLocationSearch(req, {
|
|
141
|
+
interpretPlusSignAsSpace: config === null || config === void 0 ? void 0 : (_config$app = config.app) === null || _config$app === void 0 ? void 0 : (_config$app$url = _config$app.url) === null || _config$app$url === void 0 ? void 0 : _config$app$url.interpretPlusSignAsSpace
|
|
142
|
+
})
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Step 1 - Find the match.
|
|
146
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.routeMatching, 'start');
|
|
147
|
+
let route;
|
|
148
|
+
let match;
|
|
149
|
+
const basepath = ((_AppConfig$getBasePat = AppConfig.getBasePath) === null || _AppConfig$getBasePat === void 0 ? void 0 : (_AppConfig$getBasePat2 = _AppConfig$getBasePat.call(AppConfig, {
|
|
150
|
+
req,
|
|
151
|
+
res
|
|
152
|
+
})) === null || _AppConfig$getBasePat2 === void 0 ? void 0 : _AppConfig$getBasePat2.replace(/\/$/, '')) || '';
|
|
153
|
+
routes.some(r => {
|
|
154
|
+
let _route = _objectSpread({}, r);
|
|
155
|
+
if (basepath && _route !== null && _route !== void 0 && _route.path) {
|
|
156
|
+
_route.path = `${basepath}${_route.path}`;
|
|
157
|
+
}
|
|
158
|
+
const _match = (0, _reactRouterDom.matchPath)(req.path, _route);
|
|
159
|
+
if (_match) {
|
|
160
|
+
match = _match;
|
|
161
|
+
route = _route;
|
|
162
|
+
}
|
|
163
|
+
return !!match;
|
|
164
|
+
});
|
|
165
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.routeMatching, 'end');
|
|
166
|
+
|
|
167
|
+
// Step 2 - Get the component
|
|
168
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.loadComponent, 'start');
|
|
169
|
+
const component = match ? yield route.component.getComponent() : _throw2.default;
|
|
170
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.loadComponent, 'end');
|
|
171
|
+
|
|
172
|
+
// Step 3 - Init the app state
|
|
173
|
+
const props = {
|
|
174
|
+
error: null,
|
|
175
|
+
appState: {},
|
|
176
|
+
routerContext: {},
|
|
177
|
+
req,
|
|
178
|
+
res,
|
|
179
|
+
App: WrappedApp,
|
|
180
|
+
routes,
|
|
181
|
+
location
|
|
182
|
+
};
|
|
183
|
+
let appJSX = /*#__PURE__*/_react.default.createElement(OuterApp, props);
|
|
184
|
+
let appState, appStateError;
|
|
185
|
+
if (component === _throw2.default) {
|
|
186
|
+
appState = {};
|
|
187
|
+
appStateError = new errors.HTTPNotFound('Not found');
|
|
188
|
+
} else {
|
|
189
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.fetchStrategies, 'start');
|
|
190
|
+
const ret = yield AppConfig.initAppState({
|
|
191
|
+
App: WrappedApp,
|
|
192
|
+
component,
|
|
193
|
+
match,
|
|
194
|
+
route,
|
|
195
|
+
req,
|
|
196
|
+
res,
|
|
197
|
+
location,
|
|
198
|
+
appJSX
|
|
199
|
+
});
|
|
200
|
+
appState = _objectSpread(_objectSpread({}, ret.appState), {}, {
|
|
201
|
+
__STATE_MANAGEMENT_LIBRARY: AppConfig.freeze(res.locals)
|
|
202
|
+
});
|
|
203
|
+
appStateError = ret.error;
|
|
204
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.fetchStrategies, 'end');
|
|
205
|
+
}
|
|
206
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.renderToString, 'start');
|
|
207
|
+
appJSX = /*#__PURE__*/_react.default.cloneElement(appJSX, {
|
|
208
|
+
error: appStateError,
|
|
209
|
+
appState
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Step 4 - Render the App
|
|
213
|
+
let renderResult;
|
|
214
|
+
try {
|
|
215
|
+
renderResult = renderApp({
|
|
216
|
+
App: WrappedApp,
|
|
217
|
+
appState,
|
|
218
|
+
appStateError: appStateError && logAndFormatError(appStateError),
|
|
219
|
+
routes,
|
|
220
|
+
req,
|
|
221
|
+
res,
|
|
222
|
+
location,
|
|
223
|
+
config,
|
|
224
|
+
appJSX
|
|
225
|
+
});
|
|
226
|
+
} catch (e) {
|
|
227
|
+
// This is an unrecoverable error.
|
|
228
|
+
// (errors handled by the AppErrorBoundary are considered recoverable)
|
|
229
|
+
// Here, we use Express's convention to invoke error middleware.
|
|
230
|
+
// Note, we don't have an error handling middleware yet! This is calling the
|
|
231
|
+
// default error handling middleware provided by Express
|
|
232
|
+
return next(e);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Step 5 - Determine what is going to happen, redirect, or send html with
|
|
236
|
+
// the correct status code.
|
|
237
|
+
const {
|
|
238
|
+
html,
|
|
239
|
+
routerContext,
|
|
240
|
+
error
|
|
241
|
+
} = renderResult;
|
|
242
|
+
const redirectUrl = routerContext.url;
|
|
243
|
+
const status = error && error.status || res.statusCode;
|
|
244
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.renderToString, 'end');
|
|
245
|
+
res.__performanceTimer.mark(_performance.PERFORMANCE_MARKS.total, 'end');
|
|
246
|
+
res.__performanceTimer.log();
|
|
247
|
+
if (includeServerTimingHeader) {
|
|
248
|
+
res.setHeader('Server-Timing', res.__performanceTimer.buildServerTimingHeader());
|
|
249
|
+
}
|
|
250
|
+
if (redirectUrl) {
|
|
251
|
+
res.redirect(302, redirectUrl);
|
|
252
|
+
} else {
|
|
253
|
+
res.status(status).send(html);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
return function render(_x, _x2, _x3) {
|
|
257
|
+
return _ref.apply(this, arguments);
|
|
258
|
+
};
|
|
259
|
+
}();
|
|
260
|
+
const OuterApp = ({
|
|
261
|
+
req,
|
|
262
|
+
res,
|
|
263
|
+
error,
|
|
264
|
+
App,
|
|
265
|
+
appState,
|
|
266
|
+
routes,
|
|
267
|
+
routerContext,
|
|
268
|
+
location
|
|
269
|
+
}) => {
|
|
270
|
+
var _AppConfig$getBasePat3, _AppConfig$getBasePat4;
|
|
271
|
+
const AppConfig = (0, _compatibility.getAppConfig)();
|
|
272
|
+
const basepath = ((_AppConfig$getBasePat3 = AppConfig.getBasePath) === null || _AppConfig$getBasePat3 === void 0 ? void 0 : (_AppConfig$getBasePat4 = _AppConfig$getBasePat3.call(AppConfig, {
|
|
273
|
+
req,
|
|
274
|
+
res
|
|
275
|
+
})) === null || _AppConfig$getBasePat4 === void 0 ? void 0 : _AppConfig$getBasePat4.replace(/\/$/, '')) || '';
|
|
276
|
+
return /*#__PURE__*/_react.default.createElement(_contexts.ServerContext.Provider, {
|
|
277
|
+
value: {
|
|
278
|
+
req,
|
|
279
|
+
res
|
|
280
|
+
}
|
|
281
|
+
}, /*#__PURE__*/_react.default.createElement(_reactRouterDom.StaticRouter, {
|
|
282
|
+
location: location,
|
|
283
|
+
context: routerContext,
|
|
284
|
+
basename: basepath
|
|
285
|
+
}, /*#__PURE__*/_react.default.createElement(_contexts.CorrelationIdProvider, {
|
|
286
|
+
correlationId: res.locals.requestId,
|
|
287
|
+
resetOnPageChange: false
|
|
288
|
+
}, /*#__PURE__*/_react.default.createElement(AppConfig, {
|
|
289
|
+
locals: res.locals
|
|
290
|
+
}, /*#__PURE__*/_react.default.createElement(_switch.default, {
|
|
291
|
+
error: error,
|
|
292
|
+
appState: appState,
|
|
293
|
+
routes: routes,
|
|
294
|
+
App: App
|
|
295
|
+
})))));
|
|
296
|
+
};
|
|
297
|
+
OuterApp.propTypes = {
|
|
298
|
+
req: _propTypes.default.object,
|
|
299
|
+
res: _propTypes.default.object,
|
|
300
|
+
error: _propTypes.default.object,
|
|
301
|
+
App: _propTypes.default.elementType,
|
|
302
|
+
appState: _propTypes.default.object,
|
|
303
|
+
routes: _propTypes.default.array,
|
|
304
|
+
routerContext: _propTypes.default.object,
|
|
305
|
+
location: _propTypes.default.object
|
|
306
|
+
};
|
|
307
|
+
const renderToString = (jsx, extractor) => _server.default.renderToString(extractor.collectChunks(jsx));
|
|
308
|
+
const renderApp = args => {
|
|
309
|
+
const {
|
|
310
|
+
req,
|
|
311
|
+
res,
|
|
312
|
+
appStateError,
|
|
313
|
+
appJSX,
|
|
314
|
+
appState,
|
|
315
|
+
config
|
|
316
|
+
} = args;
|
|
317
|
+
const extractor = new _server2.ChunkExtractor({
|
|
318
|
+
statsFile: BUNDLES_PATH,
|
|
319
|
+
publicPath: (0, _utils.getAssetUrl)()
|
|
320
|
+
});
|
|
321
|
+
const ssrOnly = 'mobify_server_only' in req.query || '__server_only' in req.query;
|
|
322
|
+
const prettyPrint = 'mobify_pretty' in req.query || '__pretty_print' in req.query;
|
|
323
|
+
const indent = prettyPrint ? 8 : 0;
|
|
324
|
+
let routerContext;
|
|
325
|
+
let appHtml;
|
|
326
|
+
let renderError;
|
|
327
|
+
// It's important that we render the App before extracting the script elements,
|
|
328
|
+
// otherwise it won't return the correct chunks.
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
routerContext = {};
|
|
332
|
+
appHtml = renderToString( /*#__PURE__*/_react.default.cloneElement(appJSX, {
|
|
333
|
+
routerContext
|
|
334
|
+
}), extractor);
|
|
335
|
+
} catch (e) {
|
|
336
|
+
// This will catch errors thrown from the app and pass the error
|
|
337
|
+
// to the AppErrorBoundary component, and renders the error page.
|
|
338
|
+
routerContext = {};
|
|
339
|
+
renderError = logAndFormatError(e);
|
|
340
|
+
appHtml = renderToString( /*#__PURE__*/_react.default.cloneElement(appJSX, {
|
|
341
|
+
routerContext,
|
|
342
|
+
error: renderError
|
|
343
|
+
}), extractor);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Setting type: 'application/json' stops the browser from executing the code.
|
|
347
|
+
const scriptProps = ssrOnly ? {
|
|
348
|
+
type: 'application/json'
|
|
349
|
+
} : {};
|
|
350
|
+
let bundles = [];
|
|
351
|
+
/* istanbul ignore next */
|
|
352
|
+
if (extractor) {
|
|
353
|
+
bundles = extractor.getScriptElements().map(el => /*#__PURE__*/_react.default.cloneElement(el, _objectSpread(_objectSpread({}, el.props), scriptProps)));
|
|
354
|
+
}
|
|
355
|
+
const helmet = _reactHelmet.Helmet.renderStatic();
|
|
356
|
+
|
|
357
|
+
// Return the first error encountered during the rendering pipeline.
|
|
358
|
+
const error = appStateError || renderError;
|
|
359
|
+
// Remove the stacktrace when executing remotely as to not leak any important
|
|
360
|
+
// information to users about our system.
|
|
361
|
+
if (error && (0, _ssrServer.isRemote)()) {
|
|
362
|
+
delete error.stack;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Do not include *dynamic*, executable inline scripts – these cause issues with
|
|
366
|
+
// strict CSP headers that customers often want to use. Avoid inline scripts,
|
|
367
|
+
// full-stop, whenever possible.
|
|
368
|
+
|
|
369
|
+
// Each key in `windowGlobals` is expected to be set on the window
|
|
370
|
+
// object, client-side, by code in ssr/browser/main.jsx.
|
|
371
|
+
//
|
|
372
|
+
// Do *not* add to these without a very good reason - globals are a liability.
|
|
373
|
+
const windowGlobals = {
|
|
374
|
+
__INITIAL_CORRELATION_ID__: res.locals.requestId,
|
|
375
|
+
__CONFIG__: config,
|
|
376
|
+
__PRELOADED_STATE__: appState,
|
|
377
|
+
__ERROR__: error,
|
|
378
|
+
// `window.Progressive` has a long history at Mobify and some
|
|
379
|
+
// client-side code depends on it. Maintain its name out of tradition.
|
|
380
|
+
Progressive: getWindowProgressive(req, res)
|
|
381
|
+
};
|
|
382
|
+
const scripts = [/*#__PURE__*/_react.default.createElement("script", {
|
|
383
|
+
id: "mobify-data",
|
|
384
|
+
key: "mobify-data",
|
|
385
|
+
type: "application/json" // Not executable
|
|
386
|
+
,
|
|
387
|
+
dangerouslySetInnerHTML: {
|
|
388
|
+
__html: (0, _serializeJavascript.default)(windowGlobals, {
|
|
389
|
+
isJSON: true,
|
|
390
|
+
space: indent
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
}), ...bundles];
|
|
394
|
+
const svgs = [/*#__PURE__*/_react.default.createElement("div", {
|
|
395
|
+
key: "svg_sprite",
|
|
396
|
+
dangerouslySetInnerHTML: {
|
|
397
|
+
__html: _sprite.default.stringify()
|
|
398
|
+
}
|
|
399
|
+
})];
|
|
400
|
+
const helmetHeadTags = VALID_TAG_NAMES.map(tag => helmet[tag] && helmet[tag].toComponent()).filter(tag => tag);
|
|
401
|
+
const html = _server.default.renderToString( /*#__PURE__*/_react.default.createElement(_document.default, {
|
|
402
|
+
head: [...helmetHeadTags],
|
|
403
|
+
html: appHtml,
|
|
404
|
+
afterBodyStart: svgs,
|
|
405
|
+
beforeBodyEnd: scripts,
|
|
406
|
+
htmlAttributes: helmet.htmlAttributes.toComponent(),
|
|
407
|
+
bodyAttributes: helmet.bodyAttributes.toComponent()
|
|
408
|
+
}));
|
|
409
|
+
return {
|
|
410
|
+
error,
|
|
411
|
+
html: ['<!doctype html>', html].join(''),
|
|
412
|
+
routerContext
|
|
413
|
+
};
|
|
414
|
+
};
|
|
415
|
+
const getWindowProgressive = (req, res) => {
|
|
416
|
+
const options = req.app.options || {};
|
|
417
|
+
return {
|
|
418
|
+
buildOrigin: (0, _utils.getAssetUrl)(''),
|
|
419
|
+
cacheManifest: options.cacheHashManifest || {},
|
|
420
|
+
ssrOptions: {
|
|
421
|
+
// The hostname and origin under which this page is served
|
|
422
|
+
appHostname: options.appHostname,
|
|
423
|
+
appOrigin: options.appOrigin,
|
|
424
|
+
// The id of the bundle being served, as a string,
|
|
425
|
+
// defaulting to 'development' for the local dev server
|
|
426
|
+
bundleId: process.env.BUNDLE_ID || 'development',
|
|
427
|
+
// The id of the deploy as a string, defaulting to '0'
|
|
428
|
+
// for the local dev server
|
|
429
|
+
deployId: process.env.DEPLOY_ID || '0',
|
|
430
|
+
// On a local dev server, the DEPLOY_TARGET environment variable
|
|
431
|
+
// isn't defined by default. Developers may define it if it's
|
|
432
|
+
// used by the UPWA to modify behaviour.
|
|
433
|
+
deployTarget: process.env.DEPLOY_TARGET || 'local',
|
|
434
|
+
proxyConfigs: _ssrShared.proxyConfigs,
|
|
435
|
+
// The request class (undefined by default)
|
|
436
|
+
requestClass: res.locals.requestClass
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
};
|
|
440
|
+
const serverRenderer =
|
|
441
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
442
|
+
({
|
|
443
|
+
clientStats,
|
|
444
|
+
serverStats
|
|
445
|
+
}) => {
|
|
446
|
+
return (req, res, next) => render(req, res, next);
|
|
447
|
+
};
|
|
448
|
+
var _default = exports.default = serverRenderer;
|