Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const processedEncodedUrl = await processSrcSet(
attr.value,
async ({ url }) => {
const decodedUrl = decodeURI(url)
if (!isExcludedUrl(decodedUrl)) {
const decodedUrl = decodeURIIfPossible(url)
if (
decodedUrl !== undefined &&
!isExcludedUrl(decodedUrl)
) {
const result = await processAssetUrl(url)
return result !== decodedUrl
? encodeURIPath(result)
Expand All @@ -550,8 +553,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})(),
)
} else if (attr.type === 'src') {
const url = decodeURI(attr.value)
if (checkPublicFile(url, config)) {
const url = decodeURIIfPossible(attr.value)
if (url === undefined) {
// ignore it
} else if (checkPublicFile(url, config)) {
overwriteAttrValue(
s,
attr.location,
Expand Down Expand Up @@ -1580,3 +1585,12 @@ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
function incrementIndent(indent: string = '') {
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
}

function decodeURIIfPossible(input: string): string | undefined {
try {
return decodeURI(input)
} catch {
// url is malformed, probably a interpolate syntax of template engines
return
}
}