react-static
Version:
A progressive static site generator for React
140 lines (109 loc) • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
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; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _shared = require('../../utils/shared');
var _methods = require('../methods');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ioIsSupported = typeof window !== 'undefined' && 'IntersectionObserver' in window;
var handleIntersection = function handleIntersection(element, callback) {
var io = new window.IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
// Edge doesn't support isIntersecting. intersectionRatio > 0 works as a fallback
if (element === entry.target && (entry.isIntersecting || entry.intersectionRatio > 0)) {
io.unobserve(element);
io.disconnect();
callback();
}
});
});
io.observe(element);
};
var PrefetchWhenSeen = function (_React$Component) {
_inherits(PrefetchWhenSeen, _React$Component);
function PrefetchWhenSeen() {
var _ref,
_this2 = this;
var _temp, _this, _ret;
_classCallCheck(this, PrefetchWhenSeen);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = PrefetchWhenSeen.__proto__ || Object.getPrototypeOf(PrefetchWhenSeen)).call.apply(_ref, [this].concat(args))), _this), _this.runPrefetch = function () {
return _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
var _this$props, path, onLoad, type, cleanedPath, data;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_this$props = _this.props, path = _this$props.path, onLoad = _this$props.onLoad, type = _this$props.type;
cleanedPath = (0, _shared.cleanPath)(path);
_context.next = 4;
return (0, _methods.prefetch)(cleanedPath, { type: type });
case 4:
data = _context.sent;
onLoad(data, cleanedPath);
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, _this2);
}))();
}, _this.handleRef = function (ref) {
if (ioIsSupported && ref) {
handleIntersection(ref, _this.runPrefetch);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(PrefetchWhenSeen, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (!ioIsSupported) {
this.runPrefetch();
}
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
component = _props.component,
render = _props.render,
children = _props.children,
rest = _objectWithoutProperties(_props, ['component', 'render', 'children']);
if (component) {
return _react2.default.createElement(component, {
handleRef: this.handleRef
});
}
if (render) {
return render({ handleRef: this.handleRef });
}
return _react2.default.createElement(
'div',
_extends({ ref: this.handleRef }, rest),
children
);
}
}]);
return PrefetchWhenSeen;
}(_react2.default.Component);
PrefetchWhenSeen.defaultProps = {
children: null,
path: null,
className: null,
type: null,
onLoad: function onLoad() {}
};
exports.default = PrefetchWhenSeen;