whmcs-lazy-loading
Lazy load assets for WHMCS
SKILL.mdskills/whmcs-lazy-loading/SKILL.md
GitHubWHMCS Lazy Loading Skill
Overview
This skill provides patterns for lazy loading assets in WHMCS.
Implementation Patterns
Lazy Load Manager
<?php
/**
* WHMCS Lazy Loading
* Implements lazy loading for images/scripts
*/
namespace WHMCS\Module\Performance\LazyLoad;
class LazyLoadManager {
/**
* Generate lazy load attributes
*/
public function getLazyAttributes(string $type, string $src): array {
if ($type === 'image') {
return [
'loading' => 'lazy',
'src' => $src,
'data-src' => $src
];
}
return ['data-src' => $src, 'class' => 'lazy-load'];
}
/**
* Generate lazy load script
*/
public function getLazyLoadScript(): string {
return <<<SCRIPT
document.addEventListener("DOMContentLoaded", function() {
const lazyImages = document.querySelectorAll("img[data-src]");
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.src = entry.target.dataset.src;
observer.unobserve(entry.target);
}
});
});
lazyImages.forEach(img => observer.observe(img));
});
SCRIPT;
}
}
Best Practices
- Native Lazy Loading: Use loading="lazy" attribute
- Intersection Observer: Use for custom lazy loading
- Placeholder Images: Use lightweight placeholders
- Below Fold First: Lazy load below-the-fold content
- CDN Integration: Lazy load from CDN
Related Skills
- whmcs-image-optimization
- whmcs-prefetching
- whmcs-resource-optimization
- whmcs-http2-push