-
Notifications
You must be signed in to change notification settings - Fork 324
fix: support onLoadingComplete in modern next/image shim #496
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,8 @@ interface ImageProps { | |
| className?: string; | ||
| style?: React.CSSProperties; | ||
| onLoad?: React.ReactEventHandler<HTMLImageElement>; | ||
| /** @deprecated Use onLoad instead. Still supported for migration compat. */ | ||
| onLoadingComplete?: (img: HTMLImageElement) => void; | ||
| onError?: React.ReactEventHandler<HTMLImageElement>; | ||
| onClick?: React.MouseEventHandler<HTMLImageElement>; | ||
| id?: string; | ||
|
|
@@ -197,13 +199,24 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image( | |
| sizes, | ||
| className, | ||
| style, | ||
| onLoad, | ||
| onLoadingComplete, | ||
| unoptimized: _unoptimized, | ||
| overrideSrc: _overrideSrc, | ||
| loading, | ||
| ...rest | ||
| }, | ||
| ref, | ||
| ) { | ||
| // Wire onLoadingComplete (deprecated) into onLoad — matches Next.js behavior. | ||
| // onLoad fires first, then onLoadingComplete receives the HTMLImageElement. | ||
| const handleLoad = onLoadingComplete | ||
| ? (e: React.SyntheticEvent<HTMLImageElement>) => { | ||
| onLoad?.(e); | ||
| onLoadingComplete(e.currentTarget); | ||
| } | ||
| : onLoad; | ||
|
|
||
| // Handle StaticImageData (import result) | ||
| const src = typeof srcProp === "string" ? srcProp : srcProp.src; | ||
| const imgWidth = width ?? (typeof srcProp === "object" ? srcProp.width : undefined); | ||
|
|
@@ -225,6 +238,7 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image( | |
| decoding="async" | ||
| sizes={sizes} | ||
| className={className} | ||
| onLoad={handleLoad} | ||
| style={ | ||
| fill | ||
| ? { | ||
|
|
@@ -269,6 +283,7 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image( | |
| sizes={sizes} | ||
| className={className} | ||
| background={bg} | ||
| onLoad={handleLoad} | ||
| /> | ||
| ); | ||
| } | ||
|
|
@@ -285,6 +300,7 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image( | |
| sizes={sizes} | ||
| className={className} | ||
| background={bg} | ||
| onLoad={handleLoad} | ||
| /> | ||
| ); | ||
| } | ||
|
|
@@ -350,6 +366,7 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image( | |
| sizes={sizes ?? (fill ? "100vw" : undefined)} | ||
| className={className} | ||
| data-nimg={fill ? "fill" : "1"} | ||
| onLoad={handleLoad} | ||
| style={ | ||
| fill | ||
| ? { | ||
|
|
@@ -389,6 +406,8 @@ export function getImageProps(props: ImageProps): { | |
| sizes, | ||
| className, | ||
| style, | ||
| onLoad: _onLoad, | ||
| onLoadingComplete: _onLoadingComplete, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| unoptimized: _unoptimized, | ||
| overrideSrc: _overrideSrc, | ||
| loading, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: invocation order reversed vs Next.js. In the upstream modern
next/image(image-component.tsx), when both callbacks are present,onLoadfires beforeonLoadingComplete. This PR firesonLoadingCompletefirst.This matters because user code may rely on
onLoadside effects completing beforeonLoadingCompleteruns.Note: the legacy image shim correctly fires
onLoadingCompletefirst (matching legacy Next.js behavior), so only the modern shim needs this fix.