File tree 1 file changed +18
-0
lines changed
1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,23 @@ export default function NextImageFromFile(props: NextImageFromFileProps) {
9
9
width : number ;
10
10
height : number ;
11
11
} > ( null ) ;
12
+ const imageRef = React . useRef < null | HTMLImageElement > ( null ) ;
13
+
14
+ // In case the image was cached and won't trigger onLoad
15
+ // read width and height from the ref
16
+ React . useEffect ( ( ) => {
17
+ // image already there or not rendered yet?
18
+ if ( image || ! imageRef . current ) return ;
19
+ // image has not been loaded yet?
20
+ if ( imageRef . current . complete === false ) return ;
21
+ // width and height are 0 so the image has not been painted yet?
22
+ if ( imageRef . current . width === 0 || imageRef . current . height === 0 ) return ;
23
+
24
+ setImage ( {
25
+ width : imageRef . current . width ,
26
+ height : imageRef . current . height ,
27
+ } ) ;
28
+ } ) ;
12
29
13
30
// Fall back to regular next/image if all necessary props are given
14
31
if ( typeof props . src !== "string" || ( props . width && props . height ) ) {
@@ -20,6 +37,7 @@ export default function NextImageFromFile(props: NextImageFromFileProps) {
20
37
return (
21
38
< img
22
39
src = { props . src }
40
+ ref = { imageRef }
23
41
onLoad = { ( event ) => {
24
42
const target = event . target as HTMLImageElement ;
25
43
setImage ( { width : target . width , height : target . height } ) ;
You can’t perform that action at this time.
0 commit comments