Unverified Commit 29b591b2 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

fix(next/image): do not pass-through srcSrc on lazy image (#20651)

This PR fixes a bug where we'd accidentally pass-through the user-provided `srcSet` if the image was lazy, just to then replace it when we hydrate.

---

Fixes #19041
parent 088f374c
...@@ -130,10 +130,11 @@ type GenImgAttrsData = { ...@@ -130,10 +130,11 @@ type GenImgAttrsData = {
sizes?: string sizes?: string
} }
type GenImgAttrsResult = Pick< type GenImgAttrsResult = {
JSX.IntrinsicElements['img'], src: string
'src' | 'sizes' | 'srcSet' srcSet: string | undefined
> sizes: string | undefined
}
function generateImgAttrs({ function generateImgAttrs({
src, src,
...@@ -144,7 +145,7 @@ function generateImgAttrs({ ...@@ -144,7 +145,7 @@ function generateImgAttrs({
sizes, sizes,
}: GenImgAttrsData): GenImgAttrsResult { }: GenImgAttrsData): GenImgAttrsResult {
if (unoptimized) { if (unoptimized) {
return { src } return { src, srcSet: undefined, sizes: undefined }
} }
const { widths, kind } = getWidths(width, layout) const { widths, kind } = getWidths(width, layout)
...@@ -364,6 +365,8 @@ export default function Image({ ...@@ -364,6 +365,8 @@ export default function Image({
let imgAttributes: GenImgAttrsResult = { let imgAttributes: GenImgAttrsResult = {
src: src:
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
srcSet: undefined,
sizes: undefined,
} }
if (isVisible) { if (isVisible) {
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<div>
<p>Drop srcSet prop (cannot be manually provided)</p>
<Image
src="/moving-truck.jpg"
width={300}
height={100}
srcSet="/moving-truck-mobile.jpg 375w,
/moving-truck-mobile.jpg 640w,
/moving-truck.jpg"
sizes="(max-width: 375px) 375px, 100%"
/>
<p>Assign sizes prop</p>
</div>
)
}
export default Page
/* eslint-env jest */ /* eslint-env jest */
import cheerio from 'cheerio'
import fs from 'fs-extra' import fs from 'fs-extra'
import { import {
check, check,
...@@ -10,6 +11,7 @@ import { ...@@ -10,6 +11,7 @@ import {
launchApp, launchApp,
nextBuild, nextBuild,
nextStart, nextStart,
renderViaHTTP,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
...@@ -154,6 +156,19 @@ function runTests(mode) { ...@@ -154,6 +156,19 @@ function runTests(mode) {
} }
}) })
it('should not pass through user-provided srcset (causing a flash)', async () => {
const html = await renderViaHTTP(appPort, '/drop-srcset')
const $html = cheerio.load(html)
const els = [].slice.apply($html('img'))
expect(els.length).toBe(1)
const [el] = els
expect(el.attribs.src).toBeDefined()
expect(el.attribs.srcset).toBeUndefined()
expect(el.attribs.srcSet).toBeUndefined()
})
it('should update the image on src change', async () => { it('should update the image on src change', async () => {
let browser let browser
try { try {
......
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