Skip to content

Commit 7cc4159

Browse files
authored
fix: handle different cases of React fetchPriority (#47302)
In React 18.3.0 or newer, we must user camelCase `fetchPriority` prop to avoid "Warning: Invalid DOM property". In React 18.2.0 and older, we must use the lowercase `fetchpriority` prop to avoid "Warning: Invalid DOM property". See facebook/react#25927
1 parent 23c9f0e commit 7cc4159

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

packages/next/src/client/image.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
useMemo,
99
useState,
1010
forwardRef,
11+
version,
1112
} from 'react'
1213
import Head from '../shared/lib/head'
1314
import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
@@ -367,6 +368,23 @@ function handleLoading(
367368
})
368369
}
369370

371+
function getDynamicProps(
372+
fetchPriority?: string
373+
): Record<string, string | undefined> {
374+
const [majorStr, minorStr] = version.split('.')
375+
const major = parseInt(majorStr, 10)
376+
const minor = parseInt(minorStr, 10)
377+
if (major > 18 || (major === 18 && minor >= 3)) {
378+
// In React 18.3.0 or newer, we must use camelCase
379+
// prop to avoid "Warning: Invalid DOM property".
380+
// See https://github.com/facebook/react/pull/25927
381+
return { fetchPriority }
382+
}
383+
// In React 18.2.0 or older, we must use lowercase prop
384+
// to avoid "Warning: Invalid DOM property".
385+
return { fetchpriority: fetchPriority }
386+
}
387+
370388
const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
371389
(
372390
{
@@ -401,10 +419,9 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
401419
<>
402420
<img
403421
{...rest}
422+
{...getDynamicProps(fetchPriority)}
404423
// @ts-expect-error - TODO: upgrade to `@types/react@18`
405424
loading={loading}
406-
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands
407-
fetchpriority={fetchPriority}
408425
width={widthInt}
409426
height={heightInt}
410427
decoding="async"
@@ -959,8 +976,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
959976
imageSrcSet={imgAttributes.srcSet}
960977
imageSizes={imgAttributes.sizes}
961978
crossOrigin={rest.crossOrigin}
962-
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands
963-
fetchpriority={fetchPriority}
979+
{...getDynamicProps(fetchPriority)}
964980
/>
965981
</Head>
966982
) : null}

test/integration/next-image-new/default/pages/priority.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ const Page = () => {
88
<Image
99
priority
1010
id="basic-image"
11+
alt="basic-image"
1112
src="/test.jpg"
1213
width="400"
1314
height="400"
1415
></Image>
1516
<Image
1617
priority
1718
id="basic-image-crossorigin"
19+
alt="basic-image-crossorigin"
1820
src="/test.jpg"
1921
width="400"
2022
height="400"
@@ -23,13 +25,15 @@ const Page = () => {
2325
<Image
2426
loading="eager"
2527
id="load-eager"
28+
alt="load-eager"
2629
src="/test.png"
2730
width="400"
2831
height="400"
2932
></Image>
3033
<Image
3134
priority
3235
id="responsive1"
36+
alt="responsive1"
3337
src="/wide.png"
3438
width="1200"
3539
height="700"
@@ -38,13 +42,15 @@ const Page = () => {
3842
<Image
3943
priority
4044
id="responsive2"
45+
alt="responsive2"
4146
src="/wide.png"
4247
width="1200"
4348
height="700"
4449
sizes="100vw"
4550
/>
4651
<Image
4752
id="pri-low"
53+
alt="pri-low"
4854
src="/test.webp"
4955
width="100"
5056
height="100"

test/integration/next-image-new/default/test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ function runTests(mode) {
183183
expect(warnings).not.toMatch(
184184
/was detected as the Largest Contentful Paint/gm
185185
)
186+
expect(warnings).not.toMatch(/React does not recognize the (.+) prop/gm)
186187

187188
// should preload with crossorigin
188189
expect(

0 commit comments

Comments
 (0)