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({
width,
quality,
}: DefaultImageLoaderProps): string {
// Demo: https://static.imgix.net/daisy.png?format=auto&fit=max&w=300
const params = ['auto=format', 'fit=max', 'w=' + width]
let paramsString = ''
// Demo: https://static.imgix.net/daisy.png?auto=format&fit=max&w=300
const url = new URL(`${root}${normalizeSrc(src)}`)
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) {
params.push('q=' + quality)
params.set('q', quality.toString())
}
if (params.length) {
paramsString = '?' + params.join('&')
}
return `${root}${normalizeSrc(src)}${paramsString}`
return url.href
}
function akamaiLoader({ root, src, width }: DefaultImageLoaderProps): string {
......
......@@ -67,6 +67,27 @@ const ClientSide = () => {
width={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">
<a id="errorslink">Errors</a>
</Link>
......
......@@ -92,6 +92,27 @@ const Page = () => {
width={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">
<a id="clientlink">Client Side</a>
</Link>
......
......@@ -87,6 +87,23 @@ function runTests() {
await browser.elementById('unoptimized-image').getAttribute('srcset')
).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() {
......
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