From 3727a435b508def4acb9f979308dcb8da221e95c Mon Sep 17 00:00:00 2001 From: Michael Brevard Date: Sat, 15 Jun 2024 17:04:19 +0300 Subject: [PATCH] fix: avoid setting another mounted if instance is registered --- .../runtime/client-delayed-component.ts | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/nuxt/src/components/runtime/client-delayed-component.ts b/packages/nuxt/src/components/runtime/client-delayed-component.ts index f1b61b8688..44ab4c513d 100644 --- a/packages/nuxt/src/components/runtime/client-delayed-component.ts +++ b/packages/nuxt/src/components/runtime/client-delayed-component.ts @@ -105,18 +105,20 @@ export const createLazyEventClientPage = (componentLoader: Component) => { const events: string[] = attrs.loader as string[] ?? ['mouseover'] const registeredEvents: (() => void)[] = [] - onMounted(() => { - events.forEach((event) => { - const handler = () => { - isTriggered.value = true - registeredEvents.forEach(remove => remove()) - eventsMapper.delete(instance) - } - instance.vnode.el?.addEventListener(event, handler) - registeredEvents.push(() => instance.vnode.el?.removeEventListener(event, handler)) + if (!eventsMapper.has(instance)) { + onMounted(() => { + events.forEach((event) => { + const handler = () => { + isTriggered.value = true + registeredEvents.forEach(remove => remove()) + eventsMapper.delete(instance) + } + instance.vnode.el?.addEventListener(event, handler) + registeredEvents.push(() => instance.vnode.el?.removeEventListener(event, handler)) + }) + eventsMapper.set(instance, registeredEvents) }) - eventsMapper.set(instance, registeredEvents) - }) + } onBeforeUnmount(() => { registeredEvents?.forEach(remove => remove()) eventsMapper.delete(instance)