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 next/image usage of onError() #36305

Merged
merged 6 commits into from Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion packages/next/client/image.tsx
Expand Up @@ -368,7 +368,6 @@ export default function Image({
objectFit,
objectPosition,
onLoadingComplete,
onError,
placeholder = 'empty',
blurDataURL,
...all
Expand Down
44 changes: 44 additions & 0 deletions test/integration/image-component/default/pages/on-error.js
@@ -0,0 +1,44 @@
import { useState } from 'react'
import Image from 'next/image'

const Page = () => {
return (
<div>
<h1>Test onError</h1>
<p>
If error occured while loading image, native onError should be called.
</p>
<ImageWithMessage id="1" src="/test.png" layout="fill" />

<ImageWithMessage id="2" src="/nonexistent-img.png" layout="fill" />
<div id="footer" />
</div>
)
}

function ImageWithMessage({ id, ...props }) {
const [msg, setMsg] = useState('no error occured')
const style =
props.layout === 'fill'
? { position: 'relative', width: '64px', height: '64px' }
: {}

return (
<>
<div className="wrap" style={style}>
<Image
id={`img${id}`}
onError={(e) => {
const msg = `error occured while loading ${e.target.id}`
setMsg(msg)
}}
{...props}
/>
</div>
<p id={`msg${id}`}>{msg}</p>
<hr />
</>
)
}

export default Page
23 changes: 23 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -363,6 +363,29 @@ function runTests(mode) {
)
})

it('should callback native onError when error occured while loading image', async () => {
let browser = await webdriver(appPort, '/on-error')

await check(
() => browser.eval(`document.getElementById("img1").currentSrc`),
/test\.png/
)
await check(
() => browser.eval(`document.getElementById("img2").currentSrc`),
//This is an empty data url
/nonexistent-img\.png/
)
await check(
() => browser.eval(`document.getElementById("msg1").textContent`),
'no error occured'
)
await check(
() => browser.eval(`document.getElementById("msg2").textContent`),
'error occured while loading img2'
)
setTimeout(() => {}, [10000])
styfle marked this conversation as resolved.
Show resolved Hide resolved
})

it('should work with image with blob src', async () => {
let browser
try {
Expand Down