Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@bva/iolazyload
Advanced tools
A component built to utilize intersection observers to implement lazy loading. It is extendable to allow for custom lazy loading of any component.
There are four settings the component will accept:
The success callback will take three parameters:
The error callback takes one parameter:
The error callback will default to the success callback if none is provided - and will just be fired on the DOMContentLoaded
event for each entry that it would have otherwise been bound to.
First, drop the component into your project and import it into wherever you will be performing your lazy loading.
Next, to add lazy loading you'll use one function: setupObservers
.
The default use case is for adding lazy loading to image sources, source sets, and images as backgrounds via CSS. This is done with the following attributes:
src
attribute on the image coming into the viewportbackground-image
style.background-image
style (example to follow). Requires the element to also have a unique data-io-id
attribute (for scoping styles correctly).Here's an example of setting up lazy loading for an image, and also for adding background images to a div. First, we need the markup and then perform the JS setup:
<!-- HTML / Liquid -->
<img io-img-src="www.image.com/my-image.png" io-img-srcset="www.image.com/my-image.png 400w, www.image.com/my-image-big.png 800w" />
<div data-io-id="{{- section.id -}}" io-bg-srcset="www.image.com/my-image.png 0px, www.image.com/my-image-big.png 800px, www.image.com/my-image-extra-big.png 1200px"></div>
/** JS */
import LazyLoad from '@bva/iolazyload';
document.addEventListener('DOMContentLoaded', () => {
LazyLoad.setupObservers();
});
It's possible to add custom callbacks for any element:
<!-- HTML -->
<article data-lazy-article>
...
</article>
/** JS */
import LazyLoad from '@bva/iolazyload';
const displayArticle = (entry) => {
entry.classList.add('loaded');
};
const successCallback = (entry, observer, isIntersecting) => {
if (isIntersecting) {
displayArticle(entry);
observer.unobserve(entry);
}
};
document.addEventListener('DOMContentLoaded', () => {
LazyLoad.setupObservers({
queryString: '[data-lazy-article]',
callback: successCallback,
errorCallback: displayArticle,
margins: '10% 10% 10% 10%'
});
});
Notice we use the observer object, and once the article is loaded then unobserve it so as not to try and keep loading it in everytime it scrolls into view.
The component adds a custom event that is dispatched in the default success callback after an element has had its src
or background style updated when it comes into view.
The event is ioSourceSet
and is bound to the document. The events details.target
property refers to the element that was loaded into view.
Here's an example where we add the class loaded
to an element once the default callback has loaded the element:
document.addEventListener('ioSourceSet', (event) => {
const elementLoaded = event.detail.target;
elementLoaded.classList.add('loaded');
});
FAQs
Intersection Observer based lazy loading component
We found that @bva/iolazyload demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.