From ee7f7a04aaacf30cb9bcd9082dbf8de7c218e113 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 9 Aug 2022 22:36:05 +0100 Subject: [PATCH 1/3] fix(nuxt): provide nuxt app to asyncdata handler --- packages/nuxt/src/app/composables/asyncData.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 91042dd1db4..3994f1477b8 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -1,7 +1,7 @@ import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref } from 'vue' import type { Ref, WatchSource } from 'vue' import { wrapInRef } from './utils' -import { NuxtApp, useNuxtApp } from '#app' +import { NuxtApp, useNuxtApp, callWithNuxt } from '#app' export type _Transform = (input: Input) => Output @@ -132,7 +132,7 @@ export function useAsyncData< asyncData.pending.value = true // TODO: Cancel previous promise nuxt._asyncDataPromises[key] = Promise.resolve() - .then(() => handler(nuxt)) + .then(() => callWithNuxt(nuxt, handler, [nuxt])) .then((result) => { if (options.transform) { result = options.transform(result) From 5a570911e61cc0b08e14b03be185554057072776 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 9 Aug 2022 22:43:39 +0100 Subject: [PATCH 2/3] refactor: use promise try/catch --- packages/nuxt/src/app/composables/asyncData.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 3994f1477b8..37f96d7f40a 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -131,8 +131,14 @@ export function useAsyncData< } asyncData.pending.value = true // TODO: Cancel previous promise - nuxt._asyncDataPromises[key] = Promise.resolve() - .then(() => callWithNuxt(nuxt, handler, [nuxt])) + nuxt._asyncDataPromises[key] = new Promise( + (resolve, reject) => { + try { + resolve(handler(nuxt)) + } catch (err) { + reject(err) + } + }) .then((result) => { if (options.transform) { result = options.transform(result) From 19cc72cee3f25f5dd100194b915b3103c3bdb237 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 9 Aug 2022 22:45:20 +0100 Subject: [PATCH 3/3] fix: remove unused import --- packages/nuxt/src/app/composables/asyncData.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 37f96d7f40a..ab01e52a864 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -1,7 +1,7 @@ import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref } from 'vue' import type { Ref, WatchSource } from 'vue' import { wrapInRef } from './utils' -import { NuxtApp, useNuxtApp, callWithNuxt } from '#app' +import { NuxtApp, useNuxtApp } from '#app' export type _Transform = (input: Input) => Output