From 47ac6bb84347a72d276bfe42a8ad56cc4f646ad7 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Fri, 23 Aug 2024 00:08:19 +0200 Subject: [PATCH 1/5] fix(nuxt): don't wrap setup with runWithContext --- packages/nuxt/src/app/composables/component.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index ebc234226c..f508c45b54 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -47,8 +47,7 @@ export const defineNuxtComponent: typeof defineComponent = _fetchKeyBase: key, ...options, setup (props, ctx) { - const nuxtApp = useNuxtApp() - const res = setup ? Promise.resolve(nuxtApp.runWithContext(() => setup(props, ctx))).then(r => r || {}) : {} + const res = setup ? setup(props, ctx) : {} const promises: Promise[] = [] if (options.asyncData) { From 1ac48fe0b771baba8a50b1a06952717395f5ab68 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Fri, 23 Aug 2024 00:14:54 +0200 Subject: [PATCH 2/5] fix: result --- packages/nuxt/src/app/composables/component.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index f508c45b54..c0eb7a5d6d 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -48,7 +48,7 @@ export const defineNuxtComponent: typeof defineComponent = ...options, setup (props, ctx) { const res = setup ? setup(props, ctx) : {} - + const result = Promise.resolve(res).then(() => res || {}) const promises: Promise[] = [] if (options.asyncData) { promises.push(runLegacyAsyncData(res, options.asyncData)) @@ -59,9 +59,9 @@ export const defineNuxtComponent: typeof defineComponent = useHead(typeof options.head === 'function' ? () => options.head(nuxtApp) : options.head) } - return Promise.resolve(res) + return Promise.resolve(result) .then(() => Promise.all(promises)) - .then(() => res) + .then(() => result) .finally(() => { promises.length = 0 }) From 3161c1545eb2835dc7c9a12c83bc292ca6f36172 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Fri, 23 Aug 2024 00:16:33 +0200 Subject: [PATCH 3/5] fix: move back into Promise.resolve --- packages/nuxt/src/app/composables/component.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index c0eb7a5d6d..d6b2773ee2 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -47,8 +47,7 @@ export const defineNuxtComponent: typeof defineComponent = _fetchKeyBase: key, ...options, setup (props, ctx) { - const res = setup ? setup(props, ctx) : {} - const result = Promise.resolve(res).then(() => res || {}) + const res = setup ? Promise.resolve(setup(props, ctx)).then(r => r || {}) : {} const promises: Promise[] = [] if (options.asyncData) { promises.push(runLegacyAsyncData(res, options.asyncData)) @@ -59,9 +58,9 @@ export const defineNuxtComponent: typeof defineComponent = useHead(typeof options.head === 'function' ? () => options.head(nuxtApp) : options.head) } - return Promise.resolve(result) + return Promise.resolve(res) .then(() => Promise.all(promises)) - .then(() => result) + .then(() => res) .finally(() => { promises.length = 0 }) From 2a47785dddb653f59372ebe06f5500f905730f6c Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Sat, 24 Aug 2024 17:27:27 +0200 Subject: [PATCH 4/5] fix: wrap legacyasyncdata with runWithCOntext --- packages/nuxt/src/app/composables/component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index d6b2773ee2..a7fceef52d 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -47,10 +47,11 @@ export const defineNuxtComponent: typeof defineComponent = _fetchKeyBase: key, ...options, setup (props, ctx) { + const nuxtApp = useNuxtApp() const res = setup ? Promise.resolve(setup(props, ctx)).then(r => r || {}) : {} const promises: Promise[] = [] if (options.asyncData) { - promises.push(runLegacyAsyncData(res, options.asyncData)) + promises.push(nuxtApp.runWithContext(() => runLegacyAsyncData(res, options.asyncData))) } if (options.head) { From 77a96a27f41f7e222a15b7f1c8ac42cc53c84b9e Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Sat, 24 Aug 2024 18:04:05 +0200 Subject: [PATCH 5/5] fix: try call instance related composables earlier --- packages/nuxt/src/app/composables/component.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index a7fceef52d..96a8be456e 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -1,5 +1,5 @@ import { getCurrentInstance, reactive, toRefs } from 'vue' -import type { DefineComponent, defineComponent } from 'vue' +import type { ComponentInternalInstance, DefineComponent, defineComponent } from 'vue' import { useHead } from '@unhead/vue' import type { NuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt' @@ -9,10 +9,8 @@ import { createError } from './error' export const NuxtComponentIndicator = '__nuxt_component' -async function runLegacyAsyncData (res: Record | Promise>, fn: (nuxtApp: NuxtApp) => Promise>) { +async function runLegacyAsyncData (res: Record | Promise>, vm: ComponentInternalInstance, route: ReturnType, fn: (nuxtApp: NuxtApp) => Promise>) { const nuxtApp = useNuxtApp() - const route = useRoute() - const vm = getCurrentInstance()! const { fetchKey, _fetchKeyBase } = vm.proxy!.$options const key = (typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey) || ([_fetchKeyBase, route.fullPath, route.matched.findIndex(r => Object.values(r.components || {}).includes(vm.type))].join(':')) @@ -51,7 +49,9 @@ export const defineNuxtComponent: typeof defineComponent = const res = setup ? Promise.resolve(setup(props, ctx)).then(r => r || {}) : {} const promises: Promise[] = [] if (options.asyncData) { - promises.push(nuxtApp.runWithContext(() => runLegacyAsyncData(res, options.asyncData))) + const route = useRoute() + const vm = getCurrentInstance()! + promises.push(nuxtApp.runWithContext(() => runLegacyAsyncData(res, vm, route, options.asyncData))) } if (options.head) {