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'> & { ...@@ -144,6 +144,7 @@ type ImageElementProps = Omit<ImageProps, 'src' | 'alt' | 'loader'> & {
imgStyle: ImgElementStyle imgStyle: ImgElementStyle
blurStyle: ImgElementStyle blurStyle: ImgElementStyle
isLazy: boolean isLazy: boolean
fetchPriority: string
fill?: boolean fill?: boolean
loading: LoadingValue loading: LoadingValue
config: ImageConfig config: ImageConfig
...@@ -377,6 +378,7 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( ...@@ -377,6 +378,7 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
imgStyle, imgStyle,
blurStyle, blurStyle,
isLazy, isLazy,
fetchPriority,
fill, fill,
placeholder, placeholder,
loading, loading,
...@@ -399,8 +401,10 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( ...@@ -399,8 +401,10 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
<> <>
<img <img
{...rest} {...rest}
// @ts-ignore - TODO: upgrade to `@types/react@17` // @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"
...@@ -513,6 +517,8 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -513,6 +517,8 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
onLoadingComplete, onLoadingComplete,
placeholder = 'empty', placeholder = 'empty',
blurDataURL, blurDataURL,
// @ts-expect-error - TODO: upgrade to `@types/react@18`
fetchPriority,
layout, layout,
objectFit, objectFit,
objectPosition, objectPosition,
...@@ -641,6 +647,9 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -641,6 +647,9 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
// through the built-in Image Optimization API. // through the built-in Image Optimization API.
unoptimized = true unoptimized = true
} }
if (priority) {
fetchPriority = 'high'
}
const [blurComplete, setBlurComplete] = useState(false) const [blurComplete, setBlurComplete] = useState(false)
const [showAltText, setShowAltText] = useState(false) const [showAltText, setShowAltText] = useState(false)
...@@ -891,16 +900,6 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -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) const onLoadRef = useRef(onLoad)
useEffect(() => { useEffect(() => {
...@@ -924,6 +923,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -924,6 +923,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
blurStyle, blurStyle,
loading, loading,
config, config,
fetchPriority,
fill, fill,
unoptimized, unoptimized,
placeholder, placeholder,
...@@ -955,7 +955,12 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -955,7 +955,12 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
rel="preload" rel="preload"
as="image" as="image"
href={imgAttributes.srcSet ? undefined : imgAttributes.src} 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> </Head>
) : null} ) : null}
......
...@@ -43,6 +43,13 @@ const Page = () => { ...@@ -43,6 +43,13 @@ const Page = () => {
height="700" height="700"
sizes="100vw" sizes="100vw"
/> />
<Image
id="pri-low"
src="/test.webp"
width="100"
height="100"
fetchPriority="low"
/>
<p id="stubtext">This is the priority page</p> <p id="stubtext">This is the priority page</p>
</div> </div>
) )
......
...@@ -121,17 +121,20 @@ function runTests(mode) { ...@@ -121,17 +121,20 @@ function runTests(mode) {
const links = await browser.elementsByCss('link[rel=preload][as=image]') const links = await browser.elementsByCss('link[rel=preload][as=image]')
const entries = [] const entries = []
for (const link of links) { for (const link of links) {
const fetchpriority = await link.getAttribute('fetchpriority')
const imagesrcset = await link.getAttribute('imagesrcset') const imagesrcset = await link.getAttribute('imagesrcset')
const imagesizes = await link.getAttribute('imagesizes') const imagesizes = await link.getAttribute('imagesizes')
entries.push({ imagesrcset, imagesizes }) entries.push({ fetchpriority, imagesrcset, imagesizes })
} }
expect(entries).toEqual([ expect(entries).toEqual([
{ {
fetchpriority: 'high',
imagesizes: '', imagesizes: '',
imagesrcset: imagesrcset:
'/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x', '/_next/image?url=%2Ftest.jpg&w=640&q=75 1x, /_next/image?url=%2Ftest.jpg&w=828&q=75 2x',
}, },
{ {
fetchpriority: 'high',
imagesizes: '100vw', imagesizes: '100vw',
imagesrcset: 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', '/_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) { ...@@ -152,6 +155,28 @@ function runTests(mode) {
await browser.elementById('responsive2').getAttribute('loading') await browser.elementById('responsive2').getAttribute('loading')
).toBe(null) ).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')) const warnings = (await browser.log('browser'))
.map((log) => log.message) .map((log) => log.message)
.join('\n') .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