Unverified Commit 2271cd84 authored by Steven's avatar Steven Committed by GitHub

Fix `onLoadingComplete()` callback when image src is Data URL (#29367)

Fixes #29363 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
parent f0eb9283
...@@ -10,6 +10,8 @@ import { ...@@ -10,6 +10,8 @@ import {
import { useIntersection } from './use-intersection' import { useIntersection } from './use-intersection'
const loadedImageURLs = new Set<string>() const loadedImageURLs = new Set<string>()
const emptyDataURL =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
;(global as any).__NEXT_IMAGE_IMPORTED = true ;(global as any).__NEXT_IMAGE_IMPORTED = true
...@@ -254,7 +256,7 @@ function handleLoading( ...@@ -254,7 +256,7 @@ function handleLoading(
return return
} }
const handleLoad = () => { const handleLoad = () => {
if (!img.src.startsWith('data:')) { if (img.src !== emptyDataURL) {
const p = 'decode' in img ? img.decode() : Promise.resolve() const p = 'decode' in img ? img.decode() : Promise.resolve()
p.catch(() => {}).then(() => { p.catch(() => {}).then(() => {
if (placeholder === 'blur') { if (placeholder === 'blur') {
...@@ -561,7 +563,7 @@ export default function Image({ ...@@ -561,7 +563,7 @@ export default function Image({
} }
let imgAttributes: GenImgAttrsResult = { let imgAttributes: GenImgAttrsResult = {
src: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', src: emptyDataURL,
srcSet: undefined, srcSet: undefined,
sizes: undefined, sizes: undefined,
} }
......
...@@ -11,12 +11,14 @@ const Page = () => ( ...@@ -11,12 +11,14 @@ const Page = () => (
width="128" width="128"
height="128" height="128"
/> />
<hr />
<ImageWithMessage <ImageWithMessage
id="2" id="2"
src={require('../public/test.png')} src={require('../public/test.png')}
placeholder="blur" placeholder="blur"
layout="fixed" layout="fixed"
/> />
<hr />
<ImageWithMessage <ImageWithMessage
id="3" id="3"
src="/test.svg" src="/test.svg"
...@@ -24,6 +26,7 @@ const Page = () => ( ...@@ -24,6 +26,7 @@ const Page = () => (
width="1200" width="1200"
height="1200" height="1200"
/> />
<hr />
<div style={{ position: 'relative', width: '64px', height: '64px' }}> <div style={{ position: 'relative', width: '64px', height: '64px' }}>
<ImageWithMessage <ImageWithMessage
id="4" id="4"
...@@ -32,6 +35,22 @@ const Page = () => ( ...@@ -32,6 +35,22 @@ const Page = () => (
objectFit="contain" objectFit="contain"
/> />
</div> </div>
<hr />
<ImageWithMessage
id="5"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAFCAYAAACAcVaiAAAAE0lEQVR42mNk+P+/ngEKGMngAADDdwx3Uz/3AAAAAABJRU5ErkJggg=="
layout="fixed"
width={64}
height={64}
/>
<hr />
<div style={{ position: 'relative', width: '64px', height: '64px' }}>
<ImageWithMessage
id="6"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAFCAYAAACAcVaiAAAAE0lEQVR42mNk+P+/ngEKGMngAADDdwx3Uz/3AAAAAABJRU5ErkJggg=="
layout="fill"
/>
</div>
<div id="footer" /> <div id="footer" />
</div> </div>
) )
...@@ -50,7 +69,6 @@ function ImageWithMessage({ id, ...props }) { ...@@ -50,7 +69,6 @@ function ImageWithMessage({ id, ...props }) {
{...props} {...props}
/> />
<p id={`msg${id}`}>{msg}</p> <p id={`msg${id}`}>{msg}</p>
<hr />
</> </>
) )
} }
......
...@@ -220,6 +220,14 @@ function runTests(mode) { ...@@ -220,6 +220,14 @@ function runTests(mode) {
() => browser.eval(`document.getElementById("msg4").textContent`), () => browser.eval(`document.getElementById("msg4").textContent`),
'loaded img4 with dimensions 21x21' 'loaded img4 with dimensions 21x21'
) )
await check(
() => browser.eval(`document.getElementById("msg5").textContent`),
'loaded img5 with dimensions 3x5'
)
await check(
() => browser.eval(`document.getElementById("msg6").textContent`),
'loaded img6 with dimensions 3x5'
)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
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