1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.RenderService = undefined;
- exports.default = preSSRService;
-
- var _render = require('./render');
-
- var _render2 = _interopRequireDefault(_render);
-
- var _utils = require('./utils');
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
-
- function getPathsFromRoutes(routes) {
- const paths = [];
- (0, _utils.searchRoutes)(routes, route => {
- paths.push(route.props.path);
- });
- return paths;
- }
-
- class RenderService {
- constructor({ url, interval, renderOptions }) {
- this.url = url;
- this.timeout = interval;
- this.renderOptions = renderOptions;
- }
-
- render() {
- var _this = this;
-
- return _asyncToGenerator(function* () {
- yield (0, _render2.default)(_this.renderOptions);
- _this.timer = setTimeout(_this.run.bind(_this), _this.timeout);
- })();
- }
-
- run() {
- this.render();
- }
-
- stop() {
- if (this.timer) {
- clearTimeout(this.timer);
- }
- }
- }
-
- exports.RenderService = RenderService;
- function preSSRService({
- renderFullPage, createApp, initialState, interval = 10000, onRenderSuccess, routes, timeout = 6000, verbose = true
- }) {
- const paths = getPathsFromRoutes(routes);
- paths.forEach(path => {
- new RenderService({
- url: path,
- interval,
- renderOptions: {
- routes,
- url: path,
- renderFullPage,
- createApp,
- initialState,
- onRenderSuccess,
- timeout,
- env: {
- platform: 'pc'
- },
- verbose
- }
- }).run();
- new RenderService({
- url: path,
- interval,
- renderOptions: {
- routes,
- url: path,
- renderFullPage,
- createApp,
- initialState,
- onRenderSuccess,
- timeout,
- env: {
- platform: 'mobile'
- },
- verbose
- }
- }).run();
- });
- }
|