Unverified Commit 5d7f7a47 authored by Alex Castle's avatar Alex Castle Committed by GitHub

Unlazify images if no intersection observer found (#18345)

parent 27f56eb3
...@@ -48,10 +48,10 @@ configDeviceSizes.sort((a, b) => a - b) ...@@ -48,10 +48,10 @@ configDeviceSizes.sort((a, b) => a - b)
configImageSizes.sort((a, b) => a - b) configImageSizes.sort((a, b) => a - b)
let cachedObserver: IntersectionObserver let cachedObserver: IntersectionObserver
const IntersectionObserver =
typeof window !== 'undefined' ? window.IntersectionObserver : null
function getObserver(): IntersectionObserver | undefined { function getObserver(): IntersectionObserver | undefined {
const IntersectionObserver =
typeof window !== 'undefined' ? window.IntersectionObserver : null
// Return shared instance of IntersectionObserver if already created // Return shared instance of IntersectionObserver if already created
if (cachedObserver) { if (cachedObserver) {
return cachedObserver return cachedObserver
...@@ -61,12 +61,21 @@ function getObserver(): IntersectionObserver | undefined { ...@@ -61,12 +61,21 @@ function getObserver(): IntersectionObserver | undefined {
if (!IntersectionObserver) { if (!IntersectionObserver) {
return undefined return undefined
} }
return (cachedObserver = new IntersectionObserver( return (cachedObserver = new IntersectionObserver(
(entries) => { (entries) => {
entries.forEach((entry) => { entries.forEach((entry) => {
if (entry.isIntersecting) { if (entry.isIntersecting) {
let lazyImage = entry.target as HTMLImageElement let lazyImage = entry.target as HTMLImageElement
unLazifyImage(lazyImage)
cachedObserver.unobserve(lazyImage)
}
})
},
{ rootMargin: '200px' }
))
}
function unLazifyImage(lazyImage: HTMLImageElement): void {
if (lazyImage.dataset.src) { if (lazyImage.dataset.src) {
lazyImage.src = lazyImage.dataset.src lazyImage.src = lazyImage.dataset.src
} }
...@@ -75,12 +84,6 @@ function getObserver(): IntersectionObserver | undefined { ...@@ -75,12 +84,6 @@ function getObserver(): IntersectionObserver | undefined {
} }
lazyImage.style.visibility = 'visible' lazyImage.style.visibility = 'visible'
lazyImage.classList.remove('__lazy') lazyImage.classList.remove('__lazy')
cachedObserver.unobserve(lazyImage)
}
})
},
{ rootMargin: '200px' }
))
} }
function getDeviceSizes(width: number | undefined): number[] { function getDeviceSizes(width: number | undefined): number[] {
...@@ -234,6 +237,11 @@ export default function Image({ ...@@ -234,6 +237,11 @@ export default function Image({
lazy = true lazy = true
} }
if (typeof window !== 'undefined' && !window.IntersectionObserver) {
// Rendering client side on browser without intersection observer
lazy = false
}
useEffect(() => { useEffect(() => {
const target = thisEl.current const target = thisEl.current
...@@ -246,6 +254,9 @@ export default function Image({ ...@@ -246,6 +254,9 @@ export default function Image({
return () => { return () => {
observer.unobserve(target) observer.unobserve(target)
} }
} else {
//browsers without intersection observer
unLazifyImage(target)
} }
} }
}, [thisEl, lazy]) }, [thisEl, lazy])
......
import React from 'react' import React from 'react'
import Image from 'next/image' import Image from 'next/image'
import Link from 'next/link'
const Lazy = () => { const Lazy = () => {
return ( return (
...@@ -45,6 +46,9 @@ const Lazy = () => { ...@@ -45,6 +46,9 @@ const Lazy = () => {
height={400} height={400}
width={1900} width={1900}
></Image> ></Image>
<Link href="/missing-observer">
<a id="observerlink">observer</a>
</Link>
</div> </div>
) )
} }
......
import React, { useLayoutEffect } from 'react'
import Image from 'next/image'
const Lazy = () => {
useLayoutEffect(() => {
IntersectionObserver = null //eslint-disable-line
})
return (
<div>
<p id="stubtext">
This is a page with one lazy-loaded image, to be used in the test for
browsers without intersection observer.
</p>
<Image
id="lazy-no-observer"
src="foox.jpg"
height={400}
width={1024}
loading="lazy"
></Image>
</div>
)
}
export default Lazy
...@@ -280,6 +280,17 @@ describe('Image Component Tests', () => { ...@@ -280,6 +280,17 @@ describe('Image Component Tests', () => {
browser = null browser = null
}) })
lazyLoadingTests() lazyLoadingTests()
it('should automatically load images if observer does not exist', async () => {
browser = await webdriver(appPort, '/missing-observer')
expect(
await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe('https://example.com/myaccount/foox.jpg?auto=format&w=1024')
expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&w=1024 1024w'
)
})
}) })
describe('Client-side Lazy Loading Tests', () => { describe('Client-side Lazy Loading Tests', () => {
beforeAll(async () => { beforeAll(async () => {
...@@ -291,5 +302,18 @@ describe('Image Component Tests', () => { ...@@ -291,5 +302,18 @@ describe('Image Component Tests', () => {
browser = null browser = null
}) })
lazyLoadingTests() lazyLoadingTests()
it('should automatically load images if observer does not exist', async () => {
await browser.waitForElementByCss('#observerlink').click()
await waitFor(500)
browser = await webdriver(appPort, '/missing-observer')
expect(
await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe('https://example.com/myaccount/foox.jpg?auto=format&w=1024')
expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&w=1024 1024w'
)
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment