image icon indicating copy to clipboard operation
image copied to clipboard

@load - Not Firing - Nuxt 3

Open SleepiestAdam opened this issue 3 years ago • 13 comments

Really simple code:

<nuxt-img format="webp" src="/img/sleepiest-background.png" width="1593" height="717" :class="[backgroundLoaded == true ? 'opacity-100' : 'opacity-0', 'transition-opacity duration-200 ease-in-out']" @load="backgroundLoadComplete" />

<script>
export default {
  data() {
    return {
      backgroundLoaded: false,
    };
  },
  methods: {
    backgroundLoadComplete() {
      console.log('Background did load!');
      this.backgroundLoaded = true;
    },
  },
};
</script>

Unfortunately the @load event isn't firing, and I'm not sure why.

Using Nuxt 3 (3.0.0) and @nuxt/image-edge (^1.0.0-27827328.bc9ddc0).

SleepiestAdam avatar Dec 13 '22 15:12 SleepiestAdam

For me it does not call load if the page is called directly or if you hit f5 while on the page. If the page is called from another page then it works as expected.



glashtin avatar Dec 24 '22 14:12 glashtin

Same here. It seems like the images are cached. Hitting F5 doesn't call the event handler. Hitting ctrl-F5 (delete cache) calls the handler on 'some' images.... Update: I face the same behaviour when using <img src="..." @load="doSomething" />. So does it have something to do with nuxt3 itself?

Der-Alex avatar Jan 09 '23 10:01 Der-Alex

@pi0 could you have a look at #702 I fix the load listener it should now also work with picture as requested in https://github.com/nuxt/image/pull/416#issuecomment-1132650887

piscis avatar Jan 10 '23 14:01 piscis

same here, fires only when images are lazy loaded and not inside the viewport on init.

True does not work even with <img /> https://codesandbox.io/s/hardcore-glitter-lqb5uu?file=/app.vue

gregorvoinov avatar Jan 16 '23 15:01 gregorvoinov

Any update on a fix being pushed to release? Currently blocking us going into production.

SleepiestAdam avatar Jan 17 '23 23:01 SleepiestAdam

+1 On this... same code running on Nuxt2 works fine 🥴

stefbowerman avatar Jan 19 '23 15:01 stefbowerman

Wrapping with <ClientOnly> helped me with this one as a temporary fix.

stanislavpodolia avatar Jan 25 '23 13:01 stanislavpodolia

Please fix and tag as bug :)

TheNaschkatze avatar Mar 08 '23 14:03 TheNaschkatze

A workaround work for me is use $el to get the raw element, and emit load when image is already fully loaded.

<script setup lang="ts">
const img = ref();

const emit = defineEmits(['load', 'error', 'animationend']);
onMounted(() => {
  const element = img.value.$el as HTMLImageElement;

  if (element) {
    element.onerror = (event) => emit('error', event);
    element.onanimationend = (event) => emit('animationend', event);
  }

  nextTick(() => {
    if (!element || !element.complete) return;

    emit('load');

    if (element.naturalWidth === 0) {
      emit('error');
    }
  });
});
</script>

<template>
  <NuxtImg ref="img" v-bind="$attrs" @animationend="emit('animationend')" @error="emit('error')" @load="emit('load')" />
</template>

everyx avatar Apr 22 '23 02:04 everyx

This seems like a Vue issue, is that everyone else's impression as well?

sikhote avatar May 24 '23 19:05 sikhote

Note that @load does fire (https://github.com/nuxt/image/pull/841) but on initial load the load event has already fired before Nuxt/Vue hydrates the page so it's not picked up by the code. It's likely we can resolve this, but it's not a bug in nuxt/image but rather a limitation of images in SSR-rendered Vue.

This is effectively the same issue as https://github.com/nuxt/image/issues/412.

danielroe avatar May 31 '23 20:05 danielroe

Ran into this again today, this is the solve for non-nuxt-image users:

Template:

    <img
        ref="image"
        @load="setImageLoaded"
    >

Script:

// Local state
const image = ref(null)
const hasLoaded = ref(false)

// Methods
const setImageLoaded = () => {
    hasLoaded.value = true
}
onMounted(() => {
    if (image.value?.complete) {
        setImageLoaded()
    }
})

drewbaker avatar Jan 30 '25 03:01 drewbaker

@everyx 's solution seems to work for me; I haven't been able to reproduce my image issue (similar to OP) since implementing this; testing all contexts. Checking .complete on the actual raw img element is seen in full solutions in non-Vue contexts on the web.

kalnode avatar Apr 07 '25 13:04 kalnode