UNPKG

react-ssr

Version:

A simplified solution to React server side rendering.

75 lines (56 loc) 2.67 kB
'use strict'; var _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; }; function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var fetchData = require('../fetchData'); var extractFetchData = function extractFetchData(component, _ref) { var match = _ref.match, req = _ref.req, res = _ref.res; var requiresData = component.fetchData; var ssrWaitsFor = component.ssrWaitsFor; if (requiresData || ssrWaitsFor) { return fetchData(component, match, req, res); } }; var checkRoute = function checkRoute(options) { var route = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var routeCalls = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; var components = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; if (route.routes) { var childCalls = route.routes.map(function (route) { if (route && route.path && route.path.includes(options.url)) { var result = checkRoute(options, route, routeCalls, components) || []; components.concat(result.components); return result.routeCalls || []; } }); routeCalls = routeCalls.concat(childCalls); } if (route.component) { routeCalls.push(extractFetchData(route.component, options)); } return { routeCalls: routeCalls, components: components }; }; var flatten = function flatten(arr) { var _ref2; return Array.isArray(arr) ? (_ref2 = []).concat.apply(_ref2, _toConsumableArray(arr.map(flatten))) : arr; }; var findAllDataCalls = function findAllDataCalls() { var matchedRoutes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var promises = []; matchedRoutes.forEach(function (matchedRoute) { var _checkRoute = checkRoute(_extends({}, options, { match: matchedRoute.match }), matchedRoute.route), _checkRoute$routeCall = _checkRoute.routeCalls, routeCalls = _checkRoute$routeCall === undefined ? [] : _checkRoute$routeCall; promises = promises.concat(routeCalls); }); var flattenedPromises = flatten(promises).filter(function (promise) { return typeof promise !== 'undefined'; }); return flattenedPromises; }; module.exports = findAllDataCalls;