Unverified Commit 7cc41595 authored by Steven's avatar Steven Committed by GitHub

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 https://github.com/facebook/react/pull/25927
parent 23c9f0ee
...@@ -8,6 +8,7 @@ import React, { ...@@ -8,6 +8,7 @@ import React, {
useMemo, useMemo,
useState, useState,
forwardRef, forwardRef,
version,
} from 'react' } from 'react'
import Head from '../shared/lib/head' import Head from '../shared/lib/head'
import { getImageBlurSvg } from '../shared/lib/image-blur-svg' import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
...@@ -367,6 +368,23 @@ function handleLoading( ...@@ -367,6 +368,23 @@ function handleLoading(
}) })
} }
function getDynamicProps(
fetchPriority?: string
): Record<string, string | undefined> {
const [majorStr, minorStr] = version.split('.')
const major = parseInt(majorStr, 10)
const minor = parseInt(minorStr, 10)
if (major > 18 || (major === 18 && minor >= 3)) {
// In React 18.3.0 or newer, we must use camelCase
// prop to avoid "Warning: Invalid DOM property".
// See https://github.com/facebook/react/pull/25927
return { fetchPriority }
}
// In React 18.2.0 or older, we must use lowercase prop
// to avoid "Warning: Invalid DOM property".
return { fetchpriority: fetchPriority }
}
const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
( (
{ {
...@@ -401,10 +419,9 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( ...@@ -401,10 +419,9 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
<> <>
<img <img
{...rest} {...rest}
{...getDynamicProps(fetchPriority)}
// @ts-expect-error - TODO: upgrade to `@types/react@18` // @ts-expect-error - TODO: upgrade to `@types/react@18`
loading={loading} loading={loading}
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands
fetchpriority={fetchPriority}
width={widthInt} width={widthInt}
height={heightInt} height={heightInt}
decoding="async" decoding="async"
...@@ -959,8 +976,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -959,8 +976,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
imageSrcSet={imgAttributes.srcSet} imageSrcSet={imgAttributes.srcSet}
imageSizes={imgAttributes.sizes} imageSizes={imgAttributes.sizes}
crossOrigin={rest.crossOrigin} crossOrigin={rest.crossOrigin}
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands {...getDynamicProps(fetchPriority)}
fetchpriority={fetchPriority}
/> />
</Head> </Head>
) : null} ) : null}
......
...@@ -8,6 +8,7 @@ const Page = () => { ...@@ -8,6 +8,7 @@ const Page = () => {
<Image <Image
priority priority
id="basic-image" id="basic-image"
alt="basic-image"
src="/test.jpg" src="/test.jpg"
width="400" width="400"
height="400" height="400"
...@@ -15,6 +16,7 @@ const Page = () => { ...@@ -15,6 +16,7 @@ const Page = () => {
<Image <Image
priority priority
id="basic-image-crossorigin" id="basic-image-crossorigin"
alt="basic-image-crossorigin"
src="/test.jpg" src="/test.jpg"
width="400" width="400"
height="400" height="400"
...@@ -23,6 +25,7 @@ const Page = () => { ...@@ -23,6 +25,7 @@ const Page = () => {
<Image <Image
loading="eager" loading="eager"
id="load-eager" id="load-eager"
alt="load-eager"
src="/test.png" src="/test.png"
width="400" width="400"
height="400" height="400"
...@@ -30,6 +33,7 @@ const Page = () => { ...@@ -30,6 +33,7 @@ const Page = () => {
<Image <Image
priority priority
id="responsive1" id="responsive1"
alt="responsive1"
src="/wide.png" src="/wide.png"
width="1200" width="1200"
height="700" height="700"
...@@ -38,6 +42,7 @@ const Page = () => { ...@@ -38,6 +42,7 @@ const Page = () => {
<Image <Image
priority priority
id="responsive2" id="responsive2"
alt="responsive2"
src="/wide.png" src="/wide.png"
width="1200" width="1200"
height="700" height="700"
...@@ -45,6 +50,7 @@ const Page = () => { ...@@ -45,6 +50,7 @@ const Page = () => {
/> />
<Image <Image
id="pri-low" id="pri-low"
alt="pri-low"
src="/test.webp" src="/test.webp"
width="100" width="100"
height="100" height="100"
......
...@@ -183,6 +183,7 @@ function runTests(mode) { ...@@ -183,6 +183,7 @@ function runTests(mode) {
expect(warnings).not.toMatch( expect(warnings).not.toMatch(
/was detected as the Largest Contentful Paint/gm /was detected as the Largest Contentful Paint/gm
) )
expect(warnings).not.toMatch(/React does not recognize the (.+) prop/gm)
// should preload with crossorigin // should preload with crossorigin
expect( expect(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment