react-app-renderer
Version:
147 lines • 8.24 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as queryString from 'query-string';
import { loadableReady } from '@loadable/component';
var __initialData__;
export function setInitialData(initialData) {
__initialData__ = initialData;
}
export function getInitialData() {
return __initialData__;
}
export function getRenderApp(runtime, options) {
var _a, _b;
var ErrorBoundary = options.ErrorBoundary, _c = options.appConfig, appConfig = _c === void 0 ? { app: {} } : _c;
var AppProvider = (_a = runtime === null || runtime === void 0 ? void 0 : runtime.composeAppProvider) === null || _a === void 0 ? void 0 : _a.call(runtime);
var AppComponent = (_b = runtime === null || runtime === void 0 ? void 0 : runtime.getAppComponent) === null || _b === void 0 ? void 0 : _b.call(runtime);
var rootApp = React.createElement(AppComponent, null);
if (AppProvider) {
rootApp = (React.createElement(AppProvider, null, rootApp));
}
var _d = appConfig.app, ErrorBoundaryFallback = _d.ErrorBoundaryFallback, onErrorBoundaryHandler = _d.onErrorBoundaryHandler, errorBoundary = _d.errorBoundary, _e = _d.strict, strict = _e === void 0 ? false : _e;
function App() {
// ErrorBoundary is missing in SSR
if (errorBoundary && ErrorBoundary) {
rootApp = (React.createElement(ErrorBoundary, { Fallback: ErrorBoundaryFallback, onError: onErrorBoundaryHandler }, rootApp));
}
if (strict) {
rootApp = (React.createElement(React.StrictMode, null, rootApp));
}
return rootApp;
}
return App;
}
export function reactAppRenderer(options) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var appConfig, _b, buildConfig, appLifecycle, createBaseApp, emitLifeCycles, initAppLifeCycles, context, _c, href, origin_1, pathname, search, path, query, ssrError, initialContext, _d, _e, runtime, modifiedAppConfig;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
appConfig = options.appConfig, _b = options.buildConfig, buildConfig = _b === void 0 ? {} : _b, appLifecycle = options.appLifecycle;
createBaseApp = appLifecycle.createBaseApp, emitLifeCycles = appLifecycle.emitLifeCycles, initAppLifeCycles = appLifecycle.initAppLifeCycles;
context = {};
if (!window.__ICE_APP_DATA__) return [3 /*break*/, 1];
context.initialData = window.__ICE_APP_DATA__;
context.pageInitialProps = window.__ICE_PAGE_PROPS__;
return [3 /*break*/, 3];
case 1:
if (!((_a = appConfig === null || appConfig === void 0 ? void 0 : appConfig.app) === null || _a === void 0 ? void 0 : _a.getInitialData)) return [3 /*break*/, 3];
_c = window.location, href = _c.href, origin_1 = _c.origin, pathname = _c.pathname, search = _c.search;
path = href.replace(origin_1, '');
query = queryString.parse(search);
ssrError = window.__ICE_SSR_ERROR__;
initialContext = {
pathname: pathname,
path: path,
query: query,
ssrError: ssrError
};
_d = context;
return [4 /*yield*/, appConfig.app.getInitialData(initialContext)];
case 2:
_d.initialData = _f.sent();
_f.label = 3;
case 3:
_e = createBaseApp(appConfig, buildConfig, context), runtime = _e.runtime, modifiedAppConfig = _e.appConfig;
// init app life cycles after app runtime created
initAppLifeCycles();
// set InitialData, can get the return value through getInitialData method
setInitialData(context.initialData);
// emit app launch cycle
emitLifeCycles();
return [2 /*return*/, _render(runtime, __assign(__assign({}, options), { appConfig: modifiedAppConfig }))];
}
});
});
}
function _render(runtime, options) {
var _a;
var _b = options.appConfig, appConfig = _b === void 0 ? {} : _b;
var _c = appConfig.app, rootId = _c.rootId, mountNode = _c.mountNode;
var App = getRenderApp(runtime, options);
var appMountNode = _getAppMountNode(mountNode, rootId);
if (runtime === null || runtime === void 0 ? void 0 : runtime.modifyDOMRender) {
return (_a = runtime === null || runtime === void 0 ? void 0 : runtime.modifyDOMRender) === null || _a === void 0 ? void 0 : _a.call(runtime, { App: App, appMountNode: appMountNode });
}
// add process.env.SSR for tree-shaking
if (window.__ICE_SSR_ENABLED__ && process.env.SSR) {
loadableReady(function () {
ReactDOM.hydrate(React.createElement(App, null), appMountNode);
});
}
else {
ReactDOM.render(React.createElement(App, null), appMountNode);
}
}
function _getAppMountNode(mountNode, rootId) {
return mountNode || document.getElementById(rootId) || document.getElementById('ice-container');
}
//# sourceMappingURL=renderer.js.map