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: Data URL images with 'fill' are always triggering 'missing sizes' warning #42030

Merged
merged 4 commits into from Oct 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/next/client/image.tsx
Expand Up @@ -245,7 +245,8 @@ function handleLoading(
placeholder: PlaceholderValue,
onLoadRef: React.MutableRefObject<OnLoad | undefined>,
onLoadingCompleteRef: React.MutableRefObject<OnLoadingComplete | undefined>,
setBlurComplete: (b: boolean) => void
setBlurComplete: (b: boolean) => void,
unoptimized: boolean
) {
if (!img || img['data-loaded-src'] === src) {
return
Expand Down Expand Up @@ -296,8 +297,8 @@ function handleLoading(
if (process.env.NODE_ENV !== 'production') {
if (img.getAttribute('data-nimg') === 'fill') {
if (
!img.getAttribute('sizes') ||
img.getAttribute('sizes') === '100vw'
!unoptimized &&
(!img.getAttribute('sizes') || img.getAttribute('sizes') === '100vw')
) {
let widthViewportRatio =
img.getBoundingClientRect().width / window.innerWidth
Expand Down Expand Up @@ -407,7 +408,8 @@ const ImageElement = ({
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete
setBlurComplete,
unoptimized
)
}
},
Expand All @@ -418,6 +420,7 @@ const ImageElement = ({
onLoadingCompleteRef,
setBlurComplete,
onError,
unoptimized,
]
)}
onLoad={(event) => {
Expand All @@ -428,7 +431,8 @@ const ImageElement = ({
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete
setBlurComplete,
unoptimized
)
}}
onError={(event) => {
Expand Down
@@ -0,0 +1,16 @@
import React from 'react'
import Image from 'next/image'

export default function Page() {
return (
<div style={{ position: 'absolute', width: '200px', height: '200px' }}>
<p>Data Url With Fill And Sizes</p>
<Image
src="data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' %3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20'/%3E%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1'/%3E%3C/feComponentTransfer%3E%%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%' width='100%' preserveAspectRatio='none' href='data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAgDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAL/xAAeEAABBAIDAQAAAAAAAAAAAAACAAEDBAUhBhRRsf/EABQBAQAAAAAAAAAAAAAAAAAAAAL/xAAWEQEBAQAAAAAAAAAAAAAAAAABAgD/2gAMAwEAAhEDEQA/ALPk+UoW6g46eca0oGFouu7NoNE3m/iIiZEmLbv/2Q=='/%3E%3C/svg%3E"
alt="test"
fill
sizes="200px"
/>
</div>
)
}
11 changes: 11 additions & 0 deletions test/integration/next-image-new/default/test/index.test.ts
Expand Up @@ -861,6 +861,17 @@ function runTests(mode) {
)
})

it('should not warn when data url image with fill and sizes props', async () => {
const browser = await webdriver(appPort, '/data-url-with-fill-and-sizes')
const warnings = (await browser.log())
.map((log) => log.message)
.join('\n')
expect(await hasRedbox(browser)).toBe(false)
expect(warnings).not.toMatch(
/Image with src (.*) has "fill" but is missing "sizes" prop. Please add it to improve page performance/gm
)
})

it('should not warn when svg, even if with loader prop or without', async () => {
const browser = await webdriver(appPort, '/loader-svg')
await browser.eval(`document.querySelector("footer").scrollIntoView()`)
Expand Down