From 0f27f80c0c0b154d42308b9fb3e182562e6dd01d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 25 Mar 2020 19:40:39 +0100 Subject: [PATCH] fix(vue-app): avoid multiple `$fetch` calls (#7129) Co-authored-by: Aster --- packages/vue-app/template/mixins/fetch.client.js | 10 +++++++++- packages/vue-app/template/utils.js | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vue-app/template/mixins/fetch.client.js b/packages/vue-app/template/mixins/fetch.client.js index cc5ee8fbbd..1cb99350be 100644 --- a/packages/vue-app/template/mixins/fetch.client.js +++ b/packages/vue-app/template/mixins/fetch.client.js @@ -52,7 +52,15 @@ function created() { } } -async function $fetch() { +function $fetch() { + if (!this._fetchPromise) { + this._fetchPromise = $_fetch.call(this) + .then(() => { delete this._fetchPromise }) + } + return this._fetchPromise +} + +async function $_fetch() { this.<%= globals.nuxt %>.nbFetching++ this.$fetchState.pending = true this.$fetchState.error = null diff --git a/packages/vue-app/template/utils.js b/packages/vue-app/template/utils.js index 3d542ab07c..96d89a87ce 100644 --- a/packages/vue-app/template/utils.js +++ b/packages/vue-app/template/utils.js @@ -638,5 +638,7 @@ export function addLifecycleHook(vm, hook, fn) { if (!vm.$options[hook]) { vm.$options[hook] = [] } - vm.$options[hook].push(fn) + if (!vm.$options[hook].includes(fn)) { + vm.$options[hook].push(fn) + } }