Enhance lazy loading for backgrounds
| @@ -479,12 +479,14 @@ function activateLazyLoader() { | ||
| 479 | 479 | lazyLoadObserver = new IntersectionObserver((entries, observer) => { |
| 480 | 480 | entries.forEach(entry => { |
| 481 | 481 | if (entry.target instanceof HTMLElement && entry.isIntersecting) { |
| 482 | 482 | const imageUrltarget = entry.target.dataset.bgSrc; |
| 483 | - if (imageUrl) { | |
| 483 | + const bg = target.getAttribute('bgfile'); | |
| 484 | - entry.target.style.backgroundImage = `url('${imageUrl}')`; | |
| 484 | + const isCustom = target.getAttribute('custom') === 'true'; | |
| 485 | - } | |
| 485 | + resolveImageUrl(bg, isCustom) | |
| 486 | - entry.target.classList.remove('lazy-load-background'); | |
| 486 | + .then(url => { target.style.backgroundImage = url; }) | |
| 487 | - observer.unobserve(entry.target); | |
| 487 | + .catch(() => { target.style.backgroundImage = PLACEHOLDER_IMAGE; }); | |
| 488 | + target.classList.remove('lazy-load-background'); | |
| 489 | + observer.unobserve(target); | |
| 488 | 490 | } |
| 489 | 491 | }); |
| 490 | 492 | }, options); |
| @@ -508,6 +510,24 @@ function generateUrlParameter(bg, isCustom) { | ||
| 508 | 510 | } |
| 509 | 511 | |
| 510 | 512 | /** |
| 513 | + * Resolves the image URL for the background. | |
| 514 | + * @param {string} bg Background file name | |
| 515 | + * @param {boolean} isCustom Is a custom background | |
| 516 | + * @returns {Promise<string>} CSS URL of the background | |
| 517 | + */ | |
| 518 | +async function resolveImageUrl(bg, isCustom) { | |
| 519 | + const fileExtension = bg.split('.').pop().toLowerCase(); | |
| 520 | + const isAnimated = ['mp4', 'webp'].includes(fileExtension); | |
| 521 | + const thumbnailUrl = isAnimated && !background_settings.animation | |
| 522 | + ? await getThumbnailFromStorage(bg) | |
| 523 | + : isCustom | |
| 524 | + ? bg | |
| 525 | + : getThumbnailUrl('bg', bg); | |
| 526 | + | |
| 527 | + return `url('${thumbnailUrl}')`; | |
| 528 | +} | |
| 529 | + | |
| 530 | +/** | |
| 511 | 531 | * Instantiates a background template |
| 512 | 532 | * @param {string} bg Path to background |
| 513 | 533 | * @param {boolean} isCustom Whether the background is custom |
| @@ -518,19 +538,11 @@ async function getBackgroundFromTemplate(bg, isCustom) { | ||
| 518 | 538 | const url = generateUrlParameter(bg, isCustom); |
| 519 | 539 | const title = isCustom ? bg.split('/').pop() : bg; |
| 520 | 540 | const friendlyTitle = title.slice(0, title.lastIndexOf('.')); |
| 521 | - const fileExtension = bg.split('.').pop().toLowerCase(); | |
| 522 | - const isAnimated = ['mp4', 'webp'].includes(fileExtension); | |
| 523 | - const thumbnailUrl = isAnimated && !background_settings.animation | |
| 524 | - ? await getThumbnailFromStorage(bg) | |
| 525 | - : isCustom | |
| 526 | - ? bg | |
| 527 | - : getThumbnailUrl('bg', bg); | |
| 528 | 541 | |
| 529 | 542 | template.attr('title', title); |
| 530 | 543 | template.attr('bgfile', bg); |
| 531 | 544 | template.attr('custom', String(isCustom)); |
| 532 | 545 | template.data('url', url); |
| 533 | - template.attr('data-bg-src', thumbnailUrl); | |
| 534 | 546 | template.addClass('lazy-load-background'); |
| 535 | 547 | template.css('background-image', PLACEHOLDER_IMAGE); |
| 536 | 548 | template.find('.BGSampleTitle').text(friendlyTitle); |