gatsby-plugin-image 2.9.0 → 2.10.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/babel-helpers.js +66 -66
- package/dist/babel-plugin-parse-static-images.js +80 -80
- package/dist/components/gatsby-image.browser.js +247 -247
- package/dist/components/gatsby-image.server.js +112 -112
- package/dist/components/hooks.js +399 -399
- package/dist/components/intersection-observer.js +55 -55
- package/dist/components/layout-wrapper.js +94 -94
- package/dist/components/lazy-hydrate.js +75 -75
- package/dist/components/main-image.js +43 -43
- package/dist/components/picture.js +96 -96
- package/dist/components/placeholder.js +74 -74
- package/dist/components/static-image.server.js +111 -111
- package/dist/gatsby-node.js +54 -54
- package/dist/image-utils.js +449 -449
- package/dist/index.js +27 -27
- package/dist/node-apis/image-processing.js +278 -278
- package/dist/node-apis/node-utils.js +11 -11
- package/dist/node-apis/parser.js +98 -98
- package/dist/node-apis/preprocess-source.js +118 -118
- package/dist/node-apis/watcher.js +129 -129
- package/dist/resolver-utils.js +119 -119
- package/dist/src/babel-helpers.d.ts +6 -6
- package/dist/src/babel-plugin-parse-static-images.d.ts +10 -10
- package/dist/src/gatsby-node.d.ts +5 -5
- package/dist/src/node-apis/image-processing.d.ts +30 -30
- package/dist/src/node-apis/node-utils.d.ts +1 -1
- package/dist/src/node-apis/parser.d.ts +11 -11
- package/dist/src/node-apis/preprocess-source.d.ts +2 -2
- package/dist/src/node-apis/watcher.d.ts +12 -12
- package/dist/src/resolver-utils.d.ts +21 -21
- package/package.json +4 -4
|
@@ -1,247 +1,247 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
29
|
-
if (k2 === undefined) k2 = k;
|
|
30
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
31
|
-
}) : (function(o, m, k, k2) {
|
|
32
|
-
if (k2 === undefined) k2 = k;
|
|
33
|
-
o[k2] = m[k];
|
|
34
|
-
}));
|
|
35
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
36
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
37
|
-
}) : function(o, v) {
|
|
38
|
-
o["default"] = v;
|
|
39
|
-
});
|
|
40
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
41
|
-
if (mod && mod.__esModule) return mod;
|
|
42
|
-
var result = {};
|
|
43
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
44
|
-
__setModuleDefault(result, mod);
|
|
45
|
-
return result;
|
|
46
|
-
};
|
|
47
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
48
|
-
var t = {};
|
|
49
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
50
|
-
t[p] = s[p];
|
|
51
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
52
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
53
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
54
|
-
t[p[i]] = s[p[i]];
|
|
55
|
-
}
|
|
56
|
-
return t;
|
|
57
|
-
};
|
|
58
|
-
exports.__esModule = true;
|
|
59
|
-
exports.GatsbyImage = void 0;
|
|
60
|
-
/* eslint-disable no-unused-expressions */
|
|
61
|
-
var react_1 = __importStar(require("react"));
|
|
62
|
-
var hooks_1 = require("./hooks");
|
|
63
|
-
var layout_wrapper_1 = require("./layout-wrapper");
|
|
64
|
-
var gatsby_image_server_1 = require("./gatsby-image.server");
|
|
65
|
-
var react_dom_1 = require("react-dom");
|
|
66
|
-
var GatsbyImageHydrator = /** @class */ (function (_super) {
|
|
67
|
-
__extends(GatsbyImageHydrator, _super);
|
|
68
|
-
function GatsbyImageHydrator(props) {
|
|
69
|
-
var _this_1 = _super.call(this, props) || this;
|
|
70
|
-
_this_1.root = (0, react_1.createRef)();
|
|
71
|
-
_this_1.hydrated = { current: false };
|
|
72
|
-
_this_1.forceRender = {
|
|
73
|
-
// In dev we use render not hydrate, to avoid hydration warnings
|
|
74
|
-
current: process.env.NODE_ENV === "development"
|
|
75
|
-
};
|
|
76
|
-
_this_1.lazyHydrator = null;
|
|
77
|
-
_this_1.ref = (0, react_1.createRef)();
|
|
78
|
-
_this_1.state = {
|
|
79
|
-
isLoading: (0, hooks_1.hasNativeLazyLoadSupport)(),
|
|
80
|
-
isLoaded: false
|
|
81
|
-
};
|
|
82
|
-
return _this_1;
|
|
83
|
-
}
|
|
84
|
-
GatsbyImageHydrator.prototype._lazyHydrate = function (props, state) {
|
|
85
|
-
var _this_1 = this;
|
|
86
|
-
var hasSSRHtml = this.root.current.querySelector("[data-gatsby-image-ssr]");
|
|
87
|
-
// On first server hydration do nothing
|
|
88
|
-
if ((0, hooks_1.hasNativeLazyLoadSupport)() && hasSSRHtml && !this.hydrated.current) {
|
|
89
|
-
this.hydrated.current = true;
|
|
90
|
-
return Promise.resolve();
|
|
91
|
-
}
|
|
92
|
-
return Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
|
|
93
|
-
var lazyHydrate = _a.lazyHydrate;
|
|
94
|
-
var cacheKey = JSON.stringify(_this_1.props.image.images);
|
|
95
|
-
_this_1.lazyHydrator = lazyHydrate(__assign({ image: props.image.images, isLoading: state.isLoading || (0, hooks_1.hasImageLoaded)(cacheKey), isLoaded: state.isLoaded || (0, hooks_1.hasImageLoaded)(cacheKey), toggleIsLoaded: function () {
|
|
96
|
-
var _a;
|
|
97
|
-
(_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
98
|
-
_this_1.setState({
|
|
99
|
-
isLoaded: true
|
|
100
|
-
});
|
|
101
|
-
}, ref: _this_1.ref }, props), _this_1.root, _this_1.hydrated, _this_1.forceRender);
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* Choose if setupIntersectionObserver should use the image cache or not.
|
|
106
|
-
*/
|
|
107
|
-
GatsbyImageHydrator.prototype._setupIntersectionObserver = function (useCache) {
|
|
108
|
-
var _this_1 = this;
|
|
109
|
-
if (useCache === void 0) { useCache = true; }
|
|
110
|
-
Promise.resolve().then(function () { return __importStar(require("./intersection-observer")); }).then(function (_a) {
|
|
111
|
-
var createIntersectionObserver = _a.createIntersectionObserver;
|
|
112
|
-
var intersectionObserver = createIntersectionObserver(function () {
|
|
113
|
-
var _a, _b;
|
|
114
|
-
if (_this_1.root.current) {
|
|
115
|
-
var cacheKey = JSON.stringify(_this_1.props.image.images);
|
|
116
|
-
(_b = (_a = _this_1.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
117
|
-
wasCached: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
|
|
118
|
-
});
|
|
119
|
-
_this_1.setState({
|
|
120
|
-
isLoading: true,
|
|
121
|
-
isLoaded: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
if (_this_1.root.current) {
|
|
126
|
-
_this_1.unobserveRef = intersectionObserver(_this_1.root);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
};
|
|
130
|
-
GatsbyImageHydrator.prototype.shouldComponentUpdate = function (nextProps, nextState) {
|
|
131
|
-
var _this_1 = this;
|
|
132
|
-
var hasChanged = false;
|
|
133
|
-
if (!this.state.isLoading && nextState.isLoading && !nextState.isLoaded) {
|
|
134
|
-
// Props have changed between SSR and hydration, so we need to force render instead of hydrate
|
|
135
|
-
this.forceRender.current = true;
|
|
136
|
-
}
|
|
137
|
-
// this check mostly means people do not have the correct ref checks in place, we want to reset some state to suppport loading effects
|
|
138
|
-
if (this.props.image.images !== nextProps.image.images) {
|
|
139
|
-
// reset state, we'll rely on intersection observer to reload
|
|
140
|
-
if (this.unobserveRef) {
|
|
141
|
-
// unregister intersectionObserver
|
|
142
|
-
this.unobserveRef();
|
|
143
|
-
// // on unmount, make sure we cleanup
|
|
144
|
-
if (this.hydrated.current && this.lazyHydrator) {
|
|
145
|
-
(0, react_dom_1.render)(null, this.root.current);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
this.setState({
|
|
149
|
-
isLoading: false,
|
|
150
|
-
isLoaded: false
|
|
151
|
-
}, function () {
|
|
152
|
-
_this_1._setupIntersectionObserver(false);
|
|
153
|
-
});
|
|
154
|
-
hasChanged = true;
|
|
155
|
-
}
|
|
156
|
-
if (this.root.current && !hasChanged) {
|
|
157
|
-
this._lazyHydrate(nextProps, nextState);
|
|
158
|
-
}
|
|
159
|
-
return false;
|
|
160
|
-
};
|
|
161
|
-
GatsbyImageHydrator.prototype.componentDidMount = function () {
|
|
162
|
-
var _a, _b, _c, _d;
|
|
163
|
-
if (this.root.current) {
|
|
164
|
-
var ssrElement_1 = this.root.current.querySelector("[data-gatsby-image-ssr]");
|
|
165
|
-
var cacheKey_1 = JSON.stringify(this.props.image.images);
|
|
166
|
-
// when SSR and native lazyload is supported we'll do nothing ;)
|
|
167
|
-
if ((0, hooks_1.hasNativeLazyLoadSupport)() &&
|
|
168
|
-
ssrElement_1 &&
|
|
169
|
-
(0, hooks_1.gatsbyImageIsInstalled)()) {
|
|
170
|
-
(_b = (_a = this.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, { wasCached: false });
|
|
171
|
-
// When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
|
|
172
|
-
if (ssrElement_1.complete) {
|
|
173
|
-
(_d = (_c = this.props).onLoad) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
174
|
-
(0, hooks_1.storeImageloaded)(cacheKey_1);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
// We need the current class context (this) inside our named onLoad function
|
|
178
|
-
// The named function is necessary to easily remove the listener afterward.
|
|
179
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
180
|
-
var _this_2 = this;
|
|
181
|
-
// add an onLoad to the image
|
|
182
|
-
ssrElement_1.addEventListener("load", function onLoad() {
|
|
183
|
-
var _a, _b;
|
|
184
|
-
ssrElement_1.removeEventListener("load", onLoad);
|
|
185
|
-
(_b = (_a = _this_2.props).onLoad) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
186
|
-
(0, hooks_1.storeImageloaded)(cacheKey_1);
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
// Fallback to custom lazy loading (intersection observer)
|
|
192
|
-
this._setupIntersectionObserver(true);
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
GatsbyImageHydrator.prototype.componentWillUnmount = function () {
|
|
196
|
-
// Cleanup when onmount happens
|
|
197
|
-
if (this.unobserveRef) {
|
|
198
|
-
// unregister intersectionObserver
|
|
199
|
-
this.unobserveRef();
|
|
200
|
-
// on unmount, make sure we cleanup
|
|
201
|
-
if (this.hydrated.current && this.lazyHydrator) {
|
|
202
|
-
this.lazyHydrator();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return;
|
|
206
|
-
};
|
|
207
|
-
GatsbyImageHydrator.prototype.render = function () {
|
|
208
|
-
var Type = this.props.as || "div";
|
|
209
|
-
var _a = this.props.image, width = _a.width, height = _a.height, layout = _a.layout;
|
|
210
|
-
var _b = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _b.style, wClass = _b.className, wrapperProps = __rest(_b, ["style", "className"]);
|
|
211
|
-
var className = this.props.className;
|
|
212
|
-
// preact class
|
|
213
|
-
if (this.props["class"]) {
|
|
214
|
-
className = this.props["class"];
|
|
215
|
-
}
|
|
216
|
-
var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
|
|
217
|
-
return (react_1["default"].createElement(Type, __assign({}, wrapperProps, { style: __assign(__assign(__assign({}, wStyle), this.props.style), { backgroundColor: this.props.backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : ""), ref: this.root, dangerouslySetInnerHTML: {
|
|
218
|
-
__html: sizer
|
|
219
|
-
}, suppressHydrationWarning: true })));
|
|
220
|
-
};
|
|
221
|
-
return GatsbyImageHydrator;
|
|
222
|
-
}(react_1.Component));
|
|
223
|
-
var GatsbyImage = function GatsbyImage(props) {
|
|
224
|
-
if (!props.image) {
|
|
225
|
-
if (process.env.NODE_ENV === "development") {
|
|
226
|
-
console.warn("[gatsby-plugin-image] Missing image prop");
|
|
227
|
-
}
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
if (!(0, hooks_1.gatsbyImageIsInstalled)() && process.env.NODE_ENV === "development") {
|
|
231
|
-
console.warn("[gatsby-plugin-image] You're missing out on some cool performance features. Please add \"gatsby-plugin-image\" to your gatsby-config.js");
|
|
232
|
-
}
|
|
233
|
-
var className = props.className, classSafe = props["class"], backgroundColor = props.backgroundColor, image = props.image;
|
|
234
|
-
var width = image.width, height = image.height, layout = image.layout;
|
|
235
|
-
var propsKey = JSON.stringify([
|
|
236
|
-
width,
|
|
237
|
-
height,
|
|
238
|
-
layout,
|
|
239
|
-
className,
|
|
240
|
-
classSafe,
|
|
241
|
-
backgroundColor,
|
|
242
|
-
]);
|
|
243
|
-
return react_1["default"].createElement(GatsbyImageHydrator, __assign({ key: propsKey }, props));
|
|
244
|
-
};
|
|
245
|
-
exports.GatsbyImage = GatsbyImage;
|
|
246
|
-
exports.GatsbyImage.propTypes = gatsby_image_server_1.propTypes;
|
|
247
|
-
exports.GatsbyImage.displayName = "GatsbyImage";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
29
|
+
if (k2 === undefined) k2 = k;
|
|
30
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
31
|
+
}) : (function(o, m, k, k2) {
|
|
32
|
+
if (k2 === undefined) k2 = k;
|
|
33
|
+
o[k2] = m[k];
|
|
34
|
+
}));
|
|
35
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
36
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
37
|
+
}) : function(o, v) {
|
|
38
|
+
o["default"] = v;
|
|
39
|
+
});
|
|
40
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
41
|
+
if (mod && mod.__esModule) return mod;
|
|
42
|
+
var result = {};
|
|
43
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
44
|
+
__setModuleDefault(result, mod);
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
48
|
+
var t = {};
|
|
49
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
50
|
+
t[p] = s[p];
|
|
51
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
52
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
53
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
54
|
+
t[p[i]] = s[p[i]];
|
|
55
|
+
}
|
|
56
|
+
return t;
|
|
57
|
+
};
|
|
58
|
+
exports.__esModule = true;
|
|
59
|
+
exports.GatsbyImage = void 0;
|
|
60
|
+
/* eslint-disable no-unused-expressions */
|
|
61
|
+
var react_1 = __importStar(require("react"));
|
|
62
|
+
var hooks_1 = require("./hooks");
|
|
63
|
+
var layout_wrapper_1 = require("./layout-wrapper");
|
|
64
|
+
var gatsby_image_server_1 = require("./gatsby-image.server");
|
|
65
|
+
var react_dom_1 = require("react-dom");
|
|
66
|
+
var GatsbyImageHydrator = /** @class */ (function (_super) {
|
|
67
|
+
__extends(GatsbyImageHydrator, _super);
|
|
68
|
+
function GatsbyImageHydrator(props) {
|
|
69
|
+
var _this_1 = _super.call(this, props) || this;
|
|
70
|
+
_this_1.root = (0, react_1.createRef)();
|
|
71
|
+
_this_1.hydrated = { current: false };
|
|
72
|
+
_this_1.forceRender = {
|
|
73
|
+
// In dev we use render not hydrate, to avoid hydration warnings
|
|
74
|
+
current: process.env.NODE_ENV === "development"
|
|
75
|
+
};
|
|
76
|
+
_this_1.lazyHydrator = null;
|
|
77
|
+
_this_1.ref = (0, react_1.createRef)();
|
|
78
|
+
_this_1.state = {
|
|
79
|
+
isLoading: (0, hooks_1.hasNativeLazyLoadSupport)(),
|
|
80
|
+
isLoaded: false
|
|
81
|
+
};
|
|
82
|
+
return _this_1;
|
|
83
|
+
}
|
|
84
|
+
GatsbyImageHydrator.prototype._lazyHydrate = function (props, state) {
|
|
85
|
+
var _this_1 = this;
|
|
86
|
+
var hasSSRHtml = this.root.current.querySelector("[data-gatsby-image-ssr]");
|
|
87
|
+
// On first server hydration do nothing
|
|
88
|
+
if ((0, hooks_1.hasNativeLazyLoadSupport)() && hasSSRHtml && !this.hydrated.current) {
|
|
89
|
+
this.hydrated.current = true;
|
|
90
|
+
return Promise.resolve();
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
|
|
93
|
+
var lazyHydrate = _a.lazyHydrate;
|
|
94
|
+
var cacheKey = JSON.stringify(_this_1.props.image.images);
|
|
95
|
+
_this_1.lazyHydrator = lazyHydrate(__assign({ image: props.image.images, isLoading: state.isLoading || (0, hooks_1.hasImageLoaded)(cacheKey), isLoaded: state.isLoaded || (0, hooks_1.hasImageLoaded)(cacheKey), toggleIsLoaded: function () {
|
|
96
|
+
var _a;
|
|
97
|
+
(_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
98
|
+
_this_1.setState({
|
|
99
|
+
isLoaded: true
|
|
100
|
+
});
|
|
101
|
+
}, ref: _this_1.ref }, props), _this_1.root, _this_1.hydrated, _this_1.forceRender);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Choose if setupIntersectionObserver should use the image cache or not.
|
|
106
|
+
*/
|
|
107
|
+
GatsbyImageHydrator.prototype._setupIntersectionObserver = function (useCache) {
|
|
108
|
+
var _this_1 = this;
|
|
109
|
+
if (useCache === void 0) { useCache = true; }
|
|
110
|
+
Promise.resolve().then(function () { return __importStar(require("./intersection-observer")); }).then(function (_a) {
|
|
111
|
+
var createIntersectionObserver = _a.createIntersectionObserver;
|
|
112
|
+
var intersectionObserver = createIntersectionObserver(function () {
|
|
113
|
+
var _a, _b;
|
|
114
|
+
if (_this_1.root.current) {
|
|
115
|
+
var cacheKey = JSON.stringify(_this_1.props.image.images);
|
|
116
|
+
(_b = (_a = _this_1.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
117
|
+
wasCached: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
|
|
118
|
+
});
|
|
119
|
+
_this_1.setState({
|
|
120
|
+
isLoading: true,
|
|
121
|
+
isLoaded: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
if (_this_1.root.current) {
|
|
126
|
+
_this_1.unobserveRef = intersectionObserver(_this_1.root);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
GatsbyImageHydrator.prototype.shouldComponentUpdate = function (nextProps, nextState) {
|
|
131
|
+
var _this_1 = this;
|
|
132
|
+
var hasChanged = false;
|
|
133
|
+
if (!this.state.isLoading && nextState.isLoading && !nextState.isLoaded) {
|
|
134
|
+
// Props have changed between SSR and hydration, so we need to force render instead of hydrate
|
|
135
|
+
this.forceRender.current = true;
|
|
136
|
+
}
|
|
137
|
+
// this check mostly means people do not have the correct ref checks in place, we want to reset some state to suppport loading effects
|
|
138
|
+
if (this.props.image.images !== nextProps.image.images) {
|
|
139
|
+
// reset state, we'll rely on intersection observer to reload
|
|
140
|
+
if (this.unobserveRef) {
|
|
141
|
+
// unregister intersectionObserver
|
|
142
|
+
this.unobserveRef();
|
|
143
|
+
// // on unmount, make sure we cleanup
|
|
144
|
+
if (this.hydrated.current && this.lazyHydrator) {
|
|
145
|
+
(0, react_dom_1.render)(null, this.root.current);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.setState({
|
|
149
|
+
isLoading: false,
|
|
150
|
+
isLoaded: false
|
|
151
|
+
}, function () {
|
|
152
|
+
_this_1._setupIntersectionObserver(false);
|
|
153
|
+
});
|
|
154
|
+
hasChanged = true;
|
|
155
|
+
}
|
|
156
|
+
if (this.root.current && !hasChanged) {
|
|
157
|
+
this._lazyHydrate(nextProps, nextState);
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
};
|
|
161
|
+
GatsbyImageHydrator.prototype.componentDidMount = function () {
|
|
162
|
+
var _a, _b, _c, _d;
|
|
163
|
+
if (this.root.current) {
|
|
164
|
+
var ssrElement_1 = this.root.current.querySelector("[data-gatsby-image-ssr]");
|
|
165
|
+
var cacheKey_1 = JSON.stringify(this.props.image.images);
|
|
166
|
+
// when SSR and native lazyload is supported we'll do nothing ;)
|
|
167
|
+
if ((0, hooks_1.hasNativeLazyLoadSupport)() &&
|
|
168
|
+
ssrElement_1 &&
|
|
169
|
+
(0, hooks_1.gatsbyImageIsInstalled)()) {
|
|
170
|
+
(_b = (_a = this.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, { wasCached: false });
|
|
171
|
+
// When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
|
|
172
|
+
if (ssrElement_1.complete) {
|
|
173
|
+
(_d = (_c = this.props).onLoad) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
174
|
+
(0, hooks_1.storeImageloaded)(cacheKey_1);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// We need the current class context (this) inside our named onLoad function
|
|
178
|
+
// The named function is necessary to easily remove the listener afterward.
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
180
|
+
var _this_2 = this;
|
|
181
|
+
// add an onLoad to the image
|
|
182
|
+
ssrElement_1.addEventListener("load", function onLoad() {
|
|
183
|
+
var _a, _b;
|
|
184
|
+
ssrElement_1.removeEventListener("load", onLoad);
|
|
185
|
+
(_b = (_a = _this_2.props).onLoad) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
186
|
+
(0, hooks_1.storeImageloaded)(cacheKey_1);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
// Fallback to custom lazy loading (intersection observer)
|
|
192
|
+
this._setupIntersectionObserver(true);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
GatsbyImageHydrator.prototype.componentWillUnmount = function () {
|
|
196
|
+
// Cleanup when onmount happens
|
|
197
|
+
if (this.unobserveRef) {
|
|
198
|
+
// unregister intersectionObserver
|
|
199
|
+
this.unobserveRef();
|
|
200
|
+
// on unmount, make sure we cleanup
|
|
201
|
+
if (this.hydrated.current && this.lazyHydrator) {
|
|
202
|
+
this.lazyHydrator();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
};
|
|
207
|
+
GatsbyImageHydrator.prototype.render = function () {
|
|
208
|
+
var Type = this.props.as || "div";
|
|
209
|
+
var _a = this.props.image, width = _a.width, height = _a.height, layout = _a.layout;
|
|
210
|
+
var _b = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _b.style, wClass = _b.className, wrapperProps = __rest(_b, ["style", "className"]);
|
|
211
|
+
var className = this.props.className;
|
|
212
|
+
// preact class
|
|
213
|
+
if (this.props["class"]) {
|
|
214
|
+
className = this.props["class"];
|
|
215
|
+
}
|
|
216
|
+
var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
|
|
217
|
+
return (react_1["default"].createElement(Type, __assign({}, wrapperProps, { style: __assign(__assign(__assign({}, wStyle), this.props.style), { backgroundColor: this.props.backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : ""), ref: this.root, dangerouslySetInnerHTML: {
|
|
218
|
+
__html: sizer
|
|
219
|
+
}, suppressHydrationWarning: true })));
|
|
220
|
+
};
|
|
221
|
+
return GatsbyImageHydrator;
|
|
222
|
+
}(react_1.Component));
|
|
223
|
+
var GatsbyImage = function GatsbyImage(props) {
|
|
224
|
+
if (!props.image) {
|
|
225
|
+
if (process.env.NODE_ENV === "development") {
|
|
226
|
+
console.warn("[gatsby-plugin-image] Missing image prop");
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
if (!(0, hooks_1.gatsbyImageIsInstalled)() && process.env.NODE_ENV === "development") {
|
|
231
|
+
console.warn("[gatsby-plugin-image] You're missing out on some cool performance features. Please add \"gatsby-plugin-image\" to your gatsby-config.js");
|
|
232
|
+
}
|
|
233
|
+
var className = props.className, classSafe = props["class"], backgroundColor = props.backgroundColor, image = props.image;
|
|
234
|
+
var width = image.width, height = image.height, layout = image.layout;
|
|
235
|
+
var propsKey = JSON.stringify([
|
|
236
|
+
width,
|
|
237
|
+
height,
|
|
238
|
+
layout,
|
|
239
|
+
className,
|
|
240
|
+
classSafe,
|
|
241
|
+
backgroundColor,
|
|
242
|
+
]);
|
|
243
|
+
return react_1["default"].createElement(GatsbyImageHydrator, __assign({ key: propsKey }, props));
|
|
244
|
+
};
|
|
245
|
+
exports.GatsbyImage = GatsbyImage;
|
|
246
|
+
exports.GatsbyImage.propTypes = gatsby_image_server_1.propTypes;
|
|
247
|
+
exports.GatsbyImage.displayName = "GatsbyImage";
|