Unverified Commit 5cd37d9b authored by Steven's avatar Steven Committed by GitHub

Fix `placeholder=blur` inside `<noscript>` (#27311)

We shouldn't be setting `placeholder=blur` styles when JS is disabled because we'll have no way to know when the image is loaded and it will be stuck in blur permanently as mentioned in [this comment](https://github.com/vercel/next.js/pull/19052#issuecomment-882886068).

This PR avoids blur styles on the `<noscript>` version of the image.
parent ddef873b
...@@ -458,16 +458,16 @@ export default function Image({ ...@@ -458,16 +458,16 @@ export default function Image({
objectFit, objectFit,
objectPosition, objectPosition,
}
...(placeholder === 'blur' const blurStyle =
placeholder === 'blur'
? { ? {
filter: 'blur(20px)', filter: 'blur(20px)',
backgroundSize: objectFit || 'cover', backgroundSize: objectFit || 'cover',
backgroundImage: `url("${blurDataURL}")`, backgroundImage: `url("${blurDataURL}")`,
backgroundPosition: objectPosition || '0% 0%', backgroundPosition: objectPosition || '0% 0%',
} }
: undefined), : {}
}
if (layout === 'fill') { if (layout === 'fill') {
// <Image src="i.png" layout="fill" /> // <Image src="i.png" layout="fill" />
wrapperStyle = { wrapperStyle = {
...@@ -607,7 +607,7 @@ export default function Image({ ...@@ -607,7 +607,7 @@ export default function Image({
setRef(img) setRef(img)
handleLoading(img, srcString, placeholder, onLoadingComplete) handleLoading(img, srcString, placeholder, onLoadingComplete)
}} }}
style={imgStyle} style={{ ...imgStyle, ...blurStyle }}
/> />
{priority ? ( {priority ? (
// Note how we omit the `href` attribute, as it would only be relevant // Note how we omit the `href` attribute, as it would only be relevant
......
...@@ -673,6 +673,8 @@ describe('Image Component Tests', () => { ...@@ -673,6 +673,8 @@ describe('Image Component Tests', () => {
const html = await renderViaHTTP(appPort, '/blurry-placeholder') const html = await renderViaHTTP(appPort, '/blurry-placeholder')
const $html = cheerio.load(html) const $html = cheerio.load(html)
$html('noscript > img').attr('id', 'unused')
expect($html('#blurry-placeholder')[0].attribs.style).toContain( expect($html('#blurry-placeholder')[0].attribs.style).toContain(
`background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400' viewBox='0 0 400 400'%3E%3Cfilter id='blur' filterUnits='userSpaceOnUse' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20' edgeMode='duplicate' /%3E%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1' /%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Cimage filter='url(%23blur)' href='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMDAwMDAwQEBAQFBQUFBQcHBgYHBwsICQgJCAsRCwwLCwwLEQ8SDw4PEg8bFRMTFRsfGhkaHyYiIiYwLTA+PlT/wAALCAAKAAoBAREA/8QAMwABAQEAAAAAAAAAAAAAAAAAAAcJEAABAwUAAwAAAAAAAAAAAAAFAAYRAQMEEyEVMlH/2gAIAQEAAD8Az1bLPaxhiuk0QdeCOLDtHixN2dmd2bsc5FPX7VTREX//2Q==' x='0' y='0' height='100%25' width='100%25'/%3E%3C/svg%3E")` `background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400' viewBox='0 0 400 400'%3E%3Cfilter id='blur' filterUnits='userSpaceOnUse' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20' edgeMode='duplicate' /%3E%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1' /%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Cimage filter='url(%23blur)' href='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMDAwMDAwQEBAQFBQUFBQcHBgYHBwsICQgJCAsRCwwLCwwLEQ8SDw4PEg8bFRMTFRsfGhkaHyYiIiYwLTA+PlT/wAALCAAKAAoBAREA/8QAMwABAQEAAAAAAAAAAAAAAAAAAAcJEAABAwUAAwAAAAAAAAAAAAAFAAYRAQMEEyEVMlH/2gAIAQEAAD8Az1bLPaxhiuk0QdeCOLDtHixN2dmd2bsc5FPX7VTREX//2Q==' x='0' y='0' height='100%25' width='100%25'/%3E%3C/svg%3E")`
) )
...@@ -690,6 +692,18 @@ describe('Image Component Tests', () => { ...@@ -690,6 +692,18 @@ describe('Image Component Tests', () => {
) )
}) })
it('should not use blurry placeholder for <noscript> image', async () => {
const html = await renderViaHTTP(appPort, '/blurry-placeholder')
const $html = cheerio.load(html)
const style = $html('noscript > img')[0].attribs.style
expect(style).not.toContain(`background-position`)
expect(style).not.toContain(`background-size`)
expect(style).not.toContain(
`background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400' viewBox='0 0 400 400'%3E%3Cfilter id='blur' filterUnits='userSpaceOnUse' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20' edgeMode='duplicate' /%3E%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1' /%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Cimage filter='url(%23blur)' href='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMDAwMDAwQEBAQFBQUFBQcHBgYHBwsICQgJCAsRCwwLCwwLEQ8SDw4PEg8bFRMTFRsfGhkaHyYiIiYwLTA+PlT/wAALCAAKAAoBAREA/8QAMwABAQEAAAAAAAAAAAAAAAAAAAcJEAABAwUAAwAAAAAAAAAAAAAFAAYRAQMEEyEVMlH/2gAIAQEAAD8Az1bLPaxhiuk0QdeCOLDtHixN2dmd2bsc5FPX7VTREX//2Q==' x='0' y='0' height='100%25' width='100%25'/%3E%3C/svg%3E")`
)
})
it('should remove blurry placeholder after image loads', async () => { it('should remove blurry placeholder after image loads', 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