Unverified Commit 9fb1e605 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Make lazy-loading the default for next/image (#18123)

Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 81e667bf
...@@ -142,7 +142,7 @@ export default function Image({ ...@@ -142,7 +142,7 @@ export default function Image({
sizes, sizes,
unoptimized = false, unoptimized = false,
priority = false, priority = false,
lazy = false, lazy,
className, className,
quality, quality,
width, width,
...@@ -156,11 +156,14 @@ export default function Image({ ...@@ -156,11 +156,14 @@ export default function Image({
// If priority and lazy are present, log an error and use priority only. // If priority and lazy are present, log an error and use priority only.
if (priority && lazy) { if (priority && lazy) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.error( throw new Error(
`Image with src ${src} has both priority and lazy tags. Only one should be used.` `Image with src "${src}" has both "priority" and "lazy" properties. Only one should be used.`
) )
} }
lazy = false }
if (!priority && typeof lazy === 'undefined') {
lazy = true
} }
useEffect(() => { useEffect(() => {
...@@ -221,10 +224,15 @@ export default function Image({ ...@@ -221,10 +224,15 @@ export default function Image({
let divStyle: React.CSSProperties | undefined let divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined
if (typeof height === 'number' && typeof width === 'number' && !unsized) { if (typeof height === 'number' && typeof width === 'number' && !unsized) {
// <Image src="i.png" width=100 height=100 /> // <Image src="i.png" width=100 height=100 />
const quotient = height / width const quotient = height / width
const ratio = isNaN(quotient) ? 1 : quotient * 100 const ratio = isNaN(quotient) ? 1 : quotient * 100
wrapperStyle = {
maxWidth: '100%',
width,
}
divStyle = { divStyle = {
position: 'relative', position: 'relative',
paddingBottom: `${ratio}%`, paddingBottom: `${ratio}%`,
...@@ -246,36 +254,39 @@ export default function Image({ ...@@ -246,36 +254,39 @@ export default function Image({
if (priority) { if (priority) {
// <Image src="i.png" unsized priority /> // <Image src="i.png" unsized priority />
console.warn( console.warn(
`Image with src ${src} has both priority and unsized attributes. Only one should be used.` `Image with src "${src}" has both "priority" and "unsized" properties. Only one should be used.`
) )
} }
} }
} else { } else {
// <Image src="i.png" />
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.error( throw new Error(
`Image with src ${src} must use width and height attributes or unsized attribute.` `Image with src "${src}" must use "width" and "height" properties or "unsized" property.`
) )
} }
} }
return ( return (
<div style={divStyle}> <div style={wrapperStyle}>
{shouldPreload <div style={divStyle}>
? generatePreload({ {shouldPreload
src, ? generatePreload({
widths: configSizes, src,
unoptimized, widths: configSizes,
sizes, unoptimized,
}) sizes,
: ''} })
<img : ''}
{...rest} <img
{...imgAttributes} {...rest}
className={className} {...imgAttributes}
sizes={sizes} className={className}
ref={thisEl} sizes={sizes}
style={imgStyle} ref={thisEl}
/> style={imgStyle}
/>
</div>
</div> </div>
) )
} }
......
...@@ -9,6 +9,7 @@ const ClientSide = () => { ...@@ -9,6 +9,7 @@ const ClientSide = () => {
<Image <Image
id="basic-image" id="basic-image"
src="foo.jpg" src="foo.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
quality={60} quality={60}
...@@ -17,6 +18,7 @@ const ClientSide = () => { ...@@ -17,6 +18,7 @@ const ClientSide = () => {
id="attribute-test" id="attribute-test"
data-demo="demo-value" data-demo="demo-value"
src="bar.jpg" src="bar.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
...@@ -25,6 +27,7 @@ const ClientSide = () => { ...@@ -25,6 +27,7 @@ const ClientSide = () => {
data-demo="demo-value" data-demo="demo-value"
host="secondary" host="secondary"
src="foo2.jpg" src="foo2.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
...@@ -32,6 +35,7 @@ const ClientSide = () => { ...@@ -32,6 +35,7 @@ const ClientSide = () => {
id="unoptimized-image" id="unoptimized-image"
unoptimized unoptimized
src="https://arbitraryurl.com/foo.jpg" src="https://arbitraryurl.com/foo.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
......
...@@ -9,6 +9,7 @@ const Page = () => { ...@@ -9,6 +9,7 @@ const Page = () => {
<Image <Image
id="basic-image" id="basic-image"
src="foo.jpg" src="foo.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
quality={60} quality={60}
...@@ -17,6 +18,7 @@ const Page = () => { ...@@ -17,6 +18,7 @@ const Page = () => {
id="attribute-test" id="attribute-test"
data-demo="demo-value" data-demo="demo-value"
src="bar.jpg" src="bar.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
...@@ -25,6 +27,7 @@ const Page = () => { ...@@ -25,6 +27,7 @@ const Page = () => {
data-demo="demo-value" data-demo="demo-value"
host="secondary" host="secondary"
src="foo2.jpg" src="foo2.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
...@@ -32,6 +35,7 @@ const Page = () => { ...@@ -32,6 +35,7 @@ const Page = () => {
id="unoptimized-image" id="unoptimized-image"
unoptimized unoptimized
src="https://arbitraryurl.com/foo.jpg" src="https://arbitraryurl.com/foo.jpg"
lazy={false}
width={300} width={300}
height={400} height={400}
/> />
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<div>
<p>Hello World</p>
<Image id="basic-image" src="/test.jpg" width={400} height={400}></Image>
<p id="stubtext">This is the index page</p>
<style jsx>{`
div {
display: flex;
}
`}</style>
</div>
)
}
export default Page
...@@ -52,6 +52,27 @@ function runTests() { ...@@ -52,6 +52,27 @@ function runTests() {
} }
} }
}) })
it('should work when using flexbox', async () => {
let browser
try {
browser = await webdriver(appPort, '/flex')
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').width`
)
if (result === 0) {
throw new Error('Incorrectly loaded image')
}
return 'result-correct'
}, /result-correct/)
} finally {
if (browser) {
await browser.close()
}
}
})
} }
describe('Image Component Tests', () => { describe('Image Component Tests', () => {
......
...@@ -45,14 +45,14 @@ describe('TypeScript Image Component', () => { ...@@ -45,14 +45,14 @@ describe('TypeScript Image Component', () => {
const html = await renderViaHTTP(appPort, '/valid', {}) const html = await renderViaHTTP(appPort, '/valid', {})
expect(html).toMatch(/This is valid usage of the Image component/) expect(html).toMatch(/This is valid usage of the Image component/)
expect(output).not.toMatch( expect(output).not.toMatch(
/must use width and height attributes or unsized attribute/ /must use "width" and "height" properties or "unsized" property/
) )
}) })
it('should print error when invalid Image usage', async () => { it('should print error when invalid Image usage', async () => {
await renderViaHTTP(appPort, '/invalid', {}) await renderViaHTTP(appPort, '/invalid', {})
expect(output).toMatch( expect(output).toMatch(
/must use width and height attributes or unsized attribute/ /must use "width" and "height" properties or "unsized" property/
) )
}) })
}) })
......
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