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
f8c32e55
Unverified
Commit
f8c32e55
authored
Nov 01, 2020
by
Steven
Committed by
GitHub
Nov 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add all deviceSizes to srcset for responsive and fill (#18594)
parent
9569f5a3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
19 deletions
+78
-19
image.tsx
packages/next/client/image.tsx
+30
-10
layout-responsive.js
...ration/image-component/default/pages/layout-responsive.js
+4
-4
index.test.js
test/integration/image-component/default/test/index.test.js
+44
-5
No files found.
packages/next/client/image.tsx
View file @
f8c32e55
...
@@ -105,8 +105,15 @@ function unLazifyImage(lazyImage: HTMLImageElement): void {
...
@@ -105,8 +105,15 @@ function unLazifyImage(lazyImage: HTMLImageElement): void {
lazyImage
.
classList
.
remove
(
'__lazy'
)
lazyImage
.
classList
.
remove
(
'__lazy'
)
}
}
function
getDeviceSizes
(
width
:
number
|
undefined
):
number
[]
{
function
getDeviceSizes
(
if
(
typeof
width
!==
'number'
)
{
width
:
number
|
undefined
,
layout
:
LayoutValue
):
number
[]
{
if
(
typeof
width
!==
'number'
||
layout
===
'fill'
||
layout
===
'responsive'
)
{
return
configDeviceSizes
return
configDeviceSizes
}
}
if
(
configImageSizes
.
includes
(
width
))
{
if
(
configImageSizes
.
includes
(
width
))
{
...
@@ -125,13 +132,14 @@ function getDeviceSizes(width: number | undefined): number[] {
...
@@ -125,13 +132,14 @@ function getDeviceSizes(width: number | undefined): number[] {
function
computeSrc
(
function
computeSrc
(
src
:
string
,
src
:
string
,
unoptimized
:
boolean
,
unoptimized
:
boolean
,
layout
:
LayoutValue
,
width
?:
number
,
width
?:
number
,
quality
?:
number
quality
?:
number
):
string
{
):
string
{
if
(
unoptimized
)
{
if
(
unoptimized
)
{
return
src
return
src
}
}
const
widths
=
getDeviceSizes
(
width
)
const
widths
=
getDeviceSizes
(
width
,
layout
)
const
largest
=
widths
[
widths
.
length
-
1
]
const
largest
=
widths
[
widths
.
length
-
1
]
return
callLoader
({
src
,
width
:
largest
,
quality
})
return
callLoader
({
src
,
width
:
largest
,
quality
})
}
}
...
@@ -150,6 +158,7 @@ function callLoader(loaderProps: CallLoaderProps) {
...
@@ -150,6 +158,7 @@ function callLoader(loaderProps: CallLoaderProps) {
type
SrcSetData
=
{
type
SrcSetData
=
{
src
:
string
src
:
string
unoptimized
:
boolean
unoptimized
:
boolean
layout
:
LayoutValue
width
?:
number
width
?:
number
quality
?:
number
quality
?:
number
}
}
...
@@ -157,6 +166,7 @@ type SrcSetData = {
...
@@ -157,6 +166,7 @@ type SrcSetData = {
function
generateSrcSet
({
function
generateSrcSet
({
src
,
src
,
unoptimized
,
unoptimized
,
layout
,
width
,
width
,
quality
,
quality
,
}:
SrcSetData
):
string
|
undefined
{
}:
SrcSetData
):
string
|
undefined
{
...
@@ -166,7 +176,7 @@ function generateSrcSet({
...
@@ -166,7 +176,7 @@ function generateSrcSet({
return
undefined
return
undefined
}
}
return
getDeviceSizes
(
width
)
return
getDeviceSizes
(
width
,
layout
)
.
map
((
w
)
=>
`
${
callLoader
({
src
,
width
:
w
,
quality
})}
${
w
}
w`
)
.
map
((
w
)
=>
`
${
callLoader
({
src
,
width
:
w
,
quality
})}
${
w
}
w`
)
.
join
(
', '
)
.
join
(
', '
)
}
}
...
@@ -174,6 +184,7 @@ function generateSrcSet({
...
@@ -174,6 +184,7 @@ function generateSrcSet({
type
PreloadData
=
{
type
PreloadData
=
{
src
:
string
src
:
string
unoptimized
:
boolean
unoptimized
:
boolean
layout
:
LayoutValue
width
:
number
|
undefined
width
:
number
|
undefined
sizes
?:
string
sizes
?:
string
quality
?:
number
quality
?:
number
...
@@ -181,8 +192,9 @@ type PreloadData = {
...
@@ -181,8 +192,9 @@ type PreloadData = {
function
generatePreload
({
function
generatePreload
({
src
,
src
,
width
,
unoptimized
=
false
,
unoptimized
=
false
,
layout
,
width
,
sizes
,
sizes
,
quality
,
quality
,
}:
PreloadData
):
ReactElement
{
}:
PreloadData
):
ReactElement
{
...
@@ -195,9 +207,15 @@ function generatePreload({
...
@@ -195,9 +207,15 @@ function generatePreload({
<
link
<
link
rel=
"preload"
rel=
"preload"
as=
"image"
as=
"image"
href=
{
computeSrc
(
src
,
unoptimized
,
width
,
quality
)
}
href=
{
computeSrc
(
src
,
unoptimized
,
layout
,
width
,
quality
)
}
// @ts-ignore: imagesrcset and imagesizes not yet in the link element type
// @ts-ignore: imagesrcset and imagesizes not yet in the link element type
imagesrcset=
{
generateSrcSet
({
src
,
unoptimized
,
width
,
quality
})
}
imagesrcset=
{
generateSrcSet
({
src
,
unoptimized
,
layout
,
width
,
quality
,
})
}
imagesizes=
{
sizes
}
imagesizes=
{
sizes
}
/>
/>
</
Head
>
</
Head
>
...
@@ -410,11 +428,12 @@ export default function Image({
...
@@ -410,11 +428,12 @@ export default function Image({
}
}
// Generate attribute values
// Generate attribute values
const
imgSrc
=
computeSrc
(
src
,
unoptimized
,
widthInt
,
qualityInt
)
const
imgSrc
=
computeSrc
(
src
,
unoptimized
,
layout
,
widthInt
,
qualityInt
)
const
imgSrcSet
=
generateSrcSet
({
const
imgSrcSet
=
generateSrcSet
({
src
,
src
,
width
:
widthInt
,
unoptimized
,
unoptimized
,
layout
,
width
:
widthInt
,
quality
:
qualityInt
,
quality
:
qualityInt
,
})
})
...
@@ -458,8 +477,9 @@ export default function Image({
...
@@ -458,8 +477,9 @@ export default function Image({
{
shouldPreload
{
shouldPreload
?
generatePreload
({
?
generatePreload
({
src
,
src
,
width
:
widthIn
t
,
layou
t
,
unoptimized
,
unoptimized
,
width
:
widthInt
,
sizes
,
sizes
,
quality
:
qualityInt
,
quality
:
qualityInt
,
})
})
...
...
test/integration/image-component/default/pages/layout-responsive.js
View file @
f8c32e55
...
@@ -11,28 +11,28 @@ const Page = () => {
...
@@ -11,28 +11,28 @@ const Page = () => {
width
=
"1200"
width
=
"1200"
height
=
"700"
height
=
"700"
layout
=
"responsive"
layout
=
"responsive"
><
/Image
>
/
>
<
Image
<
Image
id
=
"responsive2"
id
=
"responsive2"
src
=
"/wide.png"
src
=
"/wide.png"
width
=
"1200"
width
=
"1200"
height
=
"700"
height
=
"700"
layout
=
"responsive"
layout
=
"responsive"
><
/Image
>
/
>
<
Image
<
Image
id
=
"responsive3"
id
=
"responsive3"
src
=
"/wide.png"
src
=
"/wide.png"
width
=
"1200"
width
=
"1200"
height
=
"700"
height
=
"700"
layout
=
"responsive"
layout
=
"responsive"
><
/Image
>
/
>
<
Image
<
Image
id
=
"responsive4"
id
=
"responsive4"
src
=
"/wide.png"
src
=
"/wide.png"
width
=
"1200"
width
=
"1200"
height
=
"700"
height
=
"700"
layout
=
"responsive"
layout
=
"responsive"
><
/Image
>
/
>
<
p
>
Layout
Responsive
<
/p
>
<
p
>
Layout
Responsive
<
/p
>
<
/div
>
<
/div
>
)
)
...
...
test/integration/image-component/default/test/index.test.js
View file @
f8c32e55
...
@@ -36,15 +36,24 @@ async function hasImageMatchingUrl(browser, url) {
...
@@ -36,15 +36,24 @@ async function hasImageMatchingUrl(browser, url) {
}
}
async
function
getComputed
(
browser
,
id
,
prop
)
{
async
function
getComputed
(
browser
,
id
,
prop
)
{
const
style
=
await
browser
.
eval
(
const
val
=
await
browser
.
eval
(
`document.getElementById('
${
id
}
').
${
prop
}
`
)
`getComputedStyle(document.getElementById('
${
id
}
')).
${
prop
}
`
if
(
typeof
val
===
'number'
)
{
)
return
val
if
(
typeof
style
===
'string'
)
{
}
return
parseInt
(
style
.
replace
(
/px$/
,
''
),
10
)
if
(
typeof
val
===
'string'
)
{
return
parseInt
(
val
,
10
)
}
}
return
null
return
null
}
}
async
function
getSrc
(
browser
,
id
)
{
const
src
=
await
browser
.
elementById
(
id
).
getAttribute
(
'src'
)
if
(
src
)
{
const
url
=
new
URL
(
src
)
return
url
.
href
.
slice
(
url
.
origin
.
length
)
}
}
function
getRatio
(
width
,
height
)
{
function
getRatio
(
width
,
height
)
{
return
Math
.
round
((
height
/
width
)
*
1000
)
return
Math
.
round
((
height
/
width
)
*
1000
)
}
}
...
@@ -109,6 +118,12 @@ function runTests(mode) {
...
@@ -109,6 +118,12 @@ function runTests(mode) {
const
height
=
700
const
height
=
700
const
delta
=
250
const
delta
=
250
const
id
=
'fixed1'
const
id
=
'fixed1'
expect
(
await
getSrc
(
browser
,
id
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=1200&q=75'
)
expect
(
await
browser
.
elementById
(
id
).
getAttribute
(
'srcset'
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w'
)
await
browser
.
setDimensions
({
await
browser
.
setDimensions
({
width
:
width
+
delta
,
width
:
width
+
delta
,
height
:
height
+
delta
,
height
:
height
+
delta
,
...
@@ -136,6 +151,12 @@ function runTests(mode) {
...
@@ -136,6 +151,12 @@ function runTests(mode) {
const
height
=
700
const
height
=
700
const
delta
=
250
const
delta
=
250
const
id
=
'intrinsic1'
const
id
=
'intrinsic1'
expect
(
await
getSrc
(
browser
,
id
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=1200&q=75'
)
expect
(
await
browser
.
elementById
(
id
).
getAttribute
(
'srcset'
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w'
)
await
browser
.
setDimensions
({
await
browser
.
setDimensions
({
width
:
width
+
delta
,
width
:
width
+
delta
,
height
:
height
+
delta
,
height
:
height
+
delta
,
...
@@ -166,6 +187,12 @@ function runTests(mode) {
...
@@ -166,6 +187,12 @@ function runTests(mode) {
const
height
=
700
const
height
=
700
const
delta
=
250
const
delta
=
250
const
id
=
'responsive1'
const
id
=
'responsive1'
expect
(
await
getSrc
(
browser
,
id
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=1200&q=75'
)
expect
(
await
browser
.
elementById
(
id
).
getAttribute
(
'srcset'
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w'
)
await
browser
.
setDimensions
({
await
browser
.
setDimensions
({
width
:
width
+
delta
,
width
:
width
+
delta
,
height
:
height
+
delta
,
height
:
height
+
delta
,
...
@@ -196,6 +223,12 @@ function runTests(mode) {
...
@@ -196,6 +223,12 @@ function runTests(mode) {
const
height
=
350
const
height
=
350
const
delta
=
150
const
delta
=
150
const
id
=
'fill1'
const
id
=
'fill1'
expect
(
await
getSrc
(
browser
,
id
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=1200&q=75'
)
expect
(
await
browser
.
elementById
(
id
).
getAttribute
(
'srcset'
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w'
)
await
browser
.
setDimensions
({
await
browser
.
setDimensions
({
width
:
width
+
delta
,
width
:
width
+
delta
,
height
:
height
+
delta
,
height
:
height
+
delta
,
...
@@ -226,6 +259,12 @@ function runTests(mode) {
...
@@ -226,6 +259,12 @@ function runTests(mode) {
const
width
=
await
getComputed
(
browser
,
id
,
'width'
)
const
width
=
await
getComputed
(
browser
,
id
,
'width'
)
const
height
=
await
getComputed
(
browser
,
id
,
'height'
)
const
height
=
await
getComputed
(
browser
,
id
,
'height'
)
await
browser
.
eval
(
`document.getElementById("
${
id
}
").scrollIntoView()`
)
await
browser
.
eval
(
`document.getElementById("
${
id
}
").scrollIntoView()`
)
expect
(
await
getSrc
(
browser
,
id
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=1200&q=75'
)
expect
(
await
browser
.
elementById
(
id
).
getAttribute
(
'srcset'
)).
toBe
(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w'
)
expect
(
await
getComputed
(
browser
,
id
,
'width'
)).
toBe
(
width
)
expect
(
await
getComputed
(
browser
,
id
,
'width'
)).
toBe
(
width
)
expect
(
await
getComputed
(
browser
,
id
,
'height'
)).
toBe
(
height
)
expect
(
await
getComputed
(
browser
,
id
,
'height'
)).
toBe
(
height
)
const
delta
=
150
const
delta
=
150
...
...
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