index.js 20.2 KB
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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var runtime = require('@umijs/runtime');
var React = require('react');
var reactDom = require('react-dom');
var reactRouterConfig = require('react-router-config');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);

  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);

    if (enumerableOnly) {
      symbols = symbols.filter(function (sym) {
        return Object.getOwnPropertyDescriptor(object, sym).enumerable;
      });
    }

    keys.push.apply(keys, symbols);
  }

  return keys;
}

function _objectSpread2(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = arguments[i] != null ? arguments[i] : {};

    if (i % 2) {
      ownKeys(Object(source), true).forEach(function (key) {
        _defineProperty(target, key, source[key]);
      });
    } else if (Object.getOwnPropertyDescriptors) {
      Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
      ownKeys(Object(source)).forEach(function (key) {
        Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
      });
    }
  }

  return target;
}

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  try {
    var info = gen[key](arg);
    var value = info.value;
  } catch (error) {
    reject(error);
    return;
  }

  if (info.done) {
    resolve(value);
  } else {
    Promise.resolve(value).then(_next, _throw);
  }
}

function _asyncToGenerator(fn) {
  return function () {
    var self = this,
        args = arguments;
    return new Promise(function (resolve, reject) {
      var gen = fn.apply(self, args);

      function _next(value) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
      }

      function _throw(err) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
      }

      _next(undefined);
    });
  };
}

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;

  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }

  return target;
}

function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};

  var target = _objectWithoutPropertiesLoose(source, excluded);

  var key, i;

  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);

    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }

  return target;
}

function _slicedToArray(arr, i) {
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}

function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}

function _iterableToArrayLimit(arr, i) {
  var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);

  if (_i == null) return;
  var _arr = [];
  var _n = true;
  var _d = false;

  var _s, _e;

  try {
    for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
      _arr.push(_s.value);

      if (i && _arr.length === i) break;
    }
  } catch (err) {
    _d = true;
    _e = err;
  } finally {
    try {
      if (!_n && _i["return"] != null) _i["return"]();
    } finally {
      if (_d) throw _e;
    }
  }

  return _arr;
}

function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === "Object" && o.constructor) n = o.constructor.name;
  if (n === "Map" || n === "Set") return Array.from(o);
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}

function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;

  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];

  return arr2;
}

function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

function _createForOfIteratorHelper(o, allowArrayLike) {
  var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];

  if (!it) {
    if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
      if (it) o = it;
      var i = 0;

      var F = function () {};

      return {
        s: F,
        n: function () {
          if (i >= o.length) return {
            done: true
          };
          return {
            done: false,
            value: o[i++]
          };
        },
        e: function (e) {
          throw e;
        },
        f: F
      };
    }

    throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  }

  var normalCompletion = true,
      didErr = false,
      err;
  return {
    s: function () {
      it = it.call(o);
    },
    n: function () {
      var step = it.next();
      normalCompletion = step.done;
      return step;
    },
    e: function (e) {
      didErr = true;
      err = e;
    },
    f: function () {
      try {
        if (!normalCompletion && it.return != null) it.return();
      } finally {
        if (didErr) throw err;
      }
    }
  };
}

function Route(props) {
  return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Consumer, null, function (context) {
    var location = props.location || context.location;
    var match = props.computedMatch;

    var newProps = _objectSpread2(_objectSpread2({}, context), {}, {
      location: location,
      match: match
    });

    var render = props.render;
    return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Provider, {
      value: newProps
    }, newProps.match ? render(_objectSpread2(_objectSpread2({}, props.layoutProps), newProps)) : null);
  });
}

function Switch(props) {
  return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Consumer, null, function (context) {
    var children = props.children,
        extraProps = _objectWithoutProperties(props, ["children"]);

    var location = props.location || context.location;
    var element,
        match = null;
    React.Children.forEach(children, function (child) {
      if (match === null && /*#__PURE__*/React.isValidElement(child)) {
        element = child;
        var path = child.props.path || child.props.from;
        match = path ? runtime.matchPath(location.pathname, _objectSpread2(_objectSpread2({}, child.props), {}, {
          path: path
        })) : context.match;
      }
    });
    return match ? /*#__PURE__*/React.cloneElement(element, {
      location: location,
      computedMatch: match,
      layoutProps: extraProps
    }) : null;
  });
}

function wrapInitialPropsFetch(route, opts) {
  var component = route.component,
      restRouteParams = _objectWithoutProperties(route, ["component"]);

  var Component = route.component;

  function ComponentWithInitialPropsFetch(props) {
    var _useState = React.useState(function () {
      return window.g_initialProps;
    }),
        _useState2 = _slicedToArray(_useState, 2),
        initialProps = _useState2[0],
        setInitialProps = _useState2[1];

    React.useEffect(function () {
      /**
       * 1. 首次渲染时,此时 window.g_initialProps 变量存在,不需要再走一次 getInitialProps,这样一次 SSR 就走了 2 次 getInitialProps
       * 2. 但是路由切换时,window.getInitialProps 会被赋为 null,这时候就走 getInitialProps 逻辑
       * 3. 如果任何时候都走 2 次,配置 forceInitial: true,这个场景用于静态站点的首屏加载希望走最新数据
       * 4. 开启动态加载后,会在执行 getInitialProps 前预加载下
       */
      var handleGetInitialProps = /*#__PURE__*/function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var _preloadComponent;

          var preloadComponent, defaultCtx, ctx, _initialProps;

          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  // preload when enalbe dynamicImport
                  preloadComponent = Component;

                  if (!Component.preload) {
                    _context.next = 6;
                    break;
                  }

                  _context.next = 4;
                  return Component.preload();

                case 4:
                  preloadComponent = _context.sent;
                  // for test case, really use .default
                  preloadComponent = preloadComponent.default || preloadComponent;

                case 6:
                  defaultCtx = _objectSpread2(_objectSpread2({
                    isServer: false,
                    match: props === null || props === void 0 ? void 0 : props.match,
                    history: props === null || props === void 0 ? void 0 : props.history,
                    route: route
                  }, opts.getInitialPropsCtx || {}), restRouteParams);

                  if (!((_preloadComponent = preloadComponent) === null || _preloadComponent === void 0 ? void 0 : _preloadComponent.getInitialProps)) {
                    _context.next = 15;
                    break;
                  }

                  _context.next = 10;
                  return opts.plugin.applyPlugins({
                    key: 'ssr.modifyGetInitialPropsCtx',
                    type: runtime.ApplyPluginsType.modify,
                    initialValue: defaultCtx,
                    async: true
                  });

                case 10:
                  ctx = _context.sent;
                  _context.next = 13;
                  return preloadComponent.getInitialProps(ctx || defaultCtx);

                case 13:
                  _initialProps = _context.sent;
                  setInitialProps(_initialProps);

                case 15:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        return function handleGetInitialProps() {
          return _ref.apply(this, arguments);
        };
      }(); // null 时,一定会触发 getInitialProps 执行


      if (!window.g_initialProps) {
        handleGetInitialProps();
      }
    }, [window.location.pathname, window.location.search]);
    return /*#__PURE__*/React__default['default'].createElement(Component, _extends({}, props, initialProps));
  } // flag for having wrappered


  ComponentWithInitialPropsFetch.wrapInitialPropsLoaded = true;
  ComponentWithInitialPropsFetch.displayName = 'ComponentWithInitialPropsFetch';
  return ComponentWithInitialPropsFetch;
}

function _render(_ref2) {
  var route = _ref2.route,
      opts = _ref2.opts,
      props = _ref2.props;
  var routes = renderRoutes(_objectSpread2(_objectSpread2({}, opts), {}, {
    routes: route.routes || [],
    rootRoutes: opts.rootRoutes
  }), {
    location: props.location
  });
  var Component = route.component,
      wrappers = route.wrappers;

  if (Component) {
    var defaultPageInitialProps = opts.isServer ? {} : window.g_initialProps;

    var newProps = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, props), opts.extraProps), opts.pageInitialProps || defaultPageInitialProps), {}, {
      route: route,
      routes: opts.rootRoutes
    }); // @ts-ignore


    var ret = /*#__PURE__*/React__default['default'].createElement(Component, newProps, routes); // route.wrappers

    if (wrappers) {
      var len = wrappers.length - 1;

      while (len >= 0) {
        ret = /*#__PURE__*/React.createElement(wrappers[len], newProps, ret);
        len -= 1;
      }
    }

    return ret;
  } else {
    return routes;
  }
}

function getRouteElement(_ref3) {
  var route = _ref3.route,
      index = _ref3.index,
      opts = _ref3.opts;
  var routeProps = {
    key: route.key || index,
    exact: route.exact,
    strict: route.strict,
    sensitive: route.sensitive,
    path: route.path
  };

  if (route.redirect) {
    return /*#__PURE__*/React__default['default'].createElement(runtime.Redirect, _extends({}, routeProps, {
      from: route.path,
      to: route.redirect
    }));
  } else {
    var _route$component, _route$component2, _route$component3;

    // avoid mount and unmount with url hash change
    if ( // only when SSR config enable
    opts.ssrProps && !opts.isServer && // make sure loaded once
    !((_route$component = route.component) === null || _route$component === void 0 ? void 0 : _route$component.wrapInitialPropsLoaded) && ( // TODO need a type
    ((_route$component2 = route.component) === null || _route$component2 === void 0 ? void 0 : _route$component2.getInitialProps) || ((_route$component3 = route.component) === null || _route$component3 === void 0 ? void 0 : _route$component3.preload))) {
      // client Render for enable ssr, but not sure SSR success
      route.component = wrapInitialPropsFetch(route, opts);
    }

    return /*#__PURE__*/React__default['default'].createElement(Route, _extends({}, routeProps, {
      render: function render(props) {
        return _render({
          route: route,
          opts: opts,
          props: props
        });
      }
    }));
  }
}

function renderRoutes(opts) {
  var switchProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return opts.routes ? /*#__PURE__*/React__default['default'].createElement(Switch, switchProps, opts.routes.map(function (route, index) {
    return getRouteElement({
      route: route,
      index: index,
      opts: _objectSpread2(_objectSpread2({}, opts), {}, {
        rootRoutes: opts.rootRoutes || opts.routes
      })
    });
  })) : null;
}

function RouterComponent(props) {
  var history = props.history,
      renderRoutesProps = _objectWithoutProperties(props, ["history"]);

  React.useEffect(function () {
    // first time using window.g_initialProps
    // switch route fetching data, if exact route reset window.getInitialProps
    if (window.g_useSSR) {
      window.g_initialProps = null;
    }

    function routeChangeHandler(location, action) {
      var matchedRoutes = reactRouterConfig.matchRoutes(props.routes, location.pathname); // Set title

      if (typeof document !== 'undefined' && renderRoutesProps.defaultTitle !== undefined) {
        document.title = matchedRoutes.length && // @ts-ignore
        matchedRoutes[matchedRoutes.length - 1].route.title || renderRoutesProps.defaultTitle || '';
      }

      props.plugin.applyPlugins({
        key: 'onRouteChange',
        type: runtime.ApplyPluginsType.event,
        args: {
          routes: props.routes,
          matchedRoutes: matchedRoutes,
          location: location,
          action: action
        }
      });
    }

    routeChangeHandler(history.location, 'POP');
    return history.listen(routeChangeHandler);
  }, [history]);
  return /*#__PURE__*/React__default['default'].createElement(runtime.Router, {
    history: history
  }, renderRoutes(renderRoutesProps));
}
/**
 * preload for SSR in dynamicImport
 * exec preload Promise function before ReactDOM.hydrate
 * @param Routes
 */


function preloadComponent(_x) {
  return _preloadComponent.apply(this, arguments);
}

function _preloadComponent() {
  _preloadComponent = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(readyRoutes) {
    var pathname,
        matchedRoutes,
        _iterator,
        _step,
        _route$component,
        matchRoute,
        route,
        _preloadComponent2,
        _args = arguments;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            pathname = _args.length > 1 && _args[1] !== undefined ? _args[1] : window.location.pathname;
            // using matched routes not load all routes
            matchedRoutes = reactRouterConfig.matchRoutes(readyRoutes, pathname);
            _iterator = _createForOfIteratorHelper(matchedRoutes);
            _context.prev = 3;

            _iterator.s();

          case 5:
            if ((_step = _iterator.n()).done) {
              _context.next = 19;
              break;
            }

            matchRoute = _step.value;
            route = matchRoute.route; // load all preload function, because of only a chance to load

            if (!(typeof route.component !== 'string' && ((_route$component = route.component) === null || _route$component === void 0 ? void 0 : _route$component.preload))) {
              _context.next = 13;
              break;
            }

            _context.next = 11;
            return route.component.preload();

          case 11:
            _preloadComponent2 = _context.sent;
            route.component = _preloadComponent2.default || _preloadComponent2;

          case 13:
            if (!route.routes) {
              _context.next = 17;
              break;
            }

            _context.next = 16;
            return preloadComponent(route.routes, pathname);

          case 16:
            route.routes = _context.sent;

          case 17:
            _context.next = 5;
            break;

          case 19:
            _context.next = 24;
            break;

          case 21:
            _context.prev = 21;
            _context.t0 = _context["catch"](3);

            _iterator.e(_context.t0);

          case 24:
            _context.prev = 24;

            _iterator.f();

            return _context.finish(24);

          case 27:
            return _context.abrupt("return", readyRoutes);

          case 28:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[3, 21, 24, 27]]);
  }));
  return _preloadComponent.apply(this, arguments);
}

function renderClient(opts) {
  var rootContainer = opts.plugin.applyPlugins({
    type: runtime.ApplyPluginsType.modify,
    key: 'rootContainer',
    initialValue: /*#__PURE__*/React__default['default'].createElement(RouterComponent, {
      history: opts.history,
      routes: opts.routes,
      plugin: opts.plugin,
      ssrProps: opts.ssrProps,
      defaultTitle: opts.defaultTitle
    }),
    args: {
      history: opts.history,
      routes: opts.routes,
      plugin: opts.plugin
    }
  });

  if (opts.rootElement) {
    var rootElement = typeof opts.rootElement === 'string' ? document.getElementById(opts.rootElement) : opts.rootElement;

    var callback = opts.callback || function () {}; // flag showing SSR successed


    if (window.g_useSSR) {
      if (opts.dynamicImport) {
        // dynamicImport should preload current route component
        // first loades);
        preloadComponent(opts.routes).then(function () {
          reactDom.hydrate(rootContainer, rootElement, callback);
        });
      } else {
        reactDom.hydrate(rootContainer, rootElement, callback);
      }
    } else {
      reactDom.render(rootContainer, rootElement, callback);
    }
  } else {
    return rootContainer;
  }
}

exports.renderClient = renderClient;
exports.renderRoutes = renderRoutes;