@@ -9,6 +9,8 @@ import React, {
9
9
useState ,
10
10
type JSX ,
11
11
} from 'react'
12
+ import * as ReactDOM from 'react-dom'
13
+ import Head from '../../shared/lib/head'
12
14
import {
13
15
imageConfigDefault ,
14
16
VALID_LOADERS ,
@@ -26,6 +28,8 @@ function normalizeSrc(src: string): string {
26
28
return src [ 0 ] === '/' ? src . slice ( 1 ) : src
27
29
}
28
30
31
+ const supportsFloat = typeof ReactDOM . preload === 'function'
32
+
29
33
const configEnv = process . env . __NEXT_IMAGE_OPTS as any as ImageConfigComplete
30
34
const loadedImageURLs = new Set < string > ( )
31
35
const allImgs = new Map <
@@ -978,6 +982,20 @@ export default function Image({
978
982
}
979
983
}
980
984
985
+ const linkProps :
986
+ | React . DetailedHTMLProps <
987
+ React . LinkHTMLAttributes < HTMLLinkElement > ,
988
+ HTMLLinkElement
989
+ >
990
+ | undefined = supportsFloat
991
+ ? undefined
992
+ : {
993
+ imageSrcSet : imgAttributes . srcSet ,
994
+ imageSizes : imgAttributes . sizes ,
995
+ crossOrigin : rest . crossOrigin ,
996
+ referrerPolicy : rest . referrerPolicy ,
997
+ }
998
+
981
999
const useLayoutEffect =
982
1000
typeof window === 'undefined' ? React . useEffect : React . useLayoutEffect
983
1001
const onLoadingCompleteRef = useRef ( onLoadingComplete )
@@ -1044,6 +1062,27 @@ export default function Image({
1044
1062
) : null }
1045
1063
< ImageElement { ...imgElementArgs } />
1046
1064
</ span >
1065
+ { ! supportsFloat && priority ? (
1066
+ // Note how we omit the `href` attribute, as it would only be relevant
1067
+ // for browsers that do not support `imagesrcset`, and in those cases
1068
+ // it would likely cause the incorrect image to be preloaded.
1069
+ //
1070
+ // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-imagesrcset
1071
+ < Head >
1072
+ < link
1073
+ key = {
1074
+ '__nimg-' +
1075
+ imgAttributes . src +
1076
+ imgAttributes . srcSet +
1077
+ imgAttributes . sizes
1078
+ }
1079
+ rel = "preload"
1080
+ as = "image"
1081
+ href = { imgAttributes . srcSet ? undefined : imgAttributes . src }
1082
+ { ...linkProps }
1083
+ />
1084
+ </ Head >
1085
+ ) : null }
1047
1086
</ >
1048
1087
)
1049
1088
}
0 commit comments