Skip to content

Commit

Permalink
Prevent double encoding or srcset URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
weotch committed Nov 7, 2023
1 parent 660ecca commit e6c207b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plugins/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')
}

0 comments on commit e6c207b

Please sign in to comment.