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({
sizes,
unoptimized = false,
priority = false,
lazy = false,
lazy,
className,
quality,
width,
......@@ -156,11 +156,14 @@ export default function Image({
// If priority and lazy are present, log an error and use priority only.
if (priority && lazy) {
if (process.env.NODE_ENV !== 'production') {
console.error(
`Image with src ${src} has both priority and lazy tags. Only one should be used.`
throw new Error(
`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(() => {
......@@ -221,10 +224,15 @@ export default function Image({
let divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined
if (typeof height === 'number' && typeof width === 'number' && !unsized) {
// <Image src="i.png" width=100 height=100 />
const quotient = height / width
const ratio = isNaN(quotient) ? 1 : quotient * 100
wrapperStyle = {
maxWidth: '100%',
width,
}
divStyle = {
position: 'relative',
paddingBottom: `${ratio}%`,
......@@ -246,36 +254,39 @@ export default function Image({
if (priority) {
// <Image src="i.png" unsized priority />
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 {
// <Image src="i.png" />
if (process.env.NODE_ENV !== 'production') {
console.error(
`Image with src ${src} must use width and height attributes or unsized attribute.`
throw new Error(
`Image with src "${src}" must use "width" and "height" properties or "unsized" property.`
)
}
}
return (
<div style={divStyle}>
{shouldPreload
? generatePreload({
src,
widths: configSizes,
unoptimized,
sizes,
})
: ''}
<img
{...rest}
{...imgAttributes}
className={className}
sizes={sizes}
ref={thisEl}
style={imgStyle}
/>
<div style={wrapperStyle}>
<div style={divStyle}>
{shouldPreload
? generatePreload({
src,
widths: configSizes,
unoptimized,
sizes,
})
: ''}
<img
{...rest}
{...imgAttributes}
className={className}
sizes={sizes}
ref={thisEl}
style={imgStyle}
/>
</div>
</div>
)
}
......
......@@ -9,6 +9,7 @@ const ClientSide = () => {
<Image
id="basic-image"
src="foo.jpg"
lazy={false}
width={300}
height={400}
quality={60}
......@@ -17,6 +18,7 @@ const ClientSide = () => {
id="attribute-test"
data-demo="demo-value"
src="bar.jpg"
lazy={false}
width={300}
height={400}
/>
......@@ -25,6 +27,7 @@ const ClientSide = () => {
data-demo="demo-value"
host="secondary"
src="foo2.jpg"
lazy={false}
width={300}
height={400}
/>
......@@ -32,6 +35,7 @@ const ClientSide = () => {
id="unoptimized-image"
unoptimized
src="https://arbitraryurl.com/foo.jpg"
lazy={false}
width={300}
height={400}
/>
......
......@@ -9,6 +9,7 @@ const Page = () => {
<Image
id="basic-image"
src="foo.jpg"
lazy={false}
width={300}
height={400}
quality={60}
......@@ -17,6 +18,7 @@ const Page = () => {
id="attribute-test"
data-demo="demo-value"
src="bar.jpg"
lazy={false}
width={300}
height={400}
/>
......@@ -25,6 +27,7 @@ const Page = () => {
data-demo="demo-value"
host="secondary"
src="foo2.jpg"
lazy={false}
width={300}
height={400}
/>
......@@ -32,6 +35,7 @@ const Page = () => {
id="unoptimized-image"
unoptimized
src="https://arbitraryurl.com/foo.jpg"
lazy={false}
width={300}
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() {
}
}
})
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', () => {
......
......@@ -45,14 +45,14 @@ describe('TypeScript Image Component', () => {
const html = await renderViaHTTP(appPort, '/valid', {})
expect(html).toMatch(/This is valid usage of the Image component/)
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 () => {
await renderViaHTTP(appPort, '/invalid', {})
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