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/future/image blur-up placeholder #39785

Merged
merged 11 commits into from Aug 23, 2022
25 changes: 19 additions & 6 deletions packages/next/client/future/image.tsx
Expand Up @@ -759,20 +759,33 @@ export default function Image({
bottom: 0,
}
: {},
showAltText || placeholder === 'blur' ? {} : { color: 'transparent' },
showAltText ? {} : { color: 'transparent' },
style
)
const svgBlurPlaceholder = `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 ${widthInt} ${heightInt}'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='50'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='${blurDataURL}'/%3E%3C/svg%3E")`
const backgroundSize = imgStyle.objectFit || 'cover'
const backgroundPosition = imgStyle.objectPosition || '50% 50%'
const preserveAspectRatio =
backgroundSize === 'contain'
? 'xMidYMid'
: backgroundSize === 'cover'
? 'xMidYMid slice'
: 'none'
const svgBlurPlaceholder = `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 ${widthInt} ${heightInt}' preserveAspectRatio='${preserveAspectRatio}' %3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='50'/%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%25' width='100%25' href='${blurDataURL}'/%3E%3C/svg%3E")`
styfle marked this conversation as resolved.
Show resolved Hide resolved

const blurStyle =
placeholder === 'blur' && !blurComplete
placeholder === 'blur' && blurDataURL && !blurComplete
? {
backgroundSize: imgStyle.objectFit || 'cover',
backgroundPosition: imgStyle.objectPosition || '0% 0%',
...(blurDataURL?.startsWith('data:image')
backgroundSize,
backgroundPosition,
backgroundRepeat: 'no-repeat',
...(blurDataURL.startsWith('data:image/jpeg')
? {
backgroundImage: svgBlurPlaceholder,
}
: {
// Fallback to CSS filter in any of the following cases:
// if blurDataURL might be transparent (never true for jpeg)
// or if blurDataURL is an external url (always true for `next dev`)
filter: 'blur(20px)',
backgroundImage: `url("${blurDataURL}")`,
}),
Expand Down