diff --git a/src/embed-image-element.ts b/src/embed-image-element.ts index 5a71675..4efd8d0 100644 --- a/src/embed-image-element.ts +++ b/src/embed-image-element.ts @@ -17,7 +17,7 @@ export function embedImageElement( requestType: 'image', responseType: 'dataUrl', }).then(url => { - clone.src = url + clone.src = url || '' }), ] } else if (isSVGElementNode(clone) && !isDataUrl(clone.href.baseVal)) { @@ -30,7 +30,7 @@ export function embedImageElement( requestType: 'image', responseType: 'dataUrl', }).then(url => { - clone.href.baseVal = url + clone.href.baseVal = url || '' }), ] } diff --git a/src/fetch.ts b/src/fetch.ts index 6a6677c..061e29f 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -22,8 +22,10 @@ export function baseFetch(options: BaseFetchOptions): Promise { : 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) @@ -32,6 +34,7 @@ export function baseFetch(options: BaseFetchOptions): Promise { return response.text() } }) + .finally(() => clearTimeout(timer)) } export function contextFetch(context: Context, options: ContextFetchOptions) { diff --git a/src/options.ts b/src/options.ts index 2380cf2..53936d7 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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) } /**