Unverified Commit 3fd1cc3e authored by Steven's avatar Steven Committed by GitHub

Update 3rd party image loaders to prevent upscaling (#18647)

In PR #18147, we fixed the default loader to prevent upscaling images.

This PR fixes the same bug for 3rd party loaders.

---

Fixes #18648
parent 377aa4b6
...@@ -519,7 +519,8 @@ function normalizeSrc(src: string) { ...@@ -519,7 +519,8 @@ function normalizeSrc(src: string) {
} }
function imgixLoader({ root, src, width, quality }: LoaderProps): string { function imgixLoader({ root, src, width, quality }: LoaderProps): string {
const params = ['auto=format', 'w=' + width] // Demo: https://static.imgix.net/daisy.png?format=auto&fit=max&w=300
const params = ['auto=format', 'fit=max', 'w=' + width]
let paramsString = '' let paramsString = ''
if (quality) { if (quality) {
params.push('q=' + quality) params.push('q=' + quality)
...@@ -536,7 +537,8 @@ function akamaiLoader({ root, src, width }: LoaderProps): string { ...@@ -536,7 +537,8 @@ function akamaiLoader({ root, src, width }: LoaderProps): string {
} }
function cloudinaryLoader({ root, src, width, quality }: LoaderProps): string { function cloudinaryLoader({ root, src, width, quality }: LoaderProps): string {
const params = ['f_auto', 'w_' + width] // Demo: https://res.cloudinary.com/demo/image/upload/w_300,c_limit/turtles.jpg
const params = ['f_auto', 'c_limit', 'w_' + width]
let paramsString = '' let paramsString = ''
if (quality) { if (quality) {
params.push('q_' + quality) params.push('q_' + quality)
......
...@@ -33,37 +33,47 @@ function runTests() { ...@@ -33,37 +33,47 @@ function runTests() {
}) })
it('should modify src with the loader', async () => { it('should modify src with the loader', async () => {
expect(await browser.elementById('basic-image').getAttribute('src')).toBe( expect(await browser.elementById('basic-image').getAttribute('src')).toBe(
'https://example.com/myaccount/foo.jpg?auto=format&w=480&q=60' 'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=480&q=60'
) )
}) })
it('should correctly generate src even if preceding slash is included in prop', async () => { it('should correctly generate src even if preceding slash is included in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('src') await browser.elementById('preceding-slash-image').getAttribute('src')
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480') ).toBe(
'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480'
)
}) })
it('should add a srcset based on the loader', async () => { it('should add a srcset based on the loader', async () => {
expect( expect(
await browser.elementById('basic-image').getAttribute('srcset') await browser.elementById('basic-image').getAttribute('srcset')
).toBe('https://example.com/myaccount/foo.jpg?auto=format&w=480&q=60 480w') ).toBe(
'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=480&q=60 480w'
)
}) })
it('should add a srcset even with preceding slash in prop', async () => { it('should add a srcset even with preceding slash in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('srcset') await browser.elementById('preceding-slash-image').getAttribute('srcset')
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w') ).toBe(
'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480 480w'
)
}) })
it('should use imageSizes when width matches, not deviceSizes from next.config.js', async () => { it('should use imageSizes when width matches, not deviceSizes from next.config.js', async () => {
expect(await browser.elementById('icon-image-16').getAttribute('src')).toBe( expect(await browser.elementById('icon-image-16').getAttribute('src')).toBe(
'https://example.com/myaccount/icon.png?auto=format&w=16' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=16'
) )
expect( expect(
await browser.elementById('icon-image-16').getAttribute('srcset') await browser.elementById('icon-image-16').getAttribute('srcset')
).toBe('https://example.com/myaccount/icon.png?auto=format&w=16 16w') ).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=16 16w'
)
expect(await browser.elementById('icon-image-64').getAttribute('src')).toBe( expect(await browser.elementById('icon-image-64').getAttribute('src')).toBe(
'https://example.com/myaccount/icon.png?auto=format&w=64' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=64'
) )
expect( expect(
await browser.elementById('icon-image-64').getAttribute('srcset') await browser.elementById('icon-image-64').getAttribute('srcset')
).toBe('https://example.com/myaccount/icon.png?auto=format&w=64 64w') ).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=64 64w'
)
}) })
it('should support the unoptimized attribute', async () => { it('should support the unoptimized attribute', async () => {
expect( expect(
...@@ -80,10 +90,10 @@ function runTests() { ...@@ -80,10 +90,10 @@ function runTests() {
function lazyLoadingTests() { function lazyLoadingTests() {
it('should have loaded the first image immediately', async () => { it('should have loaded the first image immediately', async () => {
expect(await browser.elementById('lazy-top').getAttribute('src')).toBe( expect(await browser.elementById('lazy-top').getAttribute('src')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&w=1024' 'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024'
) )
expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe( expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo1.jpg?auto=format&w=1024 1024w' 'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024 1024w'
) )
}) })
it('should not have loaded the second image immediately', async () => { it('should not have loaded the second image immediately', async () => {
...@@ -111,11 +121,11 @@ function lazyLoadingTests() { ...@@ -111,11 +121,11 @@ function lazyLoadingTests() {
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('src') return browser.elementById('lazy-mid').getAttribute('src')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&w=480') }, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480')
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('srcset') return browser.elementById('lazy-mid').getAttribute('srcset')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&w=480 480w') }, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480 480w')
}) })
it('should not have loaded the third image after scrolling down', async () => { it('should not have loaded the third image after scrolling down', async () => {
expect( expect(
...@@ -160,17 +170,17 @@ function lazyLoadingTests() { ...@@ -160,17 +170,17 @@ function lazyLoadingTests() {
await waitFor(200) await waitFor(200)
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('src') await browser.elementById('lazy-without-attribute').getAttribute('src')
).toBe('https://example.com/myaccount/foo4.jpg?auto=format&w=1024') ).toBe('https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024')
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('srcset') await browser.elementById('lazy-without-attribute').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foo4.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo4.jpg?auto=format&w=1024 1024w' 'https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024 1024w'
) )
}) })
it('should load the fifth image eagerly, without scrolling', async () => { it('should load the fifth image eagerly, without scrolling', async () => {
expect(await browser.elementById('eager-loading').getAttribute('src')).toBe( expect(await browser.elementById('eager-loading').getAttribute('src')).toBe(
'https://example.com/myaccount/foo5.jpg?auto=format&w=2000' 'https://example.com/myaccount/foo5.jpg?auto=format&fit=max&w=2000'
) )
expect( expect(
await browser.elementById('eager-loading').getAttribute('srcset') await browser.elementById('eager-loading').getAttribute('srcset')
...@@ -210,14 +220,14 @@ describe('Image Component Tests', () => { ...@@ -210,14 +220,14 @@ describe('Image Component Tests', () => {
it('should add a preload tag for a priority image', async () => { it('should add a preload tag for a priority image', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriority.png?auto=format&w=480&q=60' 'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=480&q=60'
) )
).toBe(true) ).toBe(true)
}) })
it('should add a preload tag for a priority image with preceding slash', async () => { it('should add a preload tag for a priority image with preceding slash', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/fooslash.jpg?auto=format&w=480' 'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480'
) )
).toBe(true) ).toBe(true)
}) })
...@@ -231,7 +241,7 @@ describe('Image Component Tests', () => { ...@@ -231,7 +241,7 @@ describe('Image Component Tests', () => {
it('should add a preload tag for a priority image, with quality', async () => { it('should add a preload tag for a priority image, with quality', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriority.png?auto=format&w=480&q=60' 'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=480&q=60'
) )
).toBe(true) ).toBe(true)
}) })
...@@ -248,7 +258,7 @@ describe('Image Component Tests', () => { ...@@ -248,7 +258,7 @@ describe('Image Component Tests', () => {
it('should NOT add a preload tag for a priority image', async () => { it('should NOT add a preload tag for a priority image', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriorityclient.png?auto=format' 'https://example.com/myaccount/withpriorityclient.png?auto=format&fit=max'
) )
).toBe(false) ).toBe(false)
}) })
...@@ -284,11 +294,13 @@ describe('Image Component Tests', () => { ...@@ -284,11 +294,13 @@ describe('Image Component Tests', () => {
browser = await webdriver(appPort, '/missing-observer') browser = await webdriver(appPort, '/missing-observer')
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('src') await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe('https://example.com/myaccount/foox.jpg?auto=format&w=1024') ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024'
)
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset') await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&w=1024 1024w' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1024w'
) )
}) })
}) })
...@@ -308,11 +320,13 @@ describe('Image Component Tests', () => { ...@@ -308,11 +320,13 @@ describe('Image Component Tests', () => {
browser = await webdriver(appPort, '/missing-observer') browser = await webdriver(appPort, '/missing-observer')
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('src') await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe('https://example.com/myaccount/foox.jpg?auto=format&w=1024') ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024'
)
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset') await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&w=1024 1024w' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1024w'
) )
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment