Skip to content

Commit 690b460

Browse files
feat(vue,nuxt)!: rename @loaded to @image-load and add @image-error
- Emit names align with the core option names (`onImageLoad`, `onImageError`). - The declared `error` emit is removed; native `@error` listeners now fall through via Vue attribute fallthrough and also fire for the synthetic preload failure dispatched on the visible `<img>`. - The components capture `triggerLoad`'s disposer so unmount during an in-flight preload no longer fires callbacks on a destroyed instance.
1 parent 9f08c7f commit 690b460

7 files changed

Lines changed: 34 additions & 24 deletions

File tree

docs/integrations/nuxt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ The component also accepts all standard `<img>` HTML attributes via Nuxt's attri
8181

8282
| Event | Payload | Description |
8383
| --- | --- | --- |
84-
| `loaded` | `HTMLImageElement` | Emitted when the image has been successfully loaded. |
85-
| `error` | `Event` | Emitted when an error occurs during image loading. |
84+
| `image-load` | `HTMLImageElement` | Fired once when the real image finishes loading. |
85+
| `image-error` | `HTMLImageElement, Event` | Fired when the preload fails. See [migration notes](/guide/migration#native-img-error-now-fires-on-preload-failure) for native `@error` behavior. |
8686

8787
### Server-Side Rendering
8888

docs/integrations/vue.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ The component also accepts all standard `<img>` HTML attributes via Vue's attrib
7777

7878
| Event | Payload | Description |
7979
| --- | --- | --- |
80-
| `loaded` | `HTMLImageElement` | Emitted when the image has been successfully loaded. |
81-
| `error` | `Event` | Emitted when an error occurs during image loading. |
80+
| `image-load` | `HTMLImageElement` | Fired once when the real image finishes loading. |
81+
| `image-error` | `HTMLImageElement, Event` | Fired when the preload fails. See [migration notes](/guide/migration#native-img-error-now-fires-on-preload-failure) for native `@error` behavior. |
8282

8383
### Reactivity
8484

packages/nuxt/playground/app/app.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ useHead({
1111
title: '@unlazy/nuxt',
1212
})
1313
14-
function onLoaded(image: HTMLImageElement) {
14+
function onImageLoad(image: HTMLImageElement) {
1515
console.log('Image loaded:', image.src)
1616
}
1717
</script>
@@ -28,7 +28,7 @@ function onLoaded(image: HTMLImageElement) {
2828
src="/images/fall-evan-wallace.jpg"
2929
width="640"
3030
height="427"
31-
@loaded="onLoaded"
31+
@image-load="onImageLoad"
3232
/>
3333
</section>
3434

packages/nuxt/playground/nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export default defineNuxtConfig({
1818
},
1919

2020
optimizeDeps: {
21+
include: [
22+
'@vue/devtools-core',
23+
'@vue/devtools-kit',
24+
'fast-blurhash',
25+
'thumbhash',
26+
],
27+
2128
exclude: [
2229
'@unlazy/core',
2330
'unlazy',

packages/nuxt/src/runtime/components/UnLazyImage.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ const props = withDefaults(
7171
)
7272
7373
const emit = defineEmits<{
74-
(event: 'loaded', image: HTMLImageElement): void
75-
(event: 'error', error: Event): void
74+
(event: 'imageLoad', image: HTMLImageElement): void
75+
(event: 'imageError', image: HTMLImageElement, error: Event): void
7676
}>()
7777
7878
const unlazy = useRuntimeConfig().public.unlazy as ModuleOptions
@@ -121,7 +121,10 @@ watchEffect(() => {
121121
if (props.preload) {
122122
if (props.autoSizes)
123123
_autoSizes(target.value)
124-
triggerLoad(target.value, image => emit('loaded', image))
124+
cleanup = triggerLoad(target.value, {
125+
onImageLoad: image => emit('imageLoad', image),
126+
onImageError: (image, error) => emit('imageError', image, error),
127+
})
125128
return
126129
}
127130
@@ -131,9 +134,8 @@ watchEffect(() => {
131134
// Placeholder is already decoded
132135
cleanup = lazyLoad(target.value, {
133136
hash: false,
134-
onImageLoad(image) {
135-
emit('loaded', image)
136-
},
137+
onImageLoad: image => emit('imageLoad', image),
138+
onImageError: (image, error) => emit('imageError', image, error),
137139
})
138140
})
139141
@@ -159,7 +161,6 @@ onBeforeUnmount(() => {
159161
:data-srcset="srcSet"
160162
:data-sizes="autoSizes ? 'auto' : undefined"
161163
:loading="loading || (props.lazyLoad ? 'lazy' : 'eager')"
162-
@error="emit('error', $event)"
163164
>
164165
</picture>
165166
<img
@@ -171,6 +172,5 @@ onBeforeUnmount(() => {
171172
:data-srcset="srcSet"
172173
:data-sizes="autoSizes ? 'auto' : undefined"
173174
:loading="loading || (props.lazyLoad ? 'lazy' : 'eager')"
174-
@error="emit('error', $event)"
175175
>
176176
</template>

packages/vue/playground/App.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script setup lang="ts">
22
import { UnLazyImage } from '../src/components'
33
4-
function onLoaded(image: HTMLImageElement) {
4+
function onImageLoad(image: HTMLImageElement) {
55
console.log('Image loaded:', image.src)
66
}
77
8-
function onError(error: Event) {
9-
console.error('Image error:', error)
8+
function onImageError(image: HTMLImageElement, error: Event) {
9+
console.error('Image error:', image.src, error)
1010
}
1111
</script>
1212

@@ -21,8 +21,8 @@ function onError(error: Event) {
2121
src="/images/fall-evan-wallace.jpg"
2222
width="640"
2323
height="427"
24-
@loaded="onLoaded"
25-
@error="onError"
24+
@image-load="onImageLoad"
25+
@image-error="onImageError"
2626
/>
2727
</section>
2828

packages/vue/src/components/UnLazyImage.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const props = defineProps<{
3434
}>()
3535
3636
const emit = defineEmits<{
37-
(event: 'loaded', image: HTMLImageElement): void
38-
(event: 'error', error: Event): void
37+
(event: 'imageLoad', image: HTMLImageElement): void
38+
(event: 'imageError', image: HTMLImageElement, error: Event): void
3939
}>()
4040
4141
const target = ref<HTMLImageElement | undefined>()
@@ -50,15 +50,19 @@ watchEffect(() => {
5050
if (props.preload) {
5151
if (props.autoSizes)
5252
_autoSizes(target.value)
53-
triggerLoad(target.value, image => emit('loaded', image))
53+
cleanup = triggerLoad(target.value, {
54+
onImageLoad: image => emit('imageLoad', image),
55+
onImageError: (image, error) => emit('imageError', image, error),
56+
})
5457
return
5558
}
5659
5760
cleanup = lazyLoad(target.value, {
5861
hash: props.thumbhash || props.blurhash,
5962
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
6063
placeholderSize: props.placeholderSize,
61-
onImageLoad: image => emit('loaded', image),
64+
onImageLoad: image => emit('imageLoad', image),
65+
onImageError: (image, error) => emit('imageError', image, error),
6266
})
6367
})
6468
@@ -75,6 +79,5 @@ onBeforeUnmount(() => {
7579
:data-srcset="srcSet"
7680
:data-sizes="autoSizes ? 'auto' : undefined"
7781
:loading="loading || 'lazy'"
78-
@error="emit('error', $event)"
7982
>
8083
</template>

0 commit comments

Comments
 (0)