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

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

Merged
merged 5 commits into from Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 31 additions & 21 deletions packages/next/client/image.tsx
Expand Up @@ -142,7 +142,7 @@ export default function Image({
sizes,
unoptimized = false,
priority = false,
lazy = false,
lazy,
className,
quality,
width,
Expand All @@ -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(
styfle marked this conversation as resolved.
Show resolved Hide resolved
`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(() => {
Expand Down Expand Up @@ -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}%`,
Expand Down Expand Up @@ -259,23 +267,25 @@ export default function Image({
}

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>
)
}
Expand Down
19 changes: 19 additions & 0 deletions test/integration/image-component/default/pages/flex.js
@@ -0,0 +1,19 @@
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
21 changes: 21 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -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', () => {
Expand Down