From aa4faaab3c7daf8d0d6ce8bf740c93a7402135ee Mon Sep 17 00:00:00 2001 From: Becem Date: Thu, 25 Jan 2024 15:29:50 +0100 Subject: [PATCH] fix(nuxt): do not rerun synchronous `callOnce` callbacks (#25431) --- packages/nuxt/src/app/composables/once.ts | 2 +- test/nuxt/composables.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/once.ts b/packages/nuxt/src/app/composables/once.ts index c8c664826d..177ab4c9d5 100644 --- a/packages/nuxt/src/app/composables/once.ts +++ b/packages/nuxt/src/app/composables/once.ts @@ -25,7 +25,7 @@ export async function callOnce (...args: any): Promise { return } nuxtApp._once = nuxtApp._once || {} - nuxtApp._once[_key] = nuxtApp._once[_key] || fn() + nuxtApp._once[_key] = nuxtApp._once[_key] || fn() || true await nuxtApp._once[_key] nuxtApp.payload.once.add(_key) delete nuxtApp._once[_key] diff --git a/test/nuxt/composables.test.ts b/test/nuxt/composables.test.ts index f9229a01d2..ee96662c91 100644 --- a/test/nuxt/composables.test.ts +++ b/test/nuxt/composables.test.ts @@ -595,6 +595,11 @@ describe('callOnce', () => { const execute = () => callOnce(fn) await Promise.all([execute(), execute(), execute()]) expect(fn).toHaveBeenCalledTimes(1) + + const fnSync = vi.fn().mockImplementation(() => { }) + const executeSync = () => callOnce(fnSync) + await Promise.all([executeSync(), executeSync(), executeSync()]) + expect(fnSync).toHaveBeenCalledTimes(1) }) it('should use key to dedupe', async () => {