gatsby-plugin-image 2.13.0 → 2.14.0-alpha-transformer-json.26

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +142 -0
  2. package/dist/components/gatsby-image.browser.js +105 -197
  3. package/dist/components/gatsby-image.server.js +6 -12
  4. package/dist/components/hooks.js +12 -161
  5. package/dist/components/intersection-observer.js +6 -8
  6. package/dist/components/layout-wrapper.js +8 -7
  7. package/dist/components/lazy-hydrate.js +176 -37
  8. package/dist/components/main-image.js +7 -22
  9. package/dist/components/picture.js +15 -8
  10. package/dist/components/placeholder.js +5 -1
  11. package/dist/gatsby-image.browser.js +1 -1
  12. package/dist/gatsby-image.browser.modern.js +1 -1
  13. package/dist/gatsby-image.js +1 -1
  14. package/dist/gatsby-image.js.map +1 -1
  15. package/dist/gatsby-image.module.js +1 -1
  16. package/dist/gatsby-image.module.js.map +1 -1
  17. package/dist/gatsby-node.js +6 -4
  18. package/dist/global.js +2 -0
  19. package/dist/image-utils.js +1 -8
  20. package/dist/index.browser-1e6cf5a9.js +2 -0
  21. package/dist/index.browser-1e6cf5a9.js.map +1 -0
  22. package/dist/index.browser-4760edcd.js +2 -0
  23. package/dist/index.browser-4760edcd.js.map +1 -0
  24. package/dist/index.js +6 -1
  25. package/dist/lazy-hydrate-64ce7b2b.js +2 -0
  26. package/dist/lazy-hydrate-64ce7b2b.js.map +1 -0
  27. package/dist/lazy-hydrate-6766eb3d.js +2 -0
  28. package/dist/lazy-hydrate-6766eb3d.js.map +1 -0
  29. package/dist/node-apis/image-processing.js +2 -4
  30. package/dist/node-apis/preprocess-source.js +0 -1
  31. package/dist/resolver-utils.js +3 -2
  32. package/dist/src/components/gatsby-image.browser.d.ts +9 -7
  33. package/dist/src/components/gatsby-image.browser.d.ts.map +1 -1
  34. package/dist/src/components/gatsby-image.server.d.ts +3 -7
  35. package/dist/src/components/gatsby-image.server.d.ts.map +1 -1
  36. package/dist/src/components/hooks.d.ts +10 -15
  37. package/dist/src/components/hooks.d.ts.map +1 -1
  38. package/dist/src/components/intersection-observer.d.ts +1 -2
  39. package/dist/src/components/intersection-observer.d.ts.map +1 -1
  40. package/dist/src/components/layout-wrapper.d.ts +0 -1
  41. package/dist/src/components/layout-wrapper.d.ts.map +1 -1
  42. package/dist/src/components/lazy-hydrate.d.ts +4 -6
  43. package/dist/src/components/lazy-hydrate.d.ts.map +1 -1
  44. package/dist/src/components/main-image.d.ts +1 -8
  45. package/dist/src/components/main-image.d.ts.map +1 -1
  46. package/dist/src/components/picture.d.ts +1 -6
  47. package/dist/src/components/picture.d.ts.map +1 -1
  48. package/dist/src/gatsby-node.d.ts +1 -1
  49. package/dist/src/gatsby-ssr.d.ts.map +1 -1
  50. package/dist/src/global.d.ts +6 -0
  51. package/dist/src/global.d.ts.map +1 -0
  52. package/dist/src/image-utils.d.ts +1 -1
  53. package/dist/src/image-utils.d.ts.map +1 -1
  54. package/dist/src/index.browser.d.ts +1 -1
  55. package/dist/src/index.browser.d.ts.map +1 -1
  56. package/dist/src/index.d.ts +1 -0
  57. package/dist/src/index.d.ts.map +1 -1
  58. package/dist/src/node-apis/image-processing.d.ts +2 -3
  59. package/dist/src/resolver-utils.d.ts +3 -3
  60. package/gatsby-ssr.js +1 -1
  61. package/package.json +22 -20
  62. package/dist/gatsby-image.browser.module.js +0 -2
  63. package/dist/gatsby-image.browser.module.js.map +0 -1
  64. package/dist/index.browser-4e524ce6.js +0 -2
  65. package/dist/index.browser-4e524ce6.js.map +0 -1
  66. package/dist/index.browser-53efc75d.js +0 -2
  67. package/dist/index.browser-53efc75d.js.map +0 -1
  68. package/dist/index.browser-80ad8918.js +0 -3
  69. package/dist/index.browser-80ad8918.js.map +0 -1
  70. package/dist/intersection-observer-1b2d7fcb.js +0 -2
  71. package/dist/intersection-observer-1b2d7fcb.js.map +0 -1
  72. package/dist/intersection-observer-3d807f12.js +0 -2
  73. package/dist/intersection-observer-3d807f12.js.map +0 -1
  74. package/dist/intersection-observer-6b794dd8.js +0 -2
  75. package/dist/intersection-observer-6b794dd8.js.map +0 -1
  76. package/dist/lazy-hydrate-1b726509.js +0 -2
  77. package/dist/lazy-hydrate-1b726509.js.map +0 -1
  78. package/dist/lazy-hydrate-2d1e8cb4.js +0 -2
  79. package/dist/lazy-hydrate-2d1e8cb4.js.map +0 -1
  80. package/dist/lazy-hydrate-b920df6f.js +0 -2
  81. package/dist/lazy-hydrate-b920df6f.js.map +0 -1
  82. package/dist/src/components/later-hydrator.d.ts +0 -3
  83. package/dist/src/components/later-hydrator.d.ts.map +0 -1
  84. package/gatsby-browser.js +0 -6
package/CHANGELOG.md CHANGED
@@ -3,6 +3,148 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.24.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.24.0/packages/gatsby-plugin-image) (2022-09-27)
7
+
8
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.24)
9
+
10
+ #### Chores
11
+
12
+ - allow react/react-dom@experimental [#36533](https://github.com/gatsbyjs/gatsby/issues/36533) ([7ef4a3f](https://github.com/gatsbyjs/gatsby/commit/7ef4a3fe080d45e9edaff9f1d4deebd12a00ddbd))
13
+
14
+ ### [2.23.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.23.1/packages/gatsby-plugin-image) (2022-09-22)
15
+
16
+ **Note:** Version bump only for package gatsby-plugin-image
17
+
18
+ ## [2.23.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.23.0/packages/gatsby-plugin-image) (2022-09-13)
19
+
20
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.23)
21
+
22
+ #### Features
23
+
24
+ - Client export directive [#36470](https://github.com/gatsbyjs/gatsby/issues/36470) ([320efb7](https://github.com/gatsbyjs/gatsby/commit/320efb77079f7adf4c89b885a7f34e4cc3951906))
25
+
26
+ ## [2.22.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.22.0/packages/gatsby-plugin-image) (2022-08-30)
27
+
28
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.22)
29
+
30
+ #### Bug Fixes
31
+
32
+ - Make onLoad callback work on first load [#36375](https://github.com/gatsbyjs/gatsby/issues/36375) ([3dfc1ec](https://github.com/gatsbyjs/gatsby/commit/3dfc1ec1c03a962bfdcf3db6ec200883d612d3ad))
33
+
34
+ ## [2.21.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.21.0/packages/gatsby-plugin-image) (2022-08-16)
35
+
36
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.21)
37
+
38
+ #### Bug Fixes
39
+
40
+ - Add `outputPixelDensities` to `SHARP_ATTRIBUTES` [#36203](https://github.com/gatsbyjs/gatsby/issues/36203) ([ae226b5](https://github.com/gatsbyjs/gatsby/commit/ae226b589369e3b724cc3b5ded5b7b1b1a907afb))
41
+
42
+ ## [2.20.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.20.0/packages/gatsby-plugin-image) (2022-08-02)
43
+
44
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.20)
45
+
46
+ #### Bug Fixes
47
+
48
+ - Correct type for getImage [#36169](https://github.com/gatsbyjs/gatsby/issues/36169) ([b3f434b](https://github.com/gatsbyjs/gatsby/commit/b3f434b38b9a5cbcbfb6d4afca260b39cdeb76a4))
49
+
50
+ #### Chores
51
+
52
+ - update dependency typescript to ^4.7.4 [#36030](https://github.com/gatsbyjs/gatsby/issues/36030) ([94a3264](https://github.com/gatsbyjs/gatsby/commit/94a32647a8c45de620d2efe99310805910586c8a))
53
+
54
+ ## [2.19.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.19.0/packages/gatsby-plugin-image) (2022-07-19)
55
+
56
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.19)
57
+
58
+ #### Features
59
+
60
+ - Publish some packages as ESM & CJS [#36012](https://github.com/gatsbyjs/gatsby/issues/36012) ([5bd3063](https://github.com/gatsbyjs/gatsby/commit/5bd3063a1e72c6f98447bfac2bf767cca781330b))
61
+
62
+ ### [2.18.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.18.1/packages/gatsby-plugin-image) (2022-07-12)
63
+
64
+ **Note:** Version bump only for package gatsby-plugin-image
65
+
66
+ ## [2.18.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.18.0/packages/gatsby-plugin-image) (2022-07-05)
67
+
68
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.18)
69
+
70
+ #### Bug Fixes
71
+
72
+ - update dependency chokidar to ^3.5.3 [#34643](https://github.com/gatsbyjs/gatsby/issues/34643) ([9b308cb](https://github.com/gatsbyjs/gatsby/commit/9b308cb744047beeea3a5b2f500fbf5a8b8ef2d5))
73
+
74
+ ## [2.17.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.17.0/packages/gatsby-plugin-image) (2022-06-21)
75
+
76
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.17)
77
+
78
+ #### Bug Fixes
79
+
80
+ - Handle immediate unmount [#35878](https://github.com/gatsbyjs/gatsby/issues/35878) ([fbdec7e](https://github.com/gatsbyjs/gatsby/commit/fbdec7e29b14528972bb6882c4f012aac3953b76))
81
+
82
+ ### [2.16.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.16.1/packages/gatsby-plugin-image) (2022-06-08)
83
+
84
+ #### Bug Fixes
85
+
86
+ - Handle immediate unmount [#35878](https://github.com/gatsbyjs/gatsby/issues/35878) [#35883](https://github.com/gatsbyjs/gatsby/issues/35883) ([e2cf510](https://github.com/gatsbyjs/gatsby/commit/e2cf51067122885126cabcc55816e1bcb6111df5))
87
+
88
+ ## [2.16.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.16.0/packages/gatsby-plugin-image) (2022-06-07)
89
+
90
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.16)
91
+
92
+ #### Bug Fixes
93
+
94
+ - Update `createRemoteFileNode` args [#35422](https://github.com/gatsbyjs/gatsby/issues/35422) fixes [#35363](https://github.com/gatsbyjs/gatsby/issues/35363) ([caaf7a5](https://github.com/gatsbyjs/gatsby/commit/caaf7a555bb42043044f05743eec0ac1ad372cc4))
95
+
96
+ #### Chores
97
+
98
+ - update dependency typescript to ^4.7.2 [#35808](https://github.com/gatsbyjs/gatsby/issues/35808) ([2c55b79](https://github.com/gatsbyjs/gatsby/commit/2c55b794dd95b40a994f56df5f912219771ccab4))
99
+
100
+ ### [2.15.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.15.1/packages/gatsby-plugin-image) (2022-06-01)
101
+
102
+ **Note:** Version bump only for package gatsby-plugin-image
103
+
104
+ ## [2.15.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.15.0/packages/gatsby-plugin-image) (2022-05-24)
105
+
106
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.15)
107
+
108
+ #### Bug Fixes
109
+
110
+ - Throw Typegen errors & add `IGatsbyImageData` to output [#35683](https://github.com/gatsbyjs/gatsby/issues/35683) ([e7fc88b](https://github.com/gatsbyjs/gatsby/commit/e7fc88b024c84a3e9f732deec7441621024e1f95))
111
+ - fix race condition fix [#35635](https://github.com/gatsbyjs/gatsby/issues/35635) ([12cb033](https://github.com/gatsbyjs/gatsby/commit/12cb033f7998427cfbabe8d833a7c2bd516f3b1f))
112
+
113
+ #### Chores
114
+
115
+ - don't use npm-run-all to build packages ([fb4de37](https://github.com/gatsbyjs/gatsby/commit/fb4de379e914892c8ce437d4613b60499cfeb897))
116
+ - workaround lerna + npm-run-all weirdness [#35595](https://github.com/gatsbyjs/gatsby/issues/35595) ([8cc0970](https://github.com/gatsbyjs/gatsby/commit/8cc09702f9a2767c51cf5267233108c07e4ac073))
117
+
118
+ ### [2.14.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.14.1/packages/gatsby-plugin-image) (2022-05-12)
119
+
120
+ #### Bug Fixes
121
+
122
+ - fix race condition fix [#35635](https://github.com/gatsbyjs/gatsby/issues/35635) fix [#35640](https://github.com/gatsbyjs/gatsby/issues/35640) ([5566d44](https://github.com/gatsbyjs/gatsby/commit/5566d448edd4f82ba13edda0af51e8aa0362e367))
123
+
124
+ ## [2.14.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.14.0/packages/gatsby-plugin-image) (2022-05-10)
125
+
126
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.14)
127
+
128
+ #### Features
129
+
130
+ - add check for node.gatsbyImage in the getImage helper [#35507](https://github.com/gatsbyjs/gatsby/issues/35507) ([abc6dca](https://github.com/gatsbyjs/gatsby/commit/abc6dca7a24bd1fcf8946ce23e09d50c914ac589))
131
+
132
+ #### Bug Fixes
133
+
134
+ - fix image flickers fix [#35226](https://github.com/gatsbyjs/gatsby/issues/35226) ([9a91295](https://github.com/gatsbyjs/gatsby/commit/9a912956a5ea66be7281f6263928b64f20ff1d53))
135
+ - update dependency fs-extra to ^10.1.0 [#34976](https://github.com/gatsbyjs/gatsby/issues/34976) ([10752ed](https://github.com/gatsbyjs/gatsby/commit/10752ed325ac0ebc2655b994862f66abe072e09f))
136
+ - update dependency semver to ^7.3.7 [#35543](https://github.com/gatsbyjs/gatsby/issues/35543) ([f593e00](https://github.com/gatsbyjs/gatsby/commit/f593e005e4561b423a297e967205d833451a2f55))
137
+
138
+ #### Chores
139
+
140
+ - update dependency typescript to ^4.6.4 [#34984](https://github.com/gatsbyjs/gatsby/issues/34984) ([71eb414](https://github.com/gatsbyjs/gatsby/commit/71eb414ad5abf1c835a2c243f01ac98ea5ee7e37))
141
+
142
+ ## [2.13.0](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.13.0/packages/gatsby-plugin-image) (2022-04-26)
143
+
144
+ [🧾 Release notes](https://www.gatsbyjs.com/docs/reference/release-notes/v4.13)
145
+
146
+ **Note:** Version bump only for package gatsby-plugin-image
147
+
6
148
  ### [2.12.1](https://github.com/gatsbyjs/gatsby/commits/gatsby-plugin-image@2.12.1/packages/gatsby-plugin-image) (2022-04-13)
7
149
 
8
150
  **Note:** Version bump only for package gatsby-plugin-image
@@ -1,19 +1,4 @@
1
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
2
  var __assign = (this && this.__assign) || function () {
18
3
  __assign = Object.assign || function(t) {
19
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -27,7 +12,11 @@ var __assign = (this && this.__assign) || function () {
27
12
  };
28
13
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
14
  if (k2 === undefined) k2 = k;
30
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
31
20
  }) : (function(o, m, k, k2) {
32
21
  if (k2 === undefined) k2 = k;
33
22
  o[k2] = m[k];
@@ -57,188 +46,118 @@ var __rest = (this && this.__rest) || function (s, e) {
57
46
  };
58
47
  exports.__esModule = true;
59
48
  exports.GatsbyImage = void 0;
60
- /* global HAS_REACT_18 */
61
- /* eslint-disable no-unused-expressions */
62
- var react_1 = __importStar(require("react"));
49
+ var react_1 = require("react");
63
50
  var hooks_1 = require("./hooks");
64
51
  var layout_wrapper_1 = require("./layout-wrapper");
65
52
  var gatsby_image_server_1 = require("./gatsby-image.server");
66
- var reactRender;
67
- if (HAS_REACT_18) {
68
- var reactDomClient_1 = require("react-dom/client");
69
- reactRender = function (Component, el, root) {
70
- if (!root) {
71
- root = reactDomClient_1.createRoot(el);
72
- }
73
- root.render(Component);
74
- return root;
75
- };
76
- }
77
- else {
78
- var reactDomClient_2 = require("react-dom");
79
- reactRender = function (Component, el) {
80
- reactDomClient_2.render(Component, el);
81
- };
82
- }
83
- var GatsbyImageHydrator = /** @class */ (function (_super) {
84
- __extends(GatsbyImageHydrator, _super);
85
- function GatsbyImageHydrator(props) {
86
- var _this_1 = _super.call(this, props) || this;
87
- _this_1.root = (0, react_1.createRef)();
88
- _this_1.hydrated = { current: false };
89
- _this_1.forceRender = {
90
- // In dev we use render not hydrate, to avoid hydration warnings
91
- current: process.env.NODE_ENV === "development"
92
- };
93
- _this_1.lazyHydrator = null;
94
- _this_1.ref = (0, react_1.createRef)();
95
- _this_1.reactRootRef = (0, react_1.createRef)();
96
- _this_1.state = {
97
- isLoading: (0, hooks_1.hasNativeLazyLoadSupport)(),
98
- isLoaded: false
99
- };
100
- return _this_1;
53
+ var imageCache = new Set();
54
+ var renderImageToStringPromise;
55
+ var renderImage;
56
+ var GatsbyImageHydrator = function GatsbyImageHydrator(_a) {
57
+ var _b = _a.as, as = _b === void 0 ? "div" : _b, image = _a.image, style = _a.style, backgroundColor = _a.backgroundColor, className = _a.className, preactClass = _a["class"], onStartLoad = _a.onStartLoad, onLoad = _a.onLoad, onError = _a.onError, props = __rest(_a, ["as", "image", "style", "backgroundColor", "className", "class", "onStartLoad", "onLoad", "onError"]);
58
+ var width = image.width, height = image.height, layout = image.layout;
59
+ var _c = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _c.style, wClass = _c.className, wrapperProps = __rest(_c, ["style", "className"]);
60
+ var root = (0, react_1.useRef)();
61
+ var cacheKey = (0, react_1.useMemo)(function () { return JSON.stringify(image.images); }, [image.images]);
62
+ // Preact uses class instead of className so we need to check for both
63
+ if (preactClass) {
64
+ className = preactClass;
101
65
  }
102
- GatsbyImageHydrator.prototype._lazyHydrate = function (props, state) {
103
- var _this_1 = this;
104
- var hasSSRHtml = this.root.current.querySelector("[data-gatsby-image-ssr]");
105
- // On first server hydration do nothing
106
- if ((0, hooks_1.hasNativeLazyLoadSupport)() && hasSSRHtml && !this.hydrated.current) {
107
- this.hydrated.current = true;
108
- return Promise.resolve();
66
+ var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
67
+ (0, react_1.useEffect)(function () {
68
+ if (!renderImageToStringPromise) {
69
+ renderImageToStringPromise = Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
70
+ var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
71
+ renderImage = renderImageToString;
72
+ return {
73
+ renderImageToString: renderImageToString,
74
+ swapPlaceholderImage: swapPlaceholderImage
75
+ };
76
+ });
109
77
  }
110
- return Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
111
- var lazyHydrate = _a.lazyHydrate;
112
- var cacheKey = JSON.stringify(_this_1.props.image.images);
113
- _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 () {
114
- var _a;
115
- (_a = props.onLoad) === null || _a === void 0 ? void 0 : _a.call(props);
116
- _this_1.setState({
117
- isLoaded: true
118
- });
119
- }, ref: _this_1.ref }, props), _this_1.root, _this_1.hydrated, _this_1.forceRender, _this_1.reactRootRef);
120
- });
121
- };
122
- /**
123
- * Choose if setupIntersectionObserver should use the image cache or not.
124
- */
125
- GatsbyImageHydrator.prototype._setupIntersectionObserver = function (useCache) {
126
- var _this_1 = this;
127
- if (useCache === void 0) { useCache = true; }
128
- Promise.resolve().then(function () { return __importStar(require("./intersection-observer")); }).then(function (_a) {
129
- var createIntersectionObserver = _a.createIntersectionObserver;
130
- var intersectionObserver = createIntersectionObserver(function () {
131
- var _a, _b;
132
- if (_this_1.root.current) {
133
- var cacheKey = JSON.stringify(_this_1.props.image.images);
134
- (_b = (_a = _this_1.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, {
135
- wasCached: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
78
+ // The plugin image component is a bit special where if it's server-side rendered, we add extra script tags to support lazy-loading without
79
+ // In this case we stop hydration but fire the correct events.
80
+ var ssrImage = root.current.querySelector("[data-gatsby-image-ssr]");
81
+ if (ssrImage && (0, hooks_1.hasNativeLazyLoadSupport)()) {
82
+ if (ssrImage.complete) {
83
+ // Trigger onStartload and onLoad events
84
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
85
+ wasCached: true
86
+ });
87
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
88
+ wasCached: true
89
+ });
90
+ // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
91
+ setTimeout(function () {
92
+ ssrImage.removeAttribute("data-gatsby-image-ssr");
93
+ }, 0);
94
+ }
95
+ else {
96
+ document.addEventListener("load", function onLoadListener() {
97
+ document.removeEventListener("load", onLoadListener);
98
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
99
+ wasCached: true
136
100
  });
137
- _this_1.setState({
138
- isLoading: true,
139
- isLoaded: useCache && (0, hooks_1.hasImageLoaded)(cacheKey)
101
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
102
+ wasCached: true
140
103
  });
141
- }
142
- });
143
- if (_this_1.root.current) {
144
- _this_1.unobserveRef = intersectionObserver(_this_1.root);
104
+ // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
105
+ setTimeout(function () {
106
+ ssrImage.removeAttribute("data-gatsby-image-ssr");
107
+ }, 0);
108
+ });
145
109
  }
146
- });
147
- };
148
- GatsbyImageHydrator.prototype.shouldComponentUpdate = function (nextProps, nextState) {
149
- var _this_1 = this;
150
- var hasChanged = false;
151
- if (!this.state.isLoading && nextState.isLoading && !nextState.isLoaded) {
152
- // Props have changed between SSR and hydration, so we need to force render instead of hydrate
153
- this.forceRender.current = true;
110
+ imageCache.add(cacheKey);
111
+ return;
154
112
  }
155
- // this check mostly means people do not have the correct ref checks in place, we want to reset some state to suppport loading effects
156
- if (this.props.image.images !== nextProps.image.images) {
157
- // reset state, we'll rely on intersection observer to reload
158
- if (this.unobserveRef) {
159
- // unregister intersectionObserver
160
- this.unobserveRef();
161
- // // on unmount, make sure we cleanup
162
- if (this.hydrated.current && this.lazyHydrator) {
163
- this.reactRootRef.current = reactRender(null, this.root.current, this.reactRootRef.current);
164
- }
165
- }
166
- this.setState({
167
- isLoading: false,
168
- isLoaded: false
169
- }, function () {
170
- _this_1._setupIntersectionObserver(false);
171
- });
172
- hasChanged = true;
113
+ if (renderImage && imageCache.has(cacheKey)) {
114
+ return;
173
115
  }
174
- if (this.root.current && !hasChanged) {
175
- this._lazyHydrate(nextProps, nextState);
176
- }
177
- return false;
178
- };
179
- GatsbyImageHydrator.prototype.componentDidMount = function () {
180
- var _a, _b, _c, _d;
181
- if (this.root.current) {
182
- var ssrElement_1 = this.root.current.querySelector("[data-gatsby-image-ssr]");
183
- var cacheKey_1 = JSON.stringify(this.props.image.images);
184
- // when SSR and native lazyload is supported we'll do nothing ;)
185
- if ((0, hooks_1.hasNativeLazyLoadSupport)() &&
186
- ssrElement_1 &&
187
- (0, hooks_1.gatsbyImageIsInstalled)()) {
188
- (_b = (_a = this.props).onStartLoad) === null || _b === void 0 ? void 0 : _b.call(_a, { wasCached: false });
189
- // When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
190
- if (ssrElement_1.complete) {
191
- (_d = (_c = this.props).onLoad) === null || _d === void 0 ? void 0 : _d.call(_c);
192
- (0, hooks_1.storeImageloaded)(cacheKey_1);
193
- }
194
- else {
195
- // We need the current class context (this) inside our named onLoad function
196
- // The named function is necessary to easily remove the listener afterward.
197
- // eslint-disable-next-line @typescript-eslint/no-this-alias
198
- var _this_2 = this;
199
- // add an onLoad to the image
200
- ssrElement_1.addEventListener("load", function onLoad() {
201
- var _a, _b;
202
- ssrElement_1.removeEventListener("load", onLoad);
203
- (_b = (_a = _this_2.props).onLoad) === null || _b === void 0 ? void 0 : _b.call(_a);
204
- (0, hooks_1.storeImageloaded)(cacheKey_1);
205
- });
206
- }
116
+ var animationFrame;
117
+ var cleanupCallback;
118
+ renderImageToStringPromise.then(function (_a) {
119
+ var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
120
+ if (!root.current) {
207
121
  return;
208
122
  }
209
- // Fallback to custom lazy loading (intersection observer)
210
- this._setupIntersectionObserver(true);
211
- }
212
- };
213
- GatsbyImageHydrator.prototype.componentWillUnmount = function () {
214
- // Cleanup when onmount happens
215
- if (this.unobserveRef) {
216
- // unregister intersectionObserver
217
- this.unobserveRef();
218
- // on unmount, make sure we cleanup
219
- if (this.hydrated.current && this.lazyHydrator) {
220
- this.lazyHydrator();
123
+ root.current.innerHTML = renderImageToString(__assign({ isLoading: true, isLoaded: imageCache.has(cacheKey), image: image }, props));
124
+ if (!imageCache.has(cacheKey)) {
125
+ animationFrame = requestAnimationFrame(function () {
126
+ if (root.current) {
127
+ cleanupCallback = swapPlaceholderImage(root.current, cacheKey, imageCache, style, onStartLoad, onLoad, onError);
128
+ }
129
+ });
221
130
  }
131
+ });
132
+ // eslint-disable-next-line consistent-return
133
+ return function () {
134
+ if (animationFrame) {
135
+ cancelAnimationFrame(animationFrame);
136
+ }
137
+ if (cleanupCallback) {
138
+ cleanupCallback();
139
+ }
140
+ };
141
+ }, [image]);
142
+ // useLayoutEffect is ran before React commits to the DOM. This allows us to make sure our HTML is using our cached image version
143
+ (0, react_1.useLayoutEffect)(function () {
144
+ if (imageCache.has(cacheKey) && renderImage) {
145
+ root.current.innerHTML = renderImage(__assign({ isLoading: imageCache.has(cacheKey), isLoaded: imageCache.has(cacheKey), image: image }, props));
146
+ // Trigger onStartload and onLoad events
147
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
148
+ wasCached: true
149
+ });
150
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
151
+ wasCached: true
152
+ });
222
153
  }
223
- return;
224
- };
225
- GatsbyImageHydrator.prototype.render = function () {
226
- var Type = this.props.as || "div";
227
- var _a = this.props.image, width = _a.width, height = _a.height, layout = _a.layout;
228
- var _b = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _b.style, wClass = _b.className, wrapperProps = __rest(_b, ["style", "className"]);
229
- var className = this.props.className;
230
- // preact class
231
- if (this.props["class"]) {
232
- className = this.props["class"];
233
- }
234
- var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
235
- 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: {
236
- __html: sizer
237
- }, suppressHydrationWarning: true })));
238
- };
239
- return GatsbyImageHydrator;
240
- }(react_1.Component));
241
- var GatsbyImage = function GatsbyImage(props) {
154
+ }, [image]);
155
+ // By keeping all props equal React will keep the component in the DOM
156
+ return (0, react_1.createElement)(as, __assign(__assign({}, wrapperProps), { style: __assign(__assign(__assign({}, wStyle), style), { backgroundColor: backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : ""), ref: root, dangerouslySetInnerHTML: {
157
+ __html: sizer
158
+ }, suppressHydrationWarning: true }));
159
+ };
160
+ exports.GatsbyImage = (0, react_1.memo)(function GatsbyImage(props) {
242
161
  if (!props.image) {
243
162
  if (process.env.NODE_ENV === "development") {
244
163
  console.warn("[gatsby-plugin-image] Missing image prop");
@@ -248,18 +167,7 @@ var GatsbyImage = function GatsbyImage(props) {
248
167
  if (!(0, hooks_1.gatsbyImageIsInstalled)() && process.env.NODE_ENV === "development") {
249
168
  console.warn("[gatsby-plugin-image] You're missing out on some cool performance features. Please add \"gatsby-plugin-image\" to your gatsby-config.js");
250
169
  }
251
- var className = props.className, classSafe = props["class"], backgroundColor = props.backgroundColor, image = props.image;
252
- var width = image.width, height = image.height, layout = image.layout;
253
- var propsKey = JSON.stringify([
254
- width,
255
- height,
256
- layout,
257
- className,
258
- classSafe,
259
- backgroundColor,
260
- ]);
261
- return react_1["default"].createElement(GatsbyImageHydrator, __assign({ key: propsKey }, props));
262
- };
263
- exports.GatsbyImage = GatsbyImage;
170
+ return (0, react_1.createElement)(GatsbyImageHydrator, props);
171
+ });
264
172
  exports.GatsbyImage.propTypes = gatsby_image_server_1.propTypes;
265
173
  exports.GatsbyImage.displayName = "GatsbyImage";
@@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  exports.__esModule = true;
53
- exports.propTypes = exports.altValidator = exports.GatsbyImage = exports.GatsbyImageHydrator = void 0;
53
+ exports.propTypes = exports.altValidator = exports.GatsbyImage = void 0;
54
54
  var react_1 = __importDefault(require("react"));
55
55
  var hooks_1 = require("./hooks");
56
56
  var placeholder_1 = require("./placeholder");
@@ -58,13 +58,8 @@ var main_image_1 = require("./main-image");
58
58
  var layout_wrapper_1 = require("./layout-wrapper");
59
59
  var prop_types_1 = __importDefault(require("prop-types"));
60
60
  var removeNewLines = function (str) { return str.replace(/\n/g, ""); };
61
- var GatsbyImageHydrator = function GatsbyImageHydrator(_a) {
62
- var _b = _a.as, Type = _b === void 0 ? "div" : _b, children = _a.children, props = __rest(_a, ["as", "children"]);
63
- return react_1["default"].createElement(Type, __assign({}, props), children);
64
- };
65
- exports.GatsbyImageHydrator = GatsbyImageHydrator;
66
61
  var GatsbyImage = function GatsbyImage(_a) {
67
- var as = _a.as, className = _a.className, preactClass = _a["class"], style = _a.style, image = _a.image, _b = _a.loading, loading = _b === void 0 ? "lazy" : _b, imgClassName = _a.imgClassName, imgStyle = _a.imgStyle, backgroundColor = _a.backgroundColor, objectFit = _a.objectFit, objectPosition = _a.objectPosition, props = __rest(_a, ["as", "className", "class", "style", "image", "loading", "imgClassName", "imgStyle", "backgroundColor", "objectFit", "objectPosition"]);
62
+ var _b = _a.as, as = _b === void 0 ? "div" : _b, className = _a.className, preactClass = _a["class"], style = _a.style, image = _a.image, _c = _a.loading, loading = _c === void 0 ? "lazy" : _c, imgClassName = _a.imgClassName, imgStyle = _a.imgStyle, backgroundColor = _a.backgroundColor, objectFit = _a.objectFit, objectPosition = _a.objectPosition, props = __rest(_a, ["as", "className", "class", "style", "image", "loading", "imgClassName", "imgStyle", "backgroundColor", "objectFit", "objectPosition"]);
68
63
  if (!image) {
69
64
  console.warn("[gatsby-plugin-image] Missing image prop");
70
65
  return null;
@@ -74,7 +69,7 @@ var GatsbyImage = function GatsbyImage(_a) {
74
69
  }
75
70
  imgStyle = __assign({ objectFit: objectFit, objectPosition: objectPosition, backgroundColor: backgroundColor }, imgStyle);
76
71
  var width = image.width, height = image.height, layout = image.layout, images = image.images, placeholder = image.placeholder, placeholderBackgroundColor = image.backgroundColor;
77
- var _c = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _c.style, wClass = _c.className, wrapperProps = __rest(_c, ["style", "className"]);
72
+ var _d = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _d.style, wClass = _d.className, wrapperProps = __rest(_d, ["style", "className"]);
78
73
  var cleanedImages = {
79
74
  fallback: undefined,
80
75
  sources: []
@@ -89,10 +84,9 @@ var GatsbyImage = function GatsbyImage(_a) {
89
84
  return __assign(__assign({}, source), { srcSet: removeNewLines(source.srcSet) });
90
85
  });
91
86
  }
92
- return (react_1["default"].createElement(exports.GatsbyImageHydrator, __assign({}, wrapperProps, { as: as, style: __assign(__assign(__assign({}, wStyle), style), { backgroundColor: backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : "") }),
93
- react_1["default"].createElement(layout_wrapper_1.LayoutWrapper, { layout: layout, width: width, height: height },
94
- react_1["default"].createElement(placeholder_1.Placeholder, __assign({}, (0, hooks_1.getPlaceholderProps)(placeholder, false, layout, width, height, placeholderBackgroundColor, objectFit, objectPosition))),
95
- react_1["default"].createElement(main_image_1.MainImage, __assign({ "data-gatsby-image-ssr": "", className: imgClassName }, props, (0, hooks_1.getMainProps)(loading === "eager", false, cleanedImages, loading, undefined, undefined, undefined, imgStyle))))));
87
+ return react_1["default"].createElement(as, __assign(__assign({}, wrapperProps), { style: __assign(__assign(__assign({}, wStyle), style), { backgroundColor: backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : "") }), react_1["default"].createElement(layout_wrapper_1.LayoutWrapper, { layout: layout, width: width, height: height },
88
+ react_1["default"].createElement(placeholder_1.Placeholder, __assign({}, (0, hooks_1.getPlaceholderProps)(placeholder, false, layout, width, height, placeholderBackgroundColor, objectFit, objectPosition))),
89
+ react_1["default"].createElement(main_image_1.MainImage, __assign({ "data-gatsby-image-ssr": "", className: imgClassName }, props, (0, hooks_1.getMainProps)(loading === "eager", false, cleanedImages, loading, imgStyle)))));
96
90
  };
97
91
  exports.GatsbyImage = GatsbyImage;
98
92
  var altValidator = function (props, propName, componentName) {