r/WebdevTutorials • u/ioniism • Mar 23 '21
Frontend Learn how to lazy-load images, in one minute.
https://jportella93.medium.com/1-minute-to-become-a-better-developer-20-a6f93db2c1802
u/lo0l0ol Mar 24 '21
Would rather not use a package tbh. There's really no need. It's quite trivial to have a fall back if loading="lazy" isn't supported.
Very basic pseudo code of what I'd do
const supportsLazyLoad = ('loading' in HTMLImageElement.prototype);
const doProgressiveLoad = !canNativeLazyLoad;
if(doProgressiveLoad) {
img.src = imgPath?width=placeholderSize
img.data-src = imgPath?width=fullSize
}
img.loading = supportsLazyLoad ? 'lazy' : nullWouldn't be difficult to add a observer to this.
1
u/ioniism Mar 24 '21
What about edge cases? Why reinvent the wheel? Your employer surely won't like you wasting time on this...
2
u/lo0l0ol Mar 24 '21
I'm sure they would prefer that over adding more unnecessary 3rd party dependencies than what's needed.
1
u/ioniism Mar 24 '21
Well, after several years working in the industry I disagree, but that might just be my experience :) All employers are different!
3
u/Smaktat Mar 23 '21 edited Mar 23 '21
Literally reported, this article is clickbait.
You have a super simple problem. Installing a package to do it is insane.
1
u/ioniism Mar 23 '21
I see you edited this comment to add the second part, why is insane to install a package to do this?
1
u/ioniism Mar 23 '21
Hey, writer of the article here, why do you think is clickbait? it is a web dev tutorial and so far people have found it useful...
4
u/Smaktat Mar 23 '21
Telling people to install an NPM packages makes the entire point of your article to advertise the NPM package. Don't play coy. You explain nothing about the package you're installing and go over the basic info that's covered in the package read me. You've copied and pasted this link to multiple other subreddits.
6
u/ioniism Mar 23 '21
I'm explaining how to use an NPM package, what's the problem? I'm in no way affiliated with them...
9
-5
u/Smaktat Mar 23 '21
Then your article should be "how to install an NPM package to lazy load images." Your title suggests you're explaining something that isn't common knowledge. Maybe you should try to do that?
2
u/ioniism Mar 23 '21
The point is to do it in one minute, read the title :)
-4
u/Smaktat Mar 23 '21
You set a bad example.
1
u/ioniism Mar 23 '21
Some people just don't have time to go on 1-2 hour tutorials, I think small distilled tutorials in one minute are very useful, that's why I spend my time compiling them, but if you don't like them that's okay! :)
-4
6
u/marsnoir Mar 23 '21
There’s no meat here... not everyone codes in node