Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import { useIntersection } from './use-intersection'

const loadedImageURLs = new Set<string>()
const emptyDataURL =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'

if (typeof window === 'undefined') {
;(global as any).__NEXT_IMAGE_IMPORTED = true
Expand Down Expand Up @@ -254,7 +256,7 @@ function handleLoading(
return
}
const handleLoad = () => {
if (!img.src.startsWith('data:')) {
if (img.src !== emptyDataURL) {
const p = 'decode' in img ? img.decode() : Promise.resolve()
p.catch(() => {}).then(() => {
if (placeholder === 'blur') {
Expand Down Expand Up @@ -561,7 +563,7 @@ export default function Image({
}

let imgAttributes: GenImgAttrsResult = {
src: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
src: emptyDataURL,
srcSet: undefined,
sizes: undefined,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ const Page = () => (
width="128"
height="128"
/>
<hr />
<ImageWithMessage
id="2"
src={require('../public/test.png')}
placeholder="blur"
layout="fixed"
/>
<hr />
<ImageWithMessage
id="3"
src="/test.svg"
layout="responsive"
width="1200"
height="1200"
/>
<hr />
<div style={{ position: 'relative', width: '64px', height: '64px' }}>
<ImageWithMessage
id="4"
Expand All @@ -32,6 +35,22 @@ const Page = () => (
objectFit="contain"
/>
</div>
<hr />
<ImageWithMessage
id="5"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAFCAYAAACAcVaiAAAAE0lEQVR42mNk+P+/ngEKGMngAADDdwx3Uz/3AAAAAABJRU5ErkJggg=="
layout="fixed"
width={64}
ijjk marked this conversation as resolved.
Show resolved Hide resolved
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>
)
Expand All @@ -50,7 +69,6 @@ function ImageWithMessage({ id, ...props }) {
{...props}
/>
<p id={`msg${id}`}>{msg}</p>
<hr />
</>
)
}
Expand Down
8 changes: 8 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ function runTests(mode) {
() => browser.eval(`document.getElementById("msg4").textContent`),
'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 {
if (browser) {
await browser.close()
Expand Down