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:
parent
95bd68d25a
commit
4cbc61c740
3 changed files with 26 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue