UNPKG

4.4 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12var __assign = (this && this.__assign) || Object.assign || function(t) {
13 for (var s, i = 1, n = arguments.length; i < n; i++) {
14 s = arguments[i];
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16 t[p] = s[p];
17 }
18 return t;
19};
20var __rest = (this && this.__rest) || function (s, e) {
21 var t = {};
22 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
23 t[p] = s[p];
24 if (s != null && typeof Object.getOwnPropertySymbols === "function")
25 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
26 t[p[i]] = s[p[i]];
27 return t;
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30var React = require("react");
31var react_router_dom_1 = require("react-router-dom");
32var loadInitialProps_1 = require("./loadInitialProps");
33var Afterparty = /** @class */ (function (_super) {
34 __extends(Afterparty, _super);
35 function Afterparty(props) {
36 var _this = _super.call(this, props) || this;
37 _this.prefetch = function (pathname) {
38 loadInitialProps_1.loadInitialProps(_this.props.routes, pathname, {
39 history: _this.props.history,
40 })
41 .then(function (_a) {
42 var data = _a.data;
43 _this.prefetcherCache = Object.assign({}, _this.prefetcherCache, (_b = {},
44 _b[pathname] = data,
45 _b));
46 var _b;
47 })
48 .catch(function (e) { return console.log(e); });
49 };
50 _this.state = {
51 data: props.data,
52 previousLocation: null,
53 };
54 _this.prefetcherCache = {};
55 return _this;
56 }
57 // only runs clizzient
58 Afterparty.prototype.componentWillReceiveProps = function (nextProps, nextState) {
59 var _this = this;
60 var navigated = nextProps.location !== this.props.location;
61 if (navigated) {
62 window.scrollTo(0, 0);
63 // save the location so we can render the old screen
64 this.setState({
65 previousLocation: this.props.location,
66 data: undefined,
67 });
68 var data = nextProps.data, match = nextProps.match, routes = nextProps.routes, history_1 = nextProps.history, location_1 = nextProps.location, rest = __rest(nextProps, ["data", "match", "routes", "history", "location"]);
69 loadInitialProps_1.loadInitialProps(this.props.routes, nextProps.location.pathname, __assign({ location: nextProps.location, history: nextProps.history }, rest))
70 .then(function (_a) {
71 var data = _a.data;
72 _this.setState({ previousLocation: null, data: data });
73 })
74 .catch(function (e) {
75 // @todo we should more cleverly handle errors???
76 console.log(e);
77 });
78 }
79 };
80 Afterparty.prototype.render = function () {
81 var _this = this;
82 var _a = this.state, previousLocation = _a.previousLocation, data = _a.data;
83 var location = this.props.location;
84 var initialData = this.prefetcherCache[location.pathname] || data;
85 return (React.createElement(react_router_dom_1.Switch, null, this.props.routes.map(function (r, i) { return (React.createElement(react_router_dom_1.Route, { key: "route--" + i, path: r.path, exact: r.exact, location: previousLocation || location, render: function (props) {
86 return React.createElement(r.component, __assign({}, initialData, { history: props.history, location: previousLocation || location, match: props.match, prefetch: _this.prefetch }));
87 } })); })));
88 };
89 return Afterparty;
90}(React.Component));
91exports.After = react_router_dom_1.withRouter(Afterparty);
92//# sourceMappingURL=After.js.map
\No newline at end of file