Skip to content

Commit c6fd998

Browse files
authored
Merge pull request #8 from notiz-dev/enhancements/calculate-image-sizes
calculate image sizes
2 parents 2ebb79d + 29674af commit c6fd998

6 files changed

Lines changed: 143 additions & 82 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ src/assets/scully-routes.json
5151
# firebase
5252
.firebase/
5353
firebase-debug.log
54+
55+
scully.scully-plugins.config.js

package-lock.json

Lines changed: 57 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/lazy-images/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
`scully-plugin-lazy-images` is a `postRenderer` plugin for [Scully](http://scully.io/) turning your images into lazy loading images using [lazyload](https://github.com/tuupola/lazyload). This will replace the `src` attribute with `data-src` and adds the class `lazyload` to the `img` tag.
66

7+
The content around an image changes after the image is **lazily** loaded. To prevent the content to "jump" after loading, the `height` and `width` of the image is calculated and a placeholder `svg` is added to the `src` attribute with the image dimensions. The `svg` placeholder is replaced with the actual image after it is loaded.
8+
79
A browser [native approach](https://web.dev/native-lazy-loading/) would be to use `loading="lazy"` for each `img` tag. When it has broader [browser support](https://caniuse.com/#feat=loading-lazy-attr) we will switch to the native approach.
810

911
## 📦 Installation

plugins/lazy-images/package-lock.json

Lines changed: 51 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/lazy-images/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@notiz/scully-plugin-lazy-images",
3-
"version": "0.2.7",
3+
"version": "0.3.0",
44
"description": "scully plugin to add lazy loading for images",
55
"main": "src/index.js",
66
"author": "notiz.dev",
@@ -32,6 +32,8 @@
3232
"@scullyio/scully": ">=0.0.75"
3333
},
3434
"dependencies": {
35+
"axios": "^0.19.2",
36+
"image-size": "^0.8.3",
3537
"jsdom": "^16.2.1"
3638
}
3739
}

plugins/lazy-images/src/lazy-images.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
const { JSDOM } = require('jsdom');
2+
const { get } = require('axios');
3+
var sizeOf = require('image-size');
4+
const { scullyConfig } = require('@scullyio/scully');
5+
const path = require('path');
26

37
const lazyImagesPlugin = async (html, route) => {
48
const dom = new JSDOM(html);
59
const doc = dom.window.document;
610

7-
makeImageLazyload(doc);
8-
11+
await makeImageLazyload(doc, route);
912
doc.body.append(loadLazyload(doc));
1013
doc.body.append(createLazyImageScript(doc));
1114

1215
return dom.serialize();
1316
};
1417

15-
const makeImageLazyload = (doc) => {
18+
const makeImageLazyload = async (doc, route) => {
1619
var imgEl = doc.getElementsByTagName('img');
1720

1821
// can be added when loading="lazy" is supported in more browsers
1922
// for (var i = 0; i < imgEl.length; i++) {
2023
// imgEl[i].setAttribute('loading', 'lazy');
2124
// }
22-
2325
for (var i = 0; i < imgEl.length; i++) {
24-
if (imgEl[i].getAttribute('src')) {
25-
imgEl[i].setAttribute('data-src', imgEl[i].getAttribute('src'));
26-
imgEl[i].removeAttribute('src');
26+
const src = imgEl[i].getAttribute('src');
27+
let dimensions = { width: 0, height: 0 };
28+
if (src) {
29+
if (src.startsWith('http')) {
30+
try {
31+
const image = await get(src, {
32+
responseType: 'arraybuffer',
33+
});
34+
dimensions = sizeOf(image.data);
35+
imgEl[i].setAttribute('height', dimensions.height);
36+
imgEl[i].setAttribute('width', dimensions.width);
37+
} catch (err) {}
38+
} else {
39+
dimensions = sizeOf(path.join(scullyConfig.outDir, src));
40+
imgEl[i].setAttribute('height', dimensions.height);
41+
imgEl[i].setAttribute('width', dimensions.width);
42+
}
43+
imgEl[i].setAttribute('data-src', src);
44+
imgEl[i].setAttribute(
45+
'src',
46+
`data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ${dimensions.width} ${dimensions.height}'%3E%3C/svg%3E`
47+
);
2748
imgEl[i].classList.add('lazyload');
2849
}
2950
}
3051
};
3152

3253
const loadLazyload = (doc) => {
33-
3454
const lazyload = doc.createElement('script');
3555
lazyload.src = 'https://cdn.jsdelivr.net/npm/lazyload@2.0.0-rc.2/lazyload.js';
3656
return lazyload;

0 commit comments

Comments
 (0)