Fix next/image using layout=raw with priority (#37381)

Fixes a regression from #36985 which switched to native lazy loading but didn't account for the case when the user sets `priority` which changes the default behavior of `loading` prop.

See https://github.com/vercel/next.js/pull/36985#issuecomment-1132267710
This commit is contained in:
Steven 2022-06-03 21:54:22 -04:00 committed by GitHub
parent 95bd68d25a
commit 4cbc61c740
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View file

@ -457,7 +457,11 @@ export default function Image({
unoptimized = true
isLazy = false
}
if (typeof window !== 'undefined' && loadedImageURLs.has(src)) {
if (
typeof window !== 'undefined' &&
loadedImageURLs.has(src) &&
layout !== 'raw'
) {
isLazy = false
}
@ -901,7 +905,7 @@ const ImageElement = ({
blurStyle,
isLazy,
placeholder,
loading = 'lazy',
loading,
srcString,
config,
unoptimized,
@ -915,6 +919,7 @@ const ImageElement = ({
noscriptSizes,
...rest
}: ImageElementProps) => {
loading = isLazy ? 'lazy' : loading
return (
<>
<img

View file

@ -14,7 +14,7 @@ const Page = () => {
></Image>
<Image
loading="eager"
id="basic-image"
id="load-eager"
src="/test.png"
width="400"
height="400"
@ -37,7 +37,7 @@ const Page = () => {
/>
<Image
priority
id="raw"
id="raw1"
src="/test.webp"
width="1200"
height="700"

View file

@ -142,6 +142,23 @@ function runTests(mode) {
},
])
// When priority={true}, we should _not_ set loading="lazy"
expect(
await browser.elementById('basic-image').getAttribute('loading')
).toBe(null)
expect(
await browser.elementById('load-eager').getAttribute('loading')
).toBe(null)
expect(
await browser.elementById('responsive1').getAttribute('loading')
).toBe(null)
expect(
await browser.elementById('responsive2').getAttribute('loading')
).toBe(null)
expect(await browser.elementById('raw1').getAttribute('loading')).toBe(
null
)
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')