Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -901,7 +905,7 @@ const ImageElement = ({
blurStyle,
isLazy,
placeholder,
loading = 'lazy',
loading,
srcString,
config,
unoptimized,
Expand All @@ -915,6 +919,7 @@ const ImageElement = ({
noscriptSizes,
...rest
}: ImageElementProps) => {
loading = isLazy ? 'lazy' : loading
return (
<>
<img
Expand Down
4 changes: 2 additions & 2 deletions test/integration/image-component/default/pages/priority.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Page = () => {
></Image>
<Image
loading="eager"
id="basic-image"
id="load-eager"
src="/test.png"
width="400"
height="400"
Expand All @@ -37,7 +37,7 @@ const Page = () => {
/>
<Image
priority
id="raw"
id="raw1"
src="/test.webp"
width="1200"
height="700"
Expand Down
17 changes: 17 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ function runTests(mode) {
},
])

// When priority={true}, we should _not_ set loading="lazy"
expect(
await browser.elementById('basic-image').getAttribute('loading')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename the ID to be more semantic (since this isn't really a basic image)

).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')
Expand Down