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
b05cdb1f
Unverified
Commit
b05cdb1f
authored
Aug 02, 2021
by
Steven
Committed by
GitHub
Aug 02, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add naturalWidth/naturalHeight to `onLoadingComplete()` callback (#27695)
Resolves #27213
parent
b3c959b2
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
71 additions
and
15 deletions
+71
-15
image.md
docs/api-reference/next/image.md
+5
-0
image.tsx
packages/next/client/image.tsx
+11
-3
test.svg
test/integration/image-component/base-path/public/test.svg
+1
-1
on-loading-complete.js
...tion/image-component/default/pages/on-loading-complete.js
+32
-6
test.svg
test/integration/image-component/default/public/test.svg
+1
-1
index.test.js
test/integration/image-component/default/test/index.test.js
+20
-3
test.svg
test/integration/image-optimizer/app/public/test.svg
+1
-1
No files found.
docs/api-reference/next/image.md
View file @
b05cdb1f
...
...
@@ -202,6 +202,11 @@ The image position when using `layout="fill"`.
A callback function that is invoked once the image is completely loaded and the
[
placeholder
](
#placeholder
)
has been removed.
The
`onLoadingComplete`
function accepts one parameter, an object with the following properties:
-
[
`naturalWidth`
](
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth
)
-
[
`naturalHeight`
](
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight
)
### loading
> **Attention**: This property is only meant for advanced usage. Switching an
...
...
packages/next/client/image.tsx
View file @
b05cdb1f
...
...
@@ -50,6 +50,11 @@ type LayoutValue = typeof VALID_LAYOUT_VALUES[number]
type
PlaceholderValue
=
'blur'
|
'empty'
type
OnLoadingComplete
=
(
result
:
{
naturalWidth
:
number
naturalHeight
:
number
})
=>
void
type
ImgElementStyle
=
NonNullable
<
JSX
.
IntrinsicElements
[
'img'
][
'style'
]
>
interface
StaticImageData
{
...
...
@@ -103,7 +108,7 @@ export type ImageProps = Omit<
unoptimized
?:
boolean
objectFit
?:
ImgElementStyle
[
'objectFit'
]
objectPosition
?:
ImgElementStyle
[
'objectPosition'
]
onLoadingComplete
?:
()
=>
void
onLoadingComplete
?:
OnLoadingComplete
}
const
{
...
...
@@ -249,7 +254,7 @@ function handleLoading(
img
:
HTMLImageElement
|
null
,
src
:
string
,
placeholder
:
PlaceholderValue
,
onLoadingComplete
?:
()
=>
void
onLoadingComplete
?:
OnLoadingComplete
)
{
if
(
!
img
)
{
return
...
...
@@ -265,7 +270,10 @@ function handleLoading(
}
loadedImageURLs
.
add
(
src
)
if
(
onLoadingComplete
)
{
onLoadingComplete
()
const
{
naturalWidth
,
naturalHeight
}
=
img
// Pass back read-only primitive values but not the
// underlying DOM element because it could be misused.
onLoadingComplete
({
naturalWidth
,
naturalHeight
})
}
})
}
...
...
test/integration/image-component/base-path/public/test.svg
View file @
b05cdb1f
...
...
@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg
version=
"1.0"
xmlns=
"http://www.w3.org/2000/svg"
width=
"400
.000000pt"
height=
"400.000000pt"
viewBox=
"0 0 400.000000 400.0000
00"
width=
"400
"
height=
"400"
viewBox=
"0 0 400 4
00"
preserveAspectRatio=
"xMidYMid meet"
>
<g
transform=
"translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill=
"#000000"
stroke=
"none"
>
...
...
test/integration/image-component/default/pages/on-loading-complete.js
View file @
b05cdb1f
...
...
@@ -4,27 +4,53 @@ import Image from 'next/image'
const
Page
=
()
=>
(
<
div
>
<
h1
>
On
Loading
Complete
Test
<
/h1
>
<
ImageWithMessage
id
=
"1"
src
=
"/test.jpg"
/>
<
ImageWithMessage
id
=
"1"
src
=
"/test.jpg"
layout
=
"intrinsic"
width
=
"128"
height
=
"128"
/>
<
ImageWithMessage
id
=
"2"
src
=
{
require
(
'../public/test.png'
)}
placeholder
=
"blur"
layout
=
"fixed"
/>
<
ImageWithMessage
id
=
"3"
src
=
"/test.svg"
layout
=
"responsive"
width
=
"1200"
height
=
"1200"
/>
<
div
style
=
{{
position
:
'relative'
,
width
:
'64px'
,
height
:
'64px'
}}
>
<
ImageWithMessage
id
=
"4"
src
=
"/test.ico"
layout
=
"fill"
objectFit
=
"contain"
/>
<
/div
>
<
div
id
=
"footer"
/>
<
/div
>
)
function
ImageWithMessage
({
id
,
src
})
{
function
ImageWithMessage
({
id
,
...
props
})
{
const
[
msg
,
setMsg
]
=
useState
(
'[LOADING]'
)
return
(
<>
<
Image
id
=
{
`img
${
id
}
`
}
src
=
{
src
}
width
=
"400"
height
=
"400"
onLoadingComplete
=
{()
=>
setMsg
(
`loaded img
${
id
}
`
)}
onLoadingComplete
=
{({
naturalWidth
,
naturalHeight
})
=>
setMsg
(
`loaded img
${
id
}
with dimensions
${
naturalWidth
}
x
${
naturalHeight
}
`
)
}
{...
props
}
/
>
<
p
id
=
{
`msg
${
id
}
`
}
>
{
msg
}
<
/p
>
<
hr
/>
<
/
>
)
}
...
...
test/integration/image-component/default/public/test.svg
View file @
b05cdb1f
...
...
@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg
version=
"1.0"
xmlns=
"http://www.w3.org/2000/svg"
width=
"400
.000000pt"
height=
"400.000000pt"
viewBox=
"0 0 400.000000 400.0000
00"
width=
"400
"
height=
"400"
viewBox=
"0 0 400 4
00"
preserveAspectRatio=
"xMidYMid meet"
>
<g
transform=
"translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill=
"#000000"
stroke=
"none"
>
...
...
test/integration/image-component/default/test/index.test.js
View file @
b05cdb1f
...
...
@@ -188,22 +188,39 @@ function runTests(mode) {
try
{
browser
=
await
webdriver
(
appPort
,
'/on-loading-complete'
)
await
browser
.
eval
(
`document.getElementById("footer").scrollIntoView()`
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("img1").src`
),
/test
(
.*
)
jpg/
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("img2").src`
),
/test
(
.*
)
.png/
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("img3").src`
),
/test
(
.*
)
svg/
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("img4").src`
),
/test
(
.*
)
ico/
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("msg1").textContent`
),
'loaded img1'
'loaded img1
with dimensions 128x128
'
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("msg2").textContent`
),
'loaded img2'
'loaded img2 with dimensions 400x400'
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("msg3").textContent`
),
'loaded img3 with dimensions 266x266'
)
await
check
(
()
=>
browser
.
eval
(
`document.getElementById("msg4").textContent`
),
'loaded img4 with dimensions 21x21'
)
}
finally
{
if
(
browser
)
{
...
...
test/integration/image-optimizer/app/public/test.svg
View file @
b05cdb1f
...
...
@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg
version=
"1.0"
xmlns=
"http://www.w3.org/2000/svg"
width=
"400
.000000pt"
height=
"400.000000pt"
viewBox=
"0 0 400.000000 400.0000
00"
width=
"400
"
height=
"400"
viewBox=
"0 0 400 4
00"
preserveAspectRatio=
"xMidYMid meet"
>
<g
transform=
"translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill=
"#000000"
stroke=
"none"
>
...
...
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