Handle browsers that don't support IntersectionObserver (#1739)

* Remove ad-handling in videojs.jsx

Primary impetus is to remove unnecessary IntersectionObserver usage, but it should be removed anyway because:
- no longer relevant today with Adnimation's script.
- we also globally hide floating ads now, so no more invisible divs.
- the code is wrongly placed -- it's not the responsibility of the Videojs component.

* use-lazy-loaded: skip if IntersectionObserver is not supported

## Issue
Page not loading in older Safari (e.g. ipad air 2)

## Approach
Instead of using a polyfill (which comes with implementation caveats), just not apply the lazy-loading for those old browsers. Not lazy-loading is better than not loading at all, plus this is way easier to test (even by just reading the code) than testing out the polyfill implementation's caveats.

The cons is we would need the polyfill if we use it in other places in the future.

## Code Changes
Factor out the src-setting code, and use it directly when IntersectionObserver is not found.
This commit is contained in:
infinite-persistence 2022-06-23 20:10:27 +08:00 committed by GitHub
parent 3687292cdf
commit 7cbb7a54aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 49 deletions

View file

@ -525,33 +525,6 @@ export default React.memo<Props>(function VideoJs(props: Props) {
}); });
} }
} }
// fix invisible vidcrunch overlay on IOS << TODO: does not belong here. Move to ads.jsx (#739)
if (IS_IOS) {
// ads video player
const adsClaimDiv = document.querySelector('.ads__claim-item');
if (adsClaimDiv) {
// hide ad video by default
adsClaimDiv.style.display = 'none';
// ad containing div, we can keep part on page
const adsClaimParentDiv = adsClaimDiv.parentNode;
// watch parent div for when it is on viewport
const observer = new IntersectionObserver(function (entries) {
// when ad div parent becomes visible by 1px, show the ad video
if (entries[0].isIntersecting === true) {
adsClaimDiv.style.display = 'block';
}
observer.disconnect();
});
// $FlowFixMe
observer.observe(adsClaimParentDiv);
}
}
})(); })();
// Cleanup // Cleanup

View file

@ -26,29 +26,17 @@ export default function useLazyLoading(
return Math.ceil(value * devicePixelRatio); return Math.ceil(value * devicePixelRatio);
} }
useEffect(() => { function loadImgFromDataset(target, backgroundFallback, setSrcLoadedFn) {
if (!elementRef.current) { // lazy-loaded <img>:
return;
}
const lazyLoadingObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.intersectionRatio >= threshold) {
const { target } = entry;
observer.unobserve(target);
// useful for lazy loading img tags
if (target.dataset.src) { if (target.dataset.src) {
// $FlowFixMe // $FlowFixMe
target.src = target.dataset.src; target.src = target.dataset.src;
setSrcLoaded(true); setSrcLoadedFn(true);
// No fallback handling here (clients have access to 'onerror' on the image ref). // No fallback handling here (clients have access to 'onerror' on the image ref).
return; return;
} }
// useful for lazy loading background images on divs // lazy-loaded `background-image`:
if (target.dataset.backgroundImage) { if (target.dataset.backgroundImage) {
if (backgroundFallback) { if (backgroundFallback) {
const tmpImage = new Image(); const tmpImage = new Image();
@ -60,6 +48,25 @@ export default function useLazyLoading(
target.style.backgroundImage = `url(${target.dataset.backgroundImage})`; target.style.backgroundImage = `url(${target.dataset.backgroundImage})`;
} }
} }
useEffect(() => {
if (!elementRef.current) {
return;
}
if (!window.IntersectionObserver) {
loadImgFromDataset(elementRef.current, backgroundFallback, setSrcLoaded);
return;
}
const lazyLoadingObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.intersectionRatio >= threshold) {
const { target } = entry;
observer.unobserve(target);
loadImgFromDataset(target, backgroundFallback, setSrcLoaded);
}
}); });
}, },
{ {