Skip to content

Commit a2df7f6

Browse files
committed
Fix tests and fallback image
1 parent b37983a commit a2df7f6

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

packages/next/client/image.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const {
4242
domains: configDomains,
4343
} = imageData
4444
configSizes.sort((a, b) => a - b) // smallest to largest
45-
const largestSize = configSizes[configSizes.length - 1]
4645

4746
let cachedObserver: IntersectionObserver
4847
const IntersectionObserver =
@@ -83,12 +82,21 @@ function getObserver(): IntersectionObserver | undefined {
8382
function computeSrc(
8483
src: string,
8584
unoptimized: boolean,
85+
width: number | undefined,
8686
quality?: string
8787
): string {
8888
if (unoptimized) {
8989
return src
9090
}
91-
return callLoader({ src, width: largestSize, quality })
91+
let widths = configSizes
92+
if (typeof width === 'number') {
93+
widths = configSizes.filter((size) => size <= width)
94+
if (widths.length === 0) {
95+
widths = [configSizes[0]]
96+
}
97+
}
98+
const largest = widths[widths.length - 1]
99+
return callLoader({ src, width: largest, quality })
92100
}
93101

94102
type CallLoaderProps = {
@@ -156,7 +164,7 @@ function generatePreload({
156164
<link
157165
rel="preload"
158166
as="image"
159-
href={computeSrc(src, unoptimized, quality)}
167+
href={computeSrc(src, unoptimized, width, quality)}
160168
// @ts-ignore: imagesrcset and imagesizes not yet in the link element type
161169
imagesrcset={generateSrcSet({ src, unoptimized, width, quality })}
162170
imagesizes={sizes}
@@ -287,7 +295,7 @@ export default function Image({
287295
}
288296

289297
// Generate attribute values
290-
const imgSrc = computeSrc(src, unoptimized, quality)
298+
const imgSrc = computeSrc(src, unoptimized, widthInt, quality)
291299
const imgSrcSet = generateSrcSet({
292300
src,
293301
width: widthInt,

test/integration/image-component/basic/next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
images: {
3-
sizes: [480, 1024, 1600],
3+
sizes: [480, 1024, 1600, 2000],
44
path: 'https://example.com/myaccount/',
55
loader: 'imgix',
66
},

test/integration/image-component/basic/pages/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const Page = () => {
3939
width={300}
4040
height={400}
4141
/>
42-
<Image id="priority-image" priority src="withpriority.png" />
4342
<Image
4443
id="priority-image"
4544
priority

test/integration/image-component/basic/pages/lazy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ const Lazy = () => {
3535
id="lazy-without-attribute"
3636
src="foo4.jpg"
3737
height={400}
38-
width={300}
38+
width={1500}
3939
></Image>
4040
<div style={{ height: '2000px' }}></div>
4141
<Image
4242
id="eager-loading"
4343
src="foo5.jpg"
4444
loading="eager"
4545
height={400}
46-
width={300}
46+
width={2500}
4747
></Image>
4848
</div>
4949
)

test/integration/image-component/basic/test/index.test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ function runTests() {
3333
})
3434
it('should modify src with the loader', async () => {
3535
expect(await browser.elementById('basic-image').getAttribute('src')).toBe(
36-
'https://example.com/myaccount/foo.jpg?auto=format&w=1600&q=60'
36+
'https://example.com/myaccount/foo.jpg?auto=format&w=480&q=60'
3737
)
3838
})
3939
it('should correctly generate src even if preceding slash is included in prop', async () => {
4040
expect(
4141
await browser.elementById('preceding-slash-image').getAttribute('src')
42-
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=1600')
42+
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480')
4343
})
4444
it('should add a srcset based on the loader', async () => {
4545
expect(
@@ -49,9 +49,7 @@ function runTests() {
4949
it('should add a srcset even with preceding slash in prop', async () => {
5050
expect(
5151
await browser.elementById('preceding-slash-image').getAttribute('srcset')
52-
).toBe(
53-
'https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w, https://example.com/myaccount/fooslash.jpg?auto=format&w=1024 1024w'
54-
)
52+
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w')
5553
})
5654
it('should support the unoptimized attribute', async () => {
5755
expect(
@@ -68,7 +66,7 @@ function runTests() {
6866
function lazyLoadingTests() {
6967
it('should have loaded the first image immediately', async () => {
7068
expect(await browser.elementById('lazy-top').getAttribute('src')).toBe(
71-
'https://example.com/myaccount/foo1.jpg?auto=format&w=1600'
69+
'https://example.com/myaccount/foo1.jpg?auto=format&w=1024'
7270
)
7371
expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe(
7472
'https://example.com/myaccount/foo1.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo1.jpg?auto=format&w=1024 1024w'
@@ -99,7 +97,7 @@ function lazyLoadingTests() {
9997

10098
await check(() => {
10199
return browser.elementById('lazy-mid').getAttribute('src')
102-
}, 'https://example.com/myaccount/foo2.jpg?auto=format&w=1600')
100+
}, 'https://example.com/myaccount/foo2.jpg?auto=format&w=480')
103101

104102
await check(() => {
105103
return browser.elementById('lazy-mid').getAttribute('srcset')
@@ -148,17 +146,17 @@ function lazyLoadingTests() {
148146
await waitFor(200)
149147
expect(
150148
await browser.elementById('lazy-without-attribute').getAttribute('src')
151-
).toBe('https://example.com/myaccount/foo4.jpg?auto=format&w=1600')
149+
).toBe('https://example.com/myaccount/foo4.jpg?auto=format&w=1024')
152150
expect(
153151
await browser.elementById('lazy-without-attribute').getAttribute('srcset')
154152
).toBe(
155-
'https://example.com/myaccount/foo4.jpg?auto=format&w=480&q=60 480w, https://example.com/myaccount/foo4.jpg?auto=format&w=1024&q=60 1024w, https://example.com/myaccount/foo4.jpg?auto=format&w=1600&q=60 1600w'
153+
'https://example.com/myaccount/foo4.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo4.jpg?auto=format&w=1024 1024w'
156154
)
157155
})
158156

159157
it('should load the fifth image eagerly, without scrolling', async () => {
160158
expect(await browser.elementById('eager-loading').getAttribute('src')).toBe(
161-
'https://example.com/myaccount/foo5.jpg?auto=format&w=1600'
159+
'https://example.com/myaccount/foo5.jpg?auto=format&w=2000'
162160
)
163161
expect(
164162
await browser.elementById('eager-loading').getAttribute('srcset')
@@ -198,14 +196,14 @@ describe('Image Component Tests', () => {
198196
it('should add a preload tag for a priority image', async () => {
199197
expect(
200198
await hasPreloadLinkMatchingUrl(
201-
'https://example.com/myaccount/withpriority.png?auto=format&w=1600'
199+
'https://example.com/myaccount/withpriority.png?auto=format&w=480&q=60'
202200
)
203201
).toBe(true)
204202
})
205203
it('should add a preload tag for a priority image with preceding slash', async () => {
206204
expect(
207205
await hasPreloadLinkMatchingUrl(
208-
'https://example.com/myaccount/fooslash.jpg?auto=format&w=1600'
206+
'https://example.com/myaccount/fooslash.jpg?auto=format&w=480'
209207
)
210208
).toBe(true)
211209
})
@@ -219,7 +217,7 @@ describe('Image Component Tests', () => {
219217
it('should add a preload tag for a priority image, with quality', async () => {
220218
expect(
221219
await hasPreloadLinkMatchingUrl(
222-
'https://example.com/myaccount/withpriority.png?auto=format&w=1600&q=60'
220+
'https://example.com/myaccount/withpriority.png?auto=format&w=480&q=60'
223221
)
224222
).toBe(true)
225223
})

0 commit comments

Comments
 (0)