Skip to content

Commit ec9b628

Browse files
committed
fix: temporarily auto-patch the lazysizes JS to allow the internal element selector to be specified if a new getElements method is defined in the lazysizes config + update yarn.lock
1 parent 87adbcb commit ec9b628

File tree

6 files changed

+2978
-2867
lines changed

6 files changed

+2978
-2867
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"packages/*",
2828
"packages/*/*",
2929
"packages/build-tools/plugins/*"
30+
],
31+
"nohoist": [
32+
"lazysizes",
33+
"**/lazysizes"
3034
]
3135
},
3236
"scripts": {

packages/components/bolt-image/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@
3030
"access": "public",
3131
"tag": "next"
3232
},
33+
"scripts": {
34+
"postinstall": "patch-package"
35+
},
3336
"dependencies": {
3437
"@bolt/components-ratio": "^2.5.2",
3538
"@bolt/core": "^2.5.2",
36-
"lazysizes": "^4.1.4"
39+
"lazysizes": "^4.1.4",
40+
"patch-package": "^6.1.2",
41+
"postinstall-postinstall": "^2.0.0"
3742
},
3843
"style": "index.scss",
3944
"main": "index.js",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
diff --git a/node_modules/lazysizes/lazysizes.js b/node_modules/lazysizes/lazysizes.js
2+
index 26ba477..076c71e 100644
3+
--- a/node_modules/lazysizes/lazysizes.js
4+
+++ b/node_modules/lazysizes/lazysizes.js
5+
@@ -97,7 +97,12 @@
6+
};
7+
8+
var getCSS = function (elem, style){
9+
- return (getComputedStyle(elem, null) || {})[style];
10+
+ try {
11+
+ return (getComputedStyle(elem, null) || {})[style];
12+
+ }
13+
+ catch {
14+
+ return {}[style];
15+
+ }
16+
};
17+
18+
var getWidth = function(elem, parent, width){
19+
@@ -586,8 +591,21 @@
20+
_: function(){
21+
started = Date.now();
22+
23+
- lazysizes.elements = document.getElementsByClassName(lazySizesConfig.lazyClass);
24+
- preloadElems = document.getElementsByClassName(lazySizesConfig.lazyClass + ' ' + lazySizesConfig.preloadClass);
25+
+ lazysizes.elements = lazySizesConfig.getElements
26+
+ ? lazySizesConfig.getElements(`.${lazySizesConfig.lazyClass}`)
27+
+ : document.getElementsByClassName(lazySizesConfig.lazyClass);
28+
+
29+
+ preloadElems = lazySizesConfig.getElements
30+
+ ? lazySizesConfig.getElements(
31+
+ `.${lazySizesConfig.lazyClass}.${
32+
+ lazySizesConfig.preloadClass
33+
+ }`,
34+
+ )
35+
+ : document.getElementsByClassName(
36+
+ lazySizesConfig.lazyClass +
37+
+ ' ' +
38+
+ lazySizesConfig.preloadClass,
39+
+ );
40+
41+
addEventListener('scroll', throttledCheckElements, true);
42+

packages/components/bolt-image/src/_image-lazy-sizes.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import lazySizes from 'lazysizes'; // Mostly just for automatic `size` attribute support
1+
import lazySizes from 'lazysizes/lazysizes.js'; // Mostly just for automatic `size` attribute support
22
// import 'lazysizes/src/lazysizes-intersection';
33
import 'lazysizes/plugins/unveilhooks/ls.unveilhooks';
44
import 'lazysizes/plugins/progressive/ls.progressive';
@@ -8,8 +8,18 @@ import 'lazysizes/plugins/respimg/ls.respimg'; // Lighter weight version of pict
88
// lazySizes.cfg == window.lazySizesConfig
99
Object.assign(lazySizes.cfg, {
1010
lazyClass: 'js-lazyload',
11+
preloadClass: 'js-lazypreload',
1112
loadingClass: 'is-lazyloading',
1213
loadedClass: 'is-lazyloaded',
1314
preloadAfterLoad: true,
1415
// preloadAfterLoad: false,
16+
getElements(selector) {
17+
let elements = Array.from(document.querySelectorAll('bolt-image')).map(
18+
elem => elem.renderRoot.querySelector(selector),
19+
);
20+
elements = elements.filter(function(el) {
21+
return el != null;
22+
});
23+
return elements;
24+
},
1525
});

packages/components/bolt-image/src/image.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class BoltImage extends withLitHtml() {
6767

6868
lazyloadImage(image) {
6969
if (!this.isLoaded) {
70-
// Note: This immediately unveils the image. Add intersection observer?
71-
lazySizes.loader.unveil(image);
7270
this.isLoaded = true;
7371
}
7472
}

0 commit comments

Comments
 (0)