From e6c207bbb34ec1a378e6911d3347e8bf01e31971 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Tue, 7 Nov 2023 07:48:33 -0800 Subject: [PATCH] Prevent double encoding or srcset URLs --- plugins/helpers.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/helpers.js b/plugins/helpers.js index cb3577a..213cc51 100644 --- a/plugins/helpers.js +++ b/plugins/helpers.js @@ -86,9 +86,13 @@ export function srcset(url, sizes, modifiers, options, { sizes = sizes.filter(size => size <= maxWidth) } - // Make array of srcsets + // Make array of srcsets. Only encode the URL if it's not already encoded. + // I'm using decodeURIComponent rather than decodeURI so that it also tries + // to decode `@` symbols, which I found encoded in some image paths. return sizes.map(size => { const sizedUrl = $cloakImg(url, { ...modifiers, width: size }, options) - return `${encodeURI(sizedUrl)} ${size}w` + const encodedUrl = decodeURIComponent(sizedUrl) == sizedUrl ? + encodeURI(sizedUrl) : sizedUrl + return `${encodedUrl} ${size}w` }).join(', ') }