Unverified Commit 73bffd6f authored by Steven's avatar Steven Committed by GitHub

Fix `next/future/image` blur-up placeholder (#39785)

This PR is a follow up to PR #39190 so that we can dynamically set the `feComponentTransfer` when we know the image doesn't have transparency (at this time its just jpeg).

We also set the stdDeviation to 1 and the viewbox to the placeholder's width/height to avoid any rounding issues.

Finally, we also fix the conversion from `objectPosition` to `backgroundPosition` because they have different default values according to the spec.
parent 17244b8a
...@@ -28,6 +28,8 @@ function nextImageLoader(content) { ...@@ -28,6 +28,8 @@ function nextImageLoader(content) {
getImageSize(content, extension) getImageSize(content, extension)
) )
let blurDataURL let blurDataURL
let blurWidth
let blurHeight
if (VALID_BLUR_EXT.includes(extension)) { if (VALID_BLUR_EXT.includes(extension)) {
if (isDev) { if (isDev) {
...@@ -39,18 +41,21 @@ function nextImageLoader(content) { ...@@ -39,18 +41,21 @@ function nextImageLoader(content) {
blurDataURL = url.href.slice(prefix.length) blurDataURL = url.href.slice(prefix.length)
} else { } else {
// Shrink the image's largest dimension // Shrink the image's largest dimension
const dimension = if (imageSize.width >= imageSize.height) {
imageSize.width >= imageSize.height ? 'width' : 'height' blurWidth = BLUR_IMG_SIZE
blurHeight = Math.round(
(imageSize.height / imageSize.width) * BLUR_IMG_SIZE
)
} else {
blurWidth = Math.round(
(imageSize.width / imageSize.height) * BLUR_IMG_SIZE
)
blurHeight = BLUR_IMG_SIZE
}
const resizeImageSpan = imageLoaderSpan.traceChild('image-resize') const resizeImageSpan = imageLoaderSpan.traceChild('image-resize')
const resizedImage = await resizeImageSpan.traceAsyncFn(() => const resizedImage = await resizeImageSpan.traceAsyncFn(() =>
resizeImage( resizeImage(content, blurWidth, blurHeight, extension, BLUR_QUALITY)
content,
dimension,
BLUR_IMG_SIZE,
extension,
BLUR_QUALITY
)
) )
const blurDataURLSpan = imageLoaderSpan.traceChild( const blurDataURLSpan = imageLoaderSpan.traceChild(
'image-base64-tostring' 'image-base64-tostring'
...@@ -70,6 +75,8 @@ function nextImageLoader(content) { ...@@ -70,6 +75,8 @@ function nextImageLoader(content) {
height: imageSize.height, height: imageSize.height,
width: imageSize.width, width: imageSize.width,
blurDataURL, blurDataURL,
blurWidth,
blurHeight,
}) })
) )
......
...@@ -67,6 +67,8 @@ export interface StaticImageData { ...@@ -67,6 +67,8 @@ export interface StaticImageData {
height: number height: number
width: number width: number
blurDataURL?: string blurDataURL?: string
blurWidth?: number
blurHeight?: number
} }
interface StaticRequire { interface StaticRequire {
...@@ -578,6 +580,8 @@ export default function Image({ ...@@ -578,6 +580,8 @@ export default function Image({
} }
let staticSrc = '' let staticSrc = ''
let blurWidth: number | undefined
let blurHeight: number | undefined
if (isStaticImport(src)) { if (isStaticImport(src)) {
const staticImageData = isStaticRequire(src) ? src.default : src const staticImageData = isStaticRequire(src) ? src.default : src
...@@ -588,6 +592,8 @@ export default function Image({ ...@@ -588,6 +592,8 @@ export default function Image({
)}` )}`
) )
} }
blurWidth = staticImageData.blurWidth
blurHeight = staticImageData.blurHeight
blurDataURL = blurDataURL || staticImageData.blurDataURL blurDataURL = blurDataURL || staticImageData.blurDataURL
staticSrc = staticImageData.src staticSrc = staticImageData.src
...@@ -785,16 +791,24 @@ export default function Image({ ...@@ -785,16 +791,24 @@ export default function Image({
bottom: 0, bottom: 0,
} }
: {}, : {},
showAltText || placeholder === 'blur' ? {} : { color: 'transparent' }, showAltText ? {} : { color: 'transparent' },
style 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 std = blurWidth && blurHeight ? '1' : '20'
const svgWidth = blurWidth || widthInt
const svgHeight = blurHeight || heightInt
const feComponentTransfer = blurDataURL?.startsWith('data:image/jpeg')
? `%3CfeComponentTransfer%3E%3CfeFuncA type='discrete' tableValues='1 1'/%3E%3C/feComponentTransfer%3E%`
: ''
const svgBlurPlaceholder = `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 ${svgWidth} ${svgHeight}'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='${std}'/%3E${feComponentTransfer}%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='${blurDataURL}'/%3E%3C/svg%3E")`
const blurStyle = const blurStyle =
placeholder === 'blur' && !blurComplete placeholder === 'blur' && blurDataURL && !blurComplete
? { ? {
backgroundSize: imgStyle.objectFit || 'cover', backgroundSize: imgStyle.objectFit || 'cover',
backgroundPosition: imgStyle.objectPosition || '0% 0%', backgroundPosition: imgStyle.objectPosition || '50% 50%',
...(blurDataURL?.startsWith('data:image') backgroundRepeat: 'no-repeat',
...(blurDataURL.startsWith('data:image') && svgWidth && svgHeight
? { ? {
backgroundImage: svgBlurPlaceholder, backgroundImage: svgBlurPlaceholder,
} }
......
...@@ -719,8 +719,8 @@ export function sendResponse( ...@@ -719,8 +719,8 @@ export function sendResponse(
export async function resizeImage( export async function resizeImage(
content: Buffer, content: Buffer,
dimension: 'width' | 'height', width: number,
size: number, height: number,
// Should match VALID_BLUR_EXT // Should match VALID_BLUR_EXT
extension: 'avif' | 'webp' | 'png' | 'jpeg', extension: 'avif' | 'webp' | 'png' | 'jpeg',
quality: number quality: number
...@@ -748,18 +748,11 @@ export async function resizeImage( ...@@ -748,18 +748,11 @@ export async function resizeImage(
} else if (extension === 'jpeg') { } else if (extension === 'jpeg') {
transformer.jpeg({ quality }) transformer.jpeg({ quality })
} }
if (dimension === 'width') { transformer.resize(width, height)
transformer.resize(size)
} else {
transformer.resize(null, size)
}
const buf = await transformer.toBuffer() const buf = await transformer.toBuffer()
return buf return buf
} else { } else {
const resizeOperationOpts: Operation = const resizeOperationOpts: Operation = { type: 'resize', width, height }
dimension === 'width'
? { type: 'resize', width: size }
: { type: 'resize', height: size }
const buf = await processBuffer( const buf = await processBuffer(
content, content,
[resizeOperationOpts], [resizeOperationOpts],
......
...@@ -62,6 +62,7 @@ export async function rotate( ...@@ -62,6 +62,7 @@ export async function rotate(
type ResizeOpts = { image: ImageData } & ( type ResizeOpts = { image: ImageData } & (
| { width: number; height?: never } | { width: number; height?: never }
| { height: number; width?: never } | { height: number; width?: never }
| { height: number; width: number }
) )
export async function resize({ image, width, height }: ResizeOpts) { export async function resize({ image, width, height }: ResizeOpts) {
......
...@@ -9,7 +9,11 @@ type RotateOperation = { ...@@ -9,7 +9,11 @@ type RotateOperation = {
} }
type ResizeOperation = { type ResizeOperation = {
type: 'resize' type: 'resize'
} & ({ width: number; height?: never } | { height: number; width?: never }) } & (
| { width: number; height?: never }
| { height: number; width?: never }
| { width: number; height: number }
)
export type Operation = RotateOperation | ResizeOperation export type Operation = RotateOperation | ResizeOperation
export type Encoding = 'jpeg' | 'png' | 'webp' | 'avif' export type Encoding = 'jpeg' | 'png' | 'webp' | 'avif'
...@@ -37,24 +41,24 @@ export async function processBuffer( ...@@ -37,24 +41,24 @@ export async function processBuffer(
if (operation.type === 'rotate') { if (operation.type === 'rotate') {
imageData = await worker.rotate(imageData, operation.numRotations) imageData = await worker.rotate(imageData, operation.numRotations)
} else if (operation.type === 'resize') { } else if (operation.type === 'resize') {
const opt = { image: imageData, width: 0, height: 0 }
if ( if (
operation.width && operation.width &&
imageData.width && imageData.width &&
imageData.width > operation.width imageData.width > operation.width
) { ) {
imageData = await worker.resize({ opt.width = operation.width
image: imageData, }
width: operation.width, if (
})
} else if (
operation.height && operation.height &&
imageData.height && imageData.height &&
imageData.height > operation.height imageData.height > operation.height
) { ) {
imageData = await worker.resize({ opt.height = operation.height
image: imageData, }
height: operation.height,
}) if (opt.width > 0 || opt.height > 0) {
imageData = await worker.resize(opt)
} }
} }
} }
......
...@@ -5,9 +5,11 @@ import { ...@@ -5,9 +5,11 @@ import {
nextStart, nextStart,
renderViaHTTP, renderViaHTTP,
File, File,
launchApp,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
...@@ -15,10 +17,11 @@ let appPort ...@@ -15,10 +17,11 @@ let appPort
let app let app
let browser let browser
let html let html
let $
const indexPage = new File(join(appDir, 'pages/static-img.js')) const indexPage = new File(join(appDir, 'pages/static-img.js'))
const runTests = () => { const runTests = (isDev) => {
it('Should allow an image with a static src to omit height and width', async () => { it('Should allow an image with a static src to omit height and width', async () => {
expect(await browser.elementById('basic-static')).toBeTruthy() expect(await browser.elementById('basic-static')).toBeTruthy()
expect(await browser.elementById('blur-png')).toBeTruthy() expect(await browser.elementById('blur-png')).toBeTruthy()
...@@ -31,42 +34,71 @@ const runTests = () => { ...@@ -31,42 +34,71 @@ const runTests = () => {
expect(await browser.elementById('static-ico')).toBeTruthy() expect(await browser.elementById('static-ico')).toBeTruthy()
expect(await browser.elementById('static-unoptimized')).toBeTruthy() expect(await browser.elementById('static-unoptimized')).toBeTruthy()
}) })
it('Should use immutable cache-control header for static import', async () => { if (!isDev) {
await browser.eval( // cache-control is set to "0, no-store" in dev mode
`document.getElementById("basic-static").scrollIntoView()` it('Should use immutable cache-control header for static import', async () => {
) await browser.eval(
await waitFor(1000) `document.getElementById("basic-static").scrollIntoView()`
const url = await browser.eval( )
`document.getElementById("basic-static").src` await waitFor(1000)
) const url = await browser.eval(
const res = await fetch(url) `document.getElementById("basic-static").src`
expect(res.headers.get('cache-control')).toBe( )
'public, max-age=315360000, immutable' const res = await fetch(url)
) expect(res.headers.get('cache-control')).toBe(
}) 'public, max-age=315360000, immutable'
it('Should use immutable cache-control header even when unoptimized', async () => { )
await browser.eval( })
`document.getElementById("static-unoptimized").scrollIntoView()`
) it('Should use immutable cache-control header even when unoptimized', async () => {
await waitFor(1000) await browser.eval(
const url = await browser.eval( `document.getElementById("static-unoptimized").scrollIntoView()`
`document.getElementById("static-unoptimized").src` )
) await waitFor(1000)
const res = await fetch(url) const url = await browser.eval(
expect(res.headers.get('cache-control')).toBe( `document.getElementById("static-unoptimized").src`
'public, max-age=31536000, immutable' )
) const res = await fetch(url)
}) expect(res.headers.get('cache-control')).toBe(
'public, max-age=31536000, immutable'
)
})
}
it('Should automatically provide an image height and width', async () => { it('Should automatically provide an image height and width', async () => {
expect(html).toContain('width="400" height="300"') const img = $('#basic-non-static')
expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('300')
}) })
it('Should allow provided width and height to override intrinsic', async () => { it('Should allow provided width and height to override intrinsic', async () => {
expect(html).toContain('width="150" height="150"') const img = $('#defined-size-static')
expect(img.attr('width')).toBe('150')
expect(img.attr('height')).toBe('150')
}) })
it('Should add a blur to a statically imported image', async () => {
expect(html).toContain( it('Should add a blur placeholder a statically imported jpg', async () => {
`style="background-size:cover;background-position:0% 0%;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 300'%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='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAYACAMBIgACEQEDEQH/xAAnAAEBAAAAAAAAAAAAAAAAAAAABwEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAAmgP/xAAcEAACAQUBAAAAAAAAAAAAAAASFBMAAQMFERX/2gAIAQEAAT8AZ1HjrKZX55JysIc4Ff/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Af//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Af//Z'/%3E%3C/svg%3E")` const style = $('#basic-static').attr('style')
) if (isDev) {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;filter:blur(20px);background-image:url("/docs/_next/image?url=%2Fdocs%2F_next%2Fstatic%2Fmedia%2Ftest-rect.f323a148.jpg&w=8&q=70")`
)
} else {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 8 6'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='1'/%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='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAYACAMBIgACEQEDEQH/xAAnAAEBAAAAAAAAAAAAAAAAAAAABwEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAAmgP/xAAcEAACAQUBAAAAAAAAAAAAAAASFBMAAQMFERX/2gAIAQEAAT8AZ1HjrKZX55JysIc4Ff/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Af//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Af//Z'/%3E%3C/svg%3E")`
)
}
})
it('Should add a blur placeholder a statically imported png', async () => {
const style = $('#blur-png').attr('style')
if (isDev) {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;filter:blur(20px);background-image:url("/docs/_next/image?url=%2Fdocs%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=8&q=70")`
)
} else {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='1'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAOklEQVR42iWGsQkAIBDE0iuIdiLOJjiGIzjiL/Meb4okiNYIlLjK3hJMzCQG1/0qmXXOUkjAV+m9wAMe3QiV6Ne8VgAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
)
}
}) })
} }
...@@ -90,15 +122,32 @@ describe('Build Error Tests', () => { ...@@ -90,15 +122,32 @@ describe('Build Error Tests', () => {
}) })
}) })
describe('Future Static Image Component Tests for basePath', () => { describe('Future Static Image Component Tests for basePath', () => {
beforeAll(async () => { describe('production mode', () => {
await nextBuild(appDir) beforeAll(async () => {
appPort = await findPort() await nextBuild(appDir)
app = await nextStart(appDir, appPort) appPort = await findPort()
html = await renderViaHTTP(appPort, '/docs/static-img') app = await nextStart(appDir, appPort)
browser = await webdriver(appPort, '/docs/static-img') html = await renderViaHTTP(appPort, '/docs/static-img')
$ = cheerio.load(html)
browser = await webdriver(appPort, '/docs/static-img')
})
afterAll(() => {
killApp(app)
})
runTests(false)
}) })
afterAll(() => {
killApp(app) describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
html = await renderViaHTTP(appPort, '/docs/static-img')
$ = cheerio.load(html)
browser = await webdriver(appPort, '/docs/static-img')
})
afterAll(() => {
killApp(app)
})
runTests(true)
}) })
runTests()
}) })
...@@ -32,15 +32,22 @@ const Page = () => { ...@@ -32,15 +32,22 @@ const Page = () => {
<Image id="blur-jpg" src={testJPG} placeholder="blur" /> <Image id="blur-jpg" src={testJPG} placeholder="blur" />
<Image id="blur-webp" src={testWEBP} placeholder="blur" /> <Image id="blur-webp" src={testWEBP} placeholder="blur" />
<Image id="blur-avif" src={testAVIF} placeholder="blur" /> <Image id="blur-avif" src={testAVIF} placeholder="blur" />
<br />
<Image id="static-svg" src={testSVG} /> <Image id="static-svg" src={testSVG} />
<Image id="static-gif" src={testGIF} /> <Image id="static-gif" src={testGIF} />
<Image id="static-bmp" src={testBMP} /> <Image id="static-bmp" src={testBMP} />
<Image id="static-ico" src={testICO} /> <Image id="static-ico" src={testICO} />
<br />
<Image id="static-svg-fill" src={testSVG} fill /> <Image id="static-svg-fill" src={testSVG} fill />
<Image id="static-gif-fill" src={testGIF} fill /> <Image id="static-gif-fill" src={testGIF} fill />
<Image id="static-bmp-fill" src={testBMP} fill /> <Image id="static-bmp-fill" src={testBMP} fill />
<Image id="static-ico-fill" src={testICO} fill /> <Image id="static-ico-fill" src={testICO} fill />
<br /> <br />
<Image id="blur-png-fill" src={testPNG} placeholder="blur" fill />
<Image id="blur-jpg-fill" src={testJPG} placeholder="blur" fill />
<Image id="blur-webp-fill" src={testWEBP} placeholder="blur" fill />
<Image id="blur-avif-fill" src={testAVIF} placeholder="blur" fill />
<br />
<Image id="static-unoptimized" src={testJPG} unoptimized /> <Image id="static-unoptimized" src={testJPG} unoptimized />
</div> </div>
) )
......
...@@ -536,7 +536,7 @@ function runTests(mode) { ...@@ -536,7 +536,7 @@ function runTests(mode) {
) )
expect(await browser.elementById('blur1').getAttribute('sizes')).toBeNull() expect(await browser.elementById('blur1').getAttribute('sizes')).toBeNull()
expect(await browser.elementById('blur1').getAttribute('style')).toMatch( expect(await browser.elementById('blur1').getAttribute('style')).toMatch(
'background-size:cover;background-position:0% 0%;' 'color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;'
) )
expect(await browser.elementById('blur1').getAttribute('height')).toBe( expect(await browser.elementById('blur1').getAttribute('height')).toBe(
'400' '400'
...@@ -559,7 +559,9 @@ function runTests(mode) { ...@@ -559,7 +559,9 @@ function runTests(mode) {
'lazy' 'lazy'
) )
expect(await browser.elementById('blur1').getAttribute('sizes')).toBeNull() expect(await browser.elementById('blur1').getAttribute('sizes')).toBeNull()
expect(await browser.elementById('blur1').getAttribute('style')).toMatch('') expect(await browser.elementById('blur1').getAttribute('style')).toBe(
'color: transparent;'
)
expect(await browser.elementById('blur1').getAttribute('height')).toBe( expect(await browser.elementById('blur1').getAttribute('height')).toBe(
'400' '400'
) )
...@@ -579,7 +581,7 @@ function runTests(mode) { ...@@ -579,7 +581,7 @@ function runTests(mode) {
'lazy' 'lazy'
) )
expect(await browser.elementById('blur2').getAttribute('style')).toMatch( expect(await browser.elementById('blur2').getAttribute('style')).toMatch(
'background-size:cover;background-position:0% 0%;' 'color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat'
) )
expect(await browser.elementById('blur2').getAttribute('height')).toBe( expect(await browser.elementById('blur2').getAttribute('height')).toBe(
'400' '400'
...@@ -604,7 +606,9 @@ function runTests(mode) { ...@@ -604,7 +606,9 @@ function runTests(mode) {
expect(await browser.elementById('blur2').getAttribute('loading')).toBe( expect(await browser.elementById('blur2').getAttribute('loading')).toBe(
'lazy' 'lazy'
) )
expect(await browser.elementById('blur2').getAttribute('style')).toBe('') expect(await browser.elementById('blur2').getAttribute('style')).toBe(
'color: transparent;'
)
expect(await browser.elementById('blur2').getAttribute('height')).toBe( expect(await browser.elementById('blur2').getAttribute('height')).toBe(
'400' '400'
) )
...@@ -1082,11 +1086,11 @@ function runTests(mode) { ...@@ -1082,11 +1086,11 @@ function runTests(mode) {
$html('noscript > img').attr('id', 'unused') $html('noscript > img').attr('id', 'unused')
expect($html('#blurry-placeholder-raw')[0].attribs.style).toContain( expect($html('#blurry-placeholder-raw')[0].attribs.style).toContain(
`background-size:cover;background-position:0% 0%;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%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='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8P4nhDwAGuAKPn6cicwAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")` `color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8P4nhDwAGuAKPn6cicwAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
) )
expect($html('#blurry-placeholder-with-lazy')[0].attribs.style).toContain( expect($html('#blurry-placeholder-with-lazy')[0].attribs.style).toContain(
`background-size:cover;background-position:0% 0%;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%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='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mO0/8/wBwAE/wI85bEJ6gAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")` `color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mO0/8/wBwAE/wI85bEJ6gAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
) )
}) })
...@@ -1096,7 +1100,7 @@ function runTests(mode) { ...@@ -1096,7 +1100,7 @@ function runTests(mode) {
const img = $html('noscript > img')[0] const img = $html('noscript > img')[0]
expect(img).toBeDefined() expect(img).toBeDefined()
expect(img.attribs.id).toBe('blurry-placeholder-raw') expect(img.attribs.id).toBe('blurry-placeholder-raw')
expect(img.attribs.style).toBeUndefined() expect(img.attribs.style).toBe('color:transparent')
}) })
it('should remove blurry placeholder after image loads', async () => { it('should remove blurry placeholder after image loads', async () => {
...@@ -1117,7 +1121,7 @@ function runTests(mode) { ...@@ -1117,7 +1121,7 @@ function runTests(mode) {
'background-image' 'background-image'
) )
).toBe( ).toBe(
`url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%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='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mO0/8/wBwAE/wI85bEJ6gAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")` `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 400 400'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='20'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mO0/8/wBwAE/wI85bEJ6gAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
) )
await browser.eval('document.getElementById("spacer").remove()') await browser.eval('document.getElementById("spacer").remove()')
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
launchApp, launchApp,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
...@@ -16,10 +17,11 @@ let appPort ...@@ -16,10 +17,11 @@ let appPort
let app let app
let browser let browser
let html let html
let $
const indexPage = new File(join(appDir, 'pages/static-img.js')) const indexPage = new File(join(appDir, 'pages/static-img.js'))
const runTests = (isDev = false) => { const runTests = (isDev) => {
it('Should allow an image with a static src to omit height and width', async () => { it('Should allow an image with a static src to omit height and width', async () => {
expect(await browser.elementById('basic-static')).toBeTruthy() expect(await browser.elementById('basic-static')).toBeTruthy()
expect(await browser.elementById('blur-png')).toBeTruthy() expect(await browser.elementById('blur-png')).toBeTruthy()
...@@ -68,22 +70,51 @@ const runTests = (isDev = false) => { ...@@ -68,22 +70,51 @@ const runTests = (isDev = false) => {
}) })
} }
it('Should automatically provide an image height and width', async () => { it('Should automatically provide an image height and width', async () => {
expect(html).toContain('width="400" height="300"') const img = $('#basic-non-static')
expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('300')
}) })
it('Should allow provided width and height to override intrinsic', async () => { it('Should allow provided width and height to override intrinsic', async () => {
expect(html).toContain('width="150" height="150"') const img = $('#defined-size-static')
expect(img.attr('width')).toBe('150')
expect(img.attr('height')).toBe('150')
}) })
it('Should add a blur to a statically imported image in "raw" mode', async () => { it('Should add a blur placeholder a statically imported jpg', async () => {
// next-image-loader uses different blur behavior for dev/prod mode const style = $('#basic-static').attr('style')
// See next/build/webpack/loaders/next-image-loader
if (isDev) { if (isDev) {
expect(html).toContain( expect(style).toBe(
`style="background-size:cover;background-position:0% 0%;filter:blur(20px);background-image:url(&quot;/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest-rect.f323a148.jpg&amp;w=8&amp;q=70&quot;)"` `color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;filter:blur(20px);background-image:url("/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest-rect.f323a148.jpg&w=8&q=70")`
) )
} else { } else {
expect(html).toContain( expect(style).toBe(
`style="background-size:cover;background-position:0% 0%;background-image:url(&quot;data:image/svg+xml;charset=utf-8,%3Csvg xmlns=&#x27;http%3A//www.w3.org/2000/svg&#x27; viewBox=&#x27;0 0 400 300&#x27;%3E%3Cfilter id=&#x27;b&#x27; color-interpolation-filters=&#x27;sRGB&#x27;%3E%3CfeGaussianBlur stdDeviation=&#x27;50&#x27;/%3E%3C/filter%3E%3Cimage filter=&#x27;url(%23b)&#x27; x=&#x27;0&#x27; y=&#x27;0&#x27; height=&#x27;100%25&#x27; width=&#x27;100%25&#x27; href=&#x27;data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAYACAMBIgACEQEDEQH/xAAnAAEBAAAAAAAAAAAAAAAAAAAABwEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAAmgP/xAAcEAACAQUBAAAAAAAAAAAAAAASFBMAAQMFERX/2gAIAQEAAT8AZ1HjrKZX55JysIc4Ff/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Af//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Af//Z&#x27;/%3E%3C/svg%3E&quot;)` `color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 8 6'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='1'/%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='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoKCgoKCgsMDAsPEA4QDxYUExMUFiIYGhgaGCIzICUgICUgMy03LCksNy1RQDg4QFFeT0pPXnFlZXGPiI+7u/sBCgoKCgoKCwwMCw8QDhAPFhQTExQWIhgaGBoYIjMgJSAgJSAzLTcsKSw3LVFAODhAUV5PSk9ecWVlcY+Ij7u7+//CABEIAAYACAMBIgACEQEDEQH/xAAnAAEBAAAAAAAAAAAAAAAAAAAABwEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAAmgP/xAAcEAACAQUBAAAAAAAAAAAAAAASFBMAAQMFERX/2gAIAQEAAT8AZ1HjrKZX55JysIc4Ff/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Af//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Af//Z'/%3E%3C/svg%3E")`
)
}
})
it('Should add a blur placeholder a statically imported png', async () => {
const style = $('#blur-png').attr('style')
if (isDev) {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;filter:blur(20px);background-image:url("/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=8&q=70")`
)
} else {
expect(style).toBe(
`color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='1'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAOklEQVR42iWGsQkAIBDE0iuIdiLOJjiGIzjiL/Meb4okiNYIlLjK3hJMzCQG1/0qmXXOUkjAV+m9wAMe3QiV6Ne8VgAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
)
}
})
it('Should add a blur placeholder a statically imported png with fill', async () => {
const style = $('#blur-png-fill').attr('style')
if (isDev) {
expect(style).toBe(
`position:absolute;height:100%;width:100%;left:0;top:0;right:0;bottom:0;color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;filter:blur(20px);background-image:url("/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=8&q=70")`
)
} else {
expect(style).toBe(
`position:absolute;height:100%;width:100%;left:0;top:0;right:0;bottom:0;color:transparent;background-size:cover;background-position:50% 50%;background-repeat:no-repeat;background-image:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http%3A//www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cfilter id='b' color-interpolation-filters='sRGB'%3E%3CfeGaussianBlur stdDeviation='1'/%3E%3C/filter%3E%3Cimage filter='url(%23b)' x='0' y='0' height='100%25' width='100%25' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAAOklEQVR42iWGsQkAIBDE0iuIdiLOJjiGIzjiL/Meb4okiNYIlLjK3hJMzCQG1/0qmXXOUkjAV+m9wAMe3QiV6Ne8VgAAAABJRU5ErkJggg=='/%3E%3C/svg%3E")`
) )
} }
}) })
...@@ -115,6 +146,7 @@ describe('Future Static Image Component Tests', () => { ...@@ -115,6 +146,7 @@ describe('Future Static Image Component Tests', () => {
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
html = await renderViaHTTP(appPort, '/static-img') html = await renderViaHTTP(appPort, '/static-img')
$ = cheerio.load(html)
browser = await webdriver(appPort, '/static-img') browser = await webdriver(appPort, '/static-img')
}) })
afterAll(() => { afterAll(() => {
...@@ -128,6 +160,7 @@ describe('Future Static Image Component Tests', () => { ...@@ -128,6 +160,7 @@ describe('Future Static Image Component Tests', () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
html = await renderViaHTTP(appPort, '/static-img') html = await renderViaHTTP(appPort, '/static-img')
$ = cheerio.load(html)
browser = await webdriver(appPort, '/static-img') browser = await webdriver(appPort, '/static-img')
}) })
afterAll(() => { afterAll(() => {
......
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