question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How does this even work if your not overriding src?

See original GitHub issue

Pretty much all lazy loaders need to use src2 or override it in some way due to the browser automatically fetching images with a src value - so I’m a bit baffled how does this library work exactly?

Does this library just hope that it can kick in quick enough to alter the attributes on dom load? (sounds rather fragile and unreliable if so…)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mfranzkecommented, Dec 27, 2021

@garygreen thanks again for bringing up your idea with ServiceWorkers - it sadly took a lot of time due to several reasons to come up with a PoC and an evaluation of this topic.

I’ve identified the following aspects:

Pro

  • The img or iframe elements wouldn’t need to get wrapped by a noscript HTML tag any more, but still (as you’ve even also stated) we would need to identify the assets that are supposed to get lazy loaded. As Client hints within the HTTP headers aren’t an option here, I’ve mainly identified that the best would probably be query parameters added to the URL (I even also thought about implementing those via the anchor, but this might have unexpected side effects regarding caching). Additionally we would need to retrieve the width and height (same values like the equally named HTML attributes) of the image for the SVG placeholder, which could as well get transferred via query parameters.

Con

  • It doesn’t work on non-HTTPS-websites - probably a minor, issue as HTTPS should be(come) the new normal hopefully.
  • It doesn’t work for assets referenced cross-domain, like images or an iframe URL from another service, like e.g. Vimeo, or some (central) CDN.
  • It doesn’t work at the first page impression, but only the ones afterwards, as the service worker needs to get initiated first.
  • Obviously this polyfill wouldn’t work on browsers that don’t support ServiceWorker, which is mainly Microsoft Internet Explorer versions below 17; even though that there’s a graceful degradation, as you’ve written as well already.

Still these might be tradeoffs that someone might be accepting due to the simplification regarding the HTML code changes.

Conclusion

I’ve uploaded this very simple PoC to this URL - it doesn’t include any lazy functionality so far, but differentiating in between loading-lazy and non-loading-lazy could be done with removing the query parameters: https://goofy-bell-6b1148.netlify.app/

I’m still motivated to even also offer this other solution as a new polyfill though.

2reactions
mfranzkecommented, Jun 25, 2020

Thanks for the explanatation. I wonder if there is a way of using service workers to intercept requests to images and stop them from being downloaded unless they are in the viewport - tricky part would be somehow sending this meta data onto the service worker so it knows whether to allow loading or blocking of the image. Benefit of this approach would be a graceful/progressive enhancement, it won’t break any browsers that don’t support service workers and you won’t need to update any of your image srcs / srcset stuff - the browser just requests for stuff and it’s either accepted or blocked.

Maybe an idea to explore. Just thinking out loud 😃

I‘m loving your idea and it‘s actually a nice approach worth looking into. Especially as there‘s no other way I could think of to intercept the network traffic - or in other words this might be an even nicer one which even also further follows the principals of progressive enhancement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class is not Abstract and does not Override error in Java
You created a class Cat which implements an interface Animal. Your interface called Animal has an abstract method called roar which must be...
Read more >
<iframe>: The Inline Frame element - HTML - MDN Web Docs
Inline HTML to embed, overriding the src attribute. If a browser does not support the srcdoc attribute, it will fall back to the...
Read more >
Content Configuration
Configuring source paths. Tailwind CSS works by scanning all of your HTML, JavaScript components, and any other template files for class names, ...
Read more >
Working With Files
As described in the Apache Ant manual, the mkdir task will automatically create all necessary directories in the given path and will do...
Read more >
How to use @font-face in CSS
If you open the URL for the font, you can actually see all the ... SVG fonts are not currently supported by Firefox,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found