Skip to content

Commit dfc51d0

Browse files
authored
fix(next/image): handle ?dpl for src without protocol (#86836)
Follow up to previous PR based on this comment: #86485 (comment) We want to avoid adding `?dpl` when the deployment is an absolute url missing the protocol which is allowed when `unoptimized=true`.
1 parent 3a32319 commit dfc51d0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/next/src/shared/lib/get-img-props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function generateImgAttrs({
232232
}: GenImgAttrsData): GenImgAttrsResult {
233233
if (unoptimized) {
234234
const deploymentId = getDeploymentId()
235-
if (src.startsWith('/') && deploymentId) {
235+
if (src.startsWith('/') && !src.startsWith('//') && deploymentId) {
236236
const sep = src.includes('?') ? '&' : '?'
237237
src = `${src}${sep}dpl=${deploymentId}`
238238
}

test/unit/next-image-get-img-props.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,4 +796,28 @@ describe('getImageProps()', () => {
796796
delete process.env.NEXT_DEPLOYMENT_ID
797797
}
798798
})
799+
it('should not add query string for unoptimized with no protocol when NEXT_DEPLOYMENT_ID defined', async () => {
800+
try {
801+
process.env.NEXT_DEPLOYMENT_ID = 'dpl_123'
802+
const { props } = getImageProps({
803+
alt: 'a nice desc',
804+
src: '//example.com/test.png',
805+
unoptimized: true,
806+
width: 100,
807+
height: 200,
808+
})
809+
expect(warningMessages).toStrictEqual([])
810+
expect(Object.entries(props)).toStrictEqual([
811+
['alt', 'a nice desc'],
812+
['loading', 'lazy'],
813+
['width', 100],
814+
['height', 200],
815+
['decoding', 'async'],
816+
['style', { color: 'transparent' }],
817+
['src', '//example.com/test.png'],
818+
])
819+
} finally {
820+
delete process.env.NEXT_DEPLOYMENT_ID
821+
}
822+
})
799823
})

0 commit comments

Comments
 (0)