Unverified Commit 1aaae299 authored by Steven's avatar Steven Committed by GitHub

feat: add `fetchPriority` to `next/image` (#47222)

- Closes #43096 
- Available since Chrome 101:
https://chromestatus.com/feature/5273474901737472
- Coming to Safari: https://bugs.webkit.org/show_bug.cgi?id=252739
- Introduction blog: https://web.dev/priority-hints/


fix NEXT-319 ([link](https://linear.app/vercel/issue/NEXT-319))
parent ad223c81
......@@ -144,6 +144,7 @@ type ImageElementProps = Omit<ImageProps, 'src' | 'alt' | 'loader'> & {
imgStyle: ImgElementStyle
blurStyle: ImgElementStyle
isLazy: boolean
fetchPriority: string
fill?: boolean
loading: LoadingValue
config: ImageConfig
......@@ -377,6 +378,7 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
imgStyle,
blurStyle,
isLazy,
fetchPriority,
fill,
placeholder,
loading,
......@@ -399,8 +401,10 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
<>
<img
{...rest}
// @ts-ignore - TODO: upgrade to `@types/react@17`
// @ts-expect-error - TODO: upgrade to `@types/react@18`
loading={loading}
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands
fetchpriority={fetchPriority}
width={widthInt}
height={heightInt}
decoding="async"
......@@ -513,6 +517,8 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
onLoadingComplete,
placeholder = 'empty',
blurDataURL,
// @ts-expect-error - TODO: upgrade to `@types/react@18`
fetchPriority,
layout,
objectFit,
objectPosition,
......@@ -641,6 +647,9 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
// through the built-in Image Optimization API.
unoptimized = true
}
if (priority) {
fetchPriority = 'high'
}
const [blurComplete, setBlurComplete] = useState(false)
const [showAltText, setShowAltText] = useState(false)
......@@ -891,16 +900,6 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
}
}
const linkProps: React.DetailedHTMLProps<
React.LinkHTMLAttributes<HTMLLinkElement>,
HTMLLinkElement
> = {
// @ts-expect-error upgrade react types to react 18
imageSrcSet: imgAttributes.srcSet,
imageSizes: imgAttributes.sizes,
crossOrigin: rest.crossOrigin,
}
const onLoadRef = useRef(onLoad)
useEffect(() => {
......@@ -924,6 +923,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
blurStyle,
loading,
config,
fetchPriority,
fill,
unoptimized,
placeholder,
......@@ -955,7 +955,12 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
rel="preload"
as="image"
href={imgAttributes.srcSet ? undefined : imgAttributes.src}
{...linkProps}
// @ts-expect-error - TODO: upgrade to `@types/react@18`
imageSrcSet={imgAttributes.srcSet}
imageSizes={imgAttributes.sizes}
crossOrigin={rest.crossOrigin}
// Keep lowercase until https://github.com/facebook/react/pull/25927 lands
fetchpriority={fetchPriority}
/>
</Head>
) : null}
......
......@@ -43,6 +43,13 @@ const Page = () => {
height="700"
sizes="100vw"
/>
<Image
id="pri-low"
src="/test.webp"
width="100"
height="100"
fetchPriority="low"
/>
<p id="stubtext">This is the priority page</p>
</div>
)
......
......@@ -121,17 +121,20 @@ function runTests(mode) {
const links = await browser.elementsByCss('link[rel=preload][as=image]')
const entries = []
for (const link of links) {
const fetchpriority = await link.getAttribute('fetchpriority')
const imagesrcset = await link.getAttribute('imagesrcset')
const imagesizes = await link.getAttribute('imagesizes')
entries.push({ imagesrcset, imagesizes })
entries.push({ fetchpriority, imagesrcset, imagesizes })
}
expect(entries).toEqual([
{
fetchpriority: 'high',
imagesizes: '',
imagesrcset:
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x',
},
{
fetchpriority: 'high',
imagesizes: '100vw',
imagesrcset:
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w',
......@@ -152,6 +155,28 @@ function runTests(mode) {
await browser.elementById('responsive2').getAttribute('loading')
).toBe(null)
// When priority={true}, we should set fetchpriority="high"
expect(
await browser.elementById('basic-image').getAttribute('fetchpriority')
).toBe('high')
expect(
await browser.elementById('load-eager').getAttribute('fetchpriority')
).toBe(null)
expect(
await browser.elementById('responsive1').getAttribute('fetchpriority')
).toBe('high')
expect(
await browser.elementById('responsive2').getAttribute('fetchpriority')
).toBe('high')
// Setting fetchPriority="low" directly should pass-through to <img>
expect(
await browser.elementById('pri-low').getAttribute('fetchpriority')
).toBe('low')
expect(await browser.elementById('pri-low').getAttribute('loading')).toBe(
'lazy'
)
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
......
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