Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
9fb1e605
Unverified
Commit
9fb1e605
authored
Oct 22, 2020
by
Tim Neutkens
Committed by
GitHub
Oct 22, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make lazy-loading the default for next/image (#18123)
Co-authored-by:
Steven
<
steven@ceriously.com
>
parent
81e667bf
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
85 additions
and
26 deletions
+85
-26
image.tsx
packages/next/client/image.tsx
+35
-24
client-side.js
test/integration/image-component/basic/pages/client-side.js
+4
-0
index.js
test/integration/image-component/basic/pages/index.js
+4
-0
flex.js
test/integration/image-component/default/pages/flex.js
+19
-0
index.test.js
test/integration/image-component/default/test/index.test.js
+21
-0
index.test.js
...integration/image-component/typescript/test/index.test.js
+2
-2
No files found.
packages/next/client/image.tsx
View file @
9fb1e605
...
@@ -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
.
e
rror
(
throw
new
E
rror
(
`Image with src
${
src
}
has both priority and lazy tag
s. Only one should be used.`
`Image with src
"
${
src
}
" has both "priority" and "lazy" propertie
s. 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 attribut
es. Only one should be used.`
`Image with src
"
${
src
}
" has both "priority" and "unsized" properti
es. Only one should be used.`
)
)
}
}
}
}
}
else
{
}
else
{
// <Image src="i.png" />
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
console
.
e
rror
(
throw
new
E
rror
(
`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
>
)
)
}
}
...
...
test/integration/image-component/basic/pages/client-side.js
View file @
9fb1e605
...
@@ -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
}
/
>
/
>
...
...
test/integration/image-component/basic/pages/index.js
View file @
9fb1e605
...
@@ -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
}
/
>
/
>
...
...
test/integration/image-component/default/pages/flex.js
0 → 100644
View file @
9fb1e605
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
test/integration/image-component/default/test/index.test.js
View file @
9fb1e605
...
@@ -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'
,
()
=>
{
...
...
test/integration/image-component/typescript/test/index.test.js
View file @
9fb1e605
...
@@ -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
/
)
)
})
})
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment