Unverified Commit 38a4e56c authored by Theo Strahlen's avatar Theo Strahlen Committed by GitHub

(next/image): Merge query string params in imgix loader (#26719)

If the `Image` src url had existing query params, the imgix loader would simply append another query string with `?` causing both query strings to break.

This PR adds a way to safely merge query strings if needed using [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams).

## Bug

- [X] fixes #26288
- [X] Integration tests added
parent f7bc8f4b
...@@ -649,17 +649,19 @@ function imgixLoader({ ...@@ -649,17 +649,19 @@ function imgixLoader({
width, width,
quality, quality,
}: DefaultImageLoaderProps): string { }: DefaultImageLoaderProps): string {
// Demo: https://static.imgix.net/daisy.png?format=auto&fit=max&w=300 // Demo: https://static.imgix.net/daisy.png?auto=format&fit=max&w=300
const params = ['auto=format', 'fit=max', 'w=' + width] const url = new URL(`${root}${normalizeSrc(src)}`)
let paramsString = '' const params = url.searchParams
params.set('auto', params.get('auto') || 'format')
params.set('fit', params.get('fit') || 'max')
params.set('w', params.get('w') || width.toString())
if (quality) { if (quality) {
params.push('q=' + quality) params.set('q', quality.toString())
} }
if (params.length) { return url.href
paramsString = '?' + params.join('&')
}
return `${root}${normalizeSrc(src)}${paramsString}`
} }
function akamaiLoader({ root, src, width }: DefaultImageLoaderProps): string { function akamaiLoader({ root, src, width }: DefaultImageLoaderProps): string {
......
...@@ -67,6 +67,27 @@ const ClientSide = () => { ...@@ -67,6 +67,27 @@ const ClientSide = () => {
width={16} width={16}
height={16} height={16}
/> />
<Image
id="image-with-param-auto"
src="foo.png?auto=compress"
loading="eager"
width={300}
height={400}
/>
<Image
id="image-with-param-width"
src="foo.png?auto=format&w=500"
loading="eager"
width={500}
height={500}
/>
<Image
id="image-with-param-fit"
src="foo.png?auto=format&fit=crop&w=300&h=300"
loading="eager"
width={300}
height={300}
/>
<Link href="/errors"> <Link href="/errors">
<a id="errorslink">Errors</a> <a id="errorslink">Errors</a>
</Link> </Link>
......
...@@ -92,6 +92,27 @@ const Page = () => { ...@@ -92,6 +92,27 @@ const Page = () => {
width={16} width={16}
height={16} height={16}
/> />
<Image
id="image-with-param-auto"
src="foo.png?auto=compress"
loading="eager"
width={300}
height={400}
/>
<Image
id="image-with-param-width"
src="foo.png?auto=format&w=500"
loading="eager"
width={500}
height={500}
/>
<Image
id="image-with-param-fit"
src="foo.png?auto=format&fit=crop&w=300&h=300"
loading="eager"
width={300}
height={300}
/>
<Link href="/client-side"> <Link href="/client-side">
<a id="clientlink">Client Side</a> <a id="clientlink">Client Side</a>
</Link> </Link>
......
...@@ -87,6 +87,23 @@ function runTests() { ...@@ -87,6 +87,23 @@ function runTests() {
await browser.elementById('unoptimized-image').getAttribute('srcset') await browser.elementById('unoptimized-image').getAttribute('srcset')
).toBeFalsy() ).toBeFalsy()
}) })
it('should keep auto parameter if already set', async () => {
expect(
await browser.elementById('image-with-param-auto').getAttribute('src')
).toBe('https://example.com/myaccount/foo.png?auto=compress&fit=max&w=1024')
})
it('should keep width parameter if already set', async () => {
expect(
await browser.elementById('image-with-param-width').getAttribute('src')
).toBe('https://example.com/myaccount/foo.png?auto=format&w=500&fit=max')
})
it('should keep fit parameter if already set', async () => {
expect(
await browser.elementById('image-with-param-fit').getAttribute('src')
).toBe(
'https://example.com/myaccount/foo.png?auto=format&fit=crop&w=300&h=300'
)
})
} }
function lazyLoadingTests() { function lazyLoadingTests() {
......
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