Unverified Commit 8066e423 authored by Alex Castle's avatar Alex Castle Committed by GitHub

Don't lazy-load already-loaded image in client-side transition (#26968)

fixes #19074 
This change disables image lazy-loading when both of the following are true:
1) A image is being rendered following a client-side page transition
2) The image has been previously loaded during this session.

Before this change, all images with lazy-loading enabled have a visible flicker during client-side page transitions, even though they're already loaded.

With this change, there's are two performance risks:

1) There's a chance that some offscreen images will have lazy-loading disabled unnecessarily because they were previously loaded. I think the performance hit here is pretty negligible and the situation is unlikely to come up very often.

2) There's a chance a different-sized version of the image will be selected by the browser, but lazy-loading will be disabled anyway. This seems even more unlikely to me, and anyway the performance hit from a stray un-lazy-loaded image (on a client-side transition) is very minor.

In both cases, I think the performance risk is outweighed by the UX improvement of getting rid of the image flicker on page transition.
parent f1e6bc9e
...@@ -9,6 +9,8 @@ import { ...@@ -9,6 +9,8 @@ import {
} from '../server/image-config' } from '../server/image-config'
import { useIntersection } from './use-intersection' import { useIntersection } from './use-intersection'
const loadedImageURLs = new Set<string>()
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
;(global as any).__NEXT_IMAGE_IMPORTED = true ;(global as any).__NEXT_IMAGE_IMPORTED = true
} }
...@@ -264,6 +266,7 @@ function defaultImageLoader(loaderProps: ImageLoaderProps) { ...@@ -264,6 +266,7 @@ function defaultImageLoader(loaderProps: ImageLoaderProps) {
// handler instead of the img's onLoad attribute. // handler instead of the img's onLoad attribute.
function handleLoading( function handleLoading(
img: HTMLImageElement | null, img: HTMLImageElement | null,
src: string,
placeholder: PlaceholderValue, placeholder: PlaceholderValue,
onLoadingComplete?: () => void onLoadingComplete?: () => void
) { ) {
...@@ -279,6 +282,7 @@ function handleLoading( ...@@ -279,6 +282,7 @@ function handleLoading(
img.style.backgroundSize = 'none' img.style.backgroundSize = 'none'
img.style.backgroundImage = 'none' img.style.backgroundImage = 'none'
} }
loadedImageURLs.add(src)
if (onLoadingComplete) { if (onLoadingComplete) {
onLoadingComplete() onLoadingComplete()
} }
...@@ -423,6 +427,9 @@ export default function Image({ ...@@ -423,6 +427,9 @@ export default function Image({
unoptimized = true unoptimized = true
isLazy = false isLazy = false
} }
if (src && typeof window !== 'undefined' && loadedImageURLs.has(src)) {
isLazy = false
}
const [setRef, isIntersected] = useIntersection<HTMLImageElement>({ const [setRef, isIntersected] = useIntersection<HTMLImageElement>({
rootMargin: '200px', rootMargin: '200px',
...@@ -558,6 +565,8 @@ export default function Image({ ...@@ -558,6 +565,8 @@ export default function Image({
}) })
} }
let srcString: string = src
return ( return (
<div style={wrapperStyle}> <div style={wrapperStyle}>
{sizerStyle ? ( {sizerStyle ? (
...@@ -605,7 +614,7 @@ export default function Image({ ...@@ -605,7 +614,7 @@ export default function Image({
className={className} className={className}
ref={(img) => { ref={(img) => {
setRef(img) setRef(img)
handleLoading(img, placeholder, onLoadingComplete) handleLoading(img, srcString, placeholder, onLoadingComplete)
}} }}
style={imgStyle} style={imgStyle}
/> />
......
...@@ -8,7 +8,7 @@ const Lazy = () => { ...@@ -8,7 +8,7 @@ const Lazy = () => {
<p id="stubtext">This is a page with lazy-loaded images</p> <p id="stubtext">This is a page with lazy-loaded images</p>
<Image <Image
id="lazy-top" id="lazy-top"
src="foo1.jpg" src="lazy1.jpg"
height={400} height={400}
width={1024} width={1024}
loading="lazy" loading="lazy"
...@@ -16,7 +16,7 @@ const Lazy = () => { ...@@ -16,7 +16,7 @@ const Lazy = () => {
<div style={{ height: '2000px' }}></div> <div style={{ height: '2000px' }}></div>
<Image <Image
id="lazy-mid" id="lazy-mid"
src="foo2.jpg" src="lazy2.jpg"
loading="lazy" loading="lazy"
height={400} height={400}
width={300} width={300}
...@@ -25,7 +25,7 @@ const Lazy = () => { ...@@ -25,7 +25,7 @@ const Lazy = () => {
<div style={{ height: '2000px' }}></div> <div style={{ height: '2000px' }}></div>
<Image <Image
id="lazy-bottom" id="lazy-bottom"
src="https://www.otherhost.com/foo3.jpg" src="https://www.otherhost.com/lazy3.jpg"
height={400} height={400}
width={300} width={300}
unoptimized unoptimized
...@@ -34,14 +34,14 @@ const Lazy = () => { ...@@ -34,14 +34,14 @@ const Lazy = () => {
<div style={{ height: '2000px' }}></div> <div style={{ height: '2000px' }}></div>
<Image <Image
id="lazy-without-attribute" id="lazy-without-attribute"
src="foo4.jpg" src="lazy4.jpg"
height={400} height={400}
width={800} width={800}
></Image> ></Image>
<div style={{ height: '2000px' }}></div> <div style={{ height: '2000px' }}></div>
<Image <Image
id="eager-loading" id="eager-loading"
src="foo5.jpg" src="lazy5.jpg"
loading="eager" loading="eager"
height={400} height={400}
width={1900} width={1900}
......
...@@ -109,10 +109,10 @@ function runTests() { ...@@ -109,10 +109,10 @@ function runTests() {
function lazyLoadingTests() { function lazyLoadingTests() {
it('should have loaded the first image immediately', async () => { it('should have loaded the first image immediately', async () => {
expect(await browser.elementById('lazy-top').getAttribute('src')).toBe( expect(await browser.elementById('lazy-top').getAttribute('src')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=2000' 'https://example.com/myaccount/lazy1.jpg?auto=format&fit=max&w=2000'
) )
expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe( expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=2000 2x' 'https://example.com/myaccount/lazy1.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/lazy1.jpg?auto=format&fit=max&w=2000 2x'
) )
}) })
it('should not have loaded the second image immediately', async () => { it('should not have loaded the second image immediately', async () => {
...@@ -140,11 +140,11 @@ function lazyLoadingTests() { ...@@ -140,11 +140,11 @@ function lazyLoadingTests() {
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('src') return browser.elementById('lazy-mid').getAttribute('src')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=1024') }, 'https://example.com/myaccount/lazy2.jpg?auto=format&fit=max&w=1024')
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('srcset') return browser.elementById('lazy-mid').getAttribute('srcset')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480 1x, https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=1024 2x') }, 'https://example.com/myaccount/lazy2.jpg?auto=format&fit=max&w=480 1x, https://example.com/myaccount/lazy2.jpg?auto=format&fit=max&w=1024 2x')
}) })
it('should not have loaded the third image after scrolling down', async () => { it('should not have loaded the third image after scrolling down', async () => {
expect(await browser.elementById('lazy-bottom').getAttribute('src')).toBe( expect(await browser.elementById('lazy-bottom').getAttribute('src')).toBe(
...@@ -165,7 +165,7 @@ function lazyLoadingTests() { ...@@ -165,7 +165,7 @@ function lazyLoadingTests() {
) )
await waitFor(200) await waitFor(200)
expect(await browser.elementById('lazy-bottom').getAttribute('src')).toBe( expect(await browser.elementById('lazy-bottom').getAttribute('src')).toBe(
'https://www.otherhost.com/foo3.jpg' 'https://www.otherhost.com/lazy3.jpg'
) )
expect( expect(
await browser.elementById('lazy-bottom').getAttribute('srcset') await browser.elementById('lazy-bottom').getAttribute('srcset')
...@@ -189,17 +189,17 @@ function lazyLoadingTests() { ...@@ -189,17 +189,17 @@ function lazyLoadingTests() {
await waitFor(200) await waitFor(200)
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('src') await browser.elementById('lazy-without-attribute').getAttribute('src')
).toBe('https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1600') ).toBe('https://example.com/myaccount/lazy4.jpg?auto=format&fit=max&w=1600')
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('srcset') await browser.elementById('lazy-without-attribute').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1600 2x' 'https://example.com/myaccount/lazy4.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/lazy4.jpg?auto=format&fit=max&w=1600 2x'
) )
}) })
it('should load the fifth image eagerly, without scrolling', async () => { it('should load the fifth image eagerly, without scrolling', async () => {
expect(await browser.elementById('eager-loading').getAttribute('src')).toBe( expect(await browser.elementById('eager-loading').getAttribute('src')).toBe(
'https://example.com/myaccount/foo5.jpg?auto=format&fit=max&w=2000' 'https://example.com/myaccount/lazy5.jpg?auto=format&fit=max&w=2000'
) )
expect( expect(
await browser.elementById('eager-loading').getAttribute('srcset') await browser.elementById('eager-loading').getAttribute('srcset')
......
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