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: should not warn when image unmounts #46567

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function handleLoading(
img['data-loaded-src'] = src
const p = 'decode' in img ? img.decode() : Promise.resolve()
p.catch(() => {}).then(() => {
if (!img.parentNode) {
if (!img.parentElement || !img.isConnected) {
// Exit early in case of race condition:
// - onload() is called
// - decode() is called but incomplete
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Image from 'next/image'
import { useEffect, useState } from 'react'

export default function Home() {
const [displayImage, setDisplayImage] = useState(true)

useEffect(() => {
// This will cause the image to unmount.
// See https://github.com/vercel/next.js/issues/40762
setDisplayImage(false)
}, [])

return (
<main>
<h1>Should not warn on unmount</h1>
<section>
{displayImage ? (
<div style={{ position: 'relative', width: 10, height: 10 }}>
<Image priority fill src="/test.jpg" alt="alt" sizes="10px" />
</div>
) : null}
</section>
</main>
)
}
10 changes: 10 additions & 0 deletions test/integration/next-image-new/default/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,16 @@ function runTests(mode) {
'Image with src "/wide.png" has "fill" but is missing "sizes" prop. Please add it to improve page performance. Read more:'
)
})
it('should not log warnings when image unmounts', async () => {
browser = await webdriver(appPort, '/should-not-warn-unmount')
await waitFor(1000)
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
expect(warnings).not.toContain(
'Image with src "/test.jpg" has "fill" and parent element'
)
})
}
})
// Tests that use the `unsized` attribute:
Expand Down