Skip to content

Commit

Permalink
fix: fetch error does not trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Feb 21, 2023
1 parent a259920 commit e3cf180
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/embed-image-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function embedImageElement<T extends HTMLImageElement | SVGImageElement>(
requestType: 'image',
responseType: 'dataUrl',
}).then(url => {
clone.src = url
clone.src = url || ''
}),
]
} else if (isSVGElementNode(clone) && !isDataUrl(clone.href.baseVal)) {
Expand All @@ -30,7 +30,7 @@ export function embedImageElement<T extends HTMLImageElement | SVGImageElement>(
requestType: 'image',
responseType: 'dataUrl',
}).then(url => {
clone.href.baseVal = url
clone.href.baseVal = url || ''
}),
]
}
Expand Down
5 changes: 4 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function baseFetch(options: BaseFetchOptions): Promise<string> {
: undefined

return fetch(url, { signal: controller.signal, ...requestInit })
.finally(() => clearTimeout(timer))
.then(response => {
if (!response.ok) {
throw new Error('Failed fetch, not 2xx response', { cause: response })
}
switch (responseType) {
case 'dataUrl':
return response.blob().then(blobToDataUrl)
Expand All @@ -32,6 +34,7 @@ export function baseFetch(options: BaseFetchOptions): Promise<string> {
return response.text()
}
})
.finally(() => clearTimeout(timer))
}

export function contextFetch(context: Context, options: ContextFetchOptions) {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface Options {
*
* default: data:image/png;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
*/
placeholderImage?: string | ((cloned: HTMLImageElement | SVGImageElement) => string)
placeholderImage?: string | ((cloned: HTMLImageElement | SVGImageElement) => string | Promise<string>)
}

/**
Expand Down

0 comments on commit e3cf180

Please sign in to comment.