Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Instead of loading in the page, and then accessing that image, disable the loading entirely by using e.g. `data-src` instead of `src` in the HTML. You then fetch() the URL in that data attribute in your Javascript.

Here's a straightforward tutorial covering that: https://trezy.com/blog/loading-images-with-web-workers [0]

Note that this does mean the images will be delayed from loading, and will cause a layout shift when they're finally inserted. It's best to inline the image's height & width into the HTML if you know them in advance (or use `aspect-ratio`): https://www.aleksandrhovhannisyan.com/blog/setting-width-and...

[0] Just as a note, the tutorial says "<img> tags actually block your application load," which is very wrong.



Interesting, I hadn’t considered that. Maybe I could realize my dream of extending the img tag after all.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: