diff --git a/playground/layouts/default.vue b/playground/layouts/default.vue index 8ce377279..ac0885afd 100644 --- a/playground/layouts/default.vue +++ b/playground/layouts/default.vue @@ -10,5 +10,6 @@ body { background: #111111; display: flex; flex-direction: column; + color: #ffffff; } diff --git a/src/runtime/nuxt-image-mixins.ts b/src/runtime/nuxt-image-mixins.ts index 370057d5e..89c0927d7 100644 --- a/src/runtime/nuxt-image-mixins.ts +++ b/src/runtime/nuxt-image-mixins.ts @@ -65,6 +65,7 @@ export default { }, data () { return { + error: '', srcset: [], blurry: null, loading: false, @@ -73,7 +74,7 @@ export default { }, async fetch () { if (!this.legacy) { - this.blurry = await this.$img.lqip(this.src) + this.blurry = await this.getPlaceholder() } }, mounted () { @@ -132,7 +133,7 @@ export default { }, watch: { async src () { - this.blurry = await this.$img.lqip(this.src) + this.blurry = await this.getPlaceholder() this.original = null if (!this.legacy) { this.$img.$observer.remove(this.$el) @@ -144,15 +145,28 @@ export default { } }, methods: { + getPlaceholder () { + try { + return this.$img.lqip(this.src) + } catch (e) { + this.onError(e) + return false + } + }, generateSizedImage (width: number, height: number, format: string) { - const image = this.$img(this.src, { - width, - height, - format, - fit: this.fit, - ...this.operations - }) - return encodeURI(image) + try { + const image = this.$img(this.src, { + width, + height, + format, + fit: this.fit, + ...this.operations + }) + return encodeURI(image) + } catch (e) { + this.onError(e) + return '' + } }, loadOriginalImage () { this.loading = true @@ -167,6 +181,11 @@ export default { ...this.imgAttributes, ...extraAttributes }) + }, + onError (e: Error) { + this.error = e.message + // eslint-disable-next-line no-console + console.error(e.message) } }, beforeDestroy () { diff --git a/src/runtime/nuxt-image.ts b/src/runtime/nuxt-image.ts index d9fb8ea3d..63e591252 100644 --- a/src/runtime/nuxt-image.ts +++ b/src/runtime/nuxt-image.ts @@ -23,6 +23,11 @@ export default { } }, render (h) { + if (this.error) { + return h('div', { + class: ['__nim_w'].concat(this.$attrs.class || '') + }, [this.error]) + } if (this.legacy) { return this.renderLegacy(h) } diff --git a/src/runtime/nuxt-picture.ts b/src/runtime/nuxt-picture.ts index f73ddf5f1..afcc12f1d 100644 --- a/src/runtime/nuxt-picture.ts +++ b/src/runtime/nuxt-picture.ts @@ -26,6 +26,11 @@ export default { } }, render (h) { + if (this.error) { + return h('div', { + class: ['__nim_w'].concat(this.$attrs.class || '') + }, [this.error]) + } if (this.legacy) { return this.renderLegacy(h) }