From c5d6db7fd0e09f4077537443e79638a1383b3730 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Jan 2023 11:13:21 +0000 Subject: [PATCH] fix(nuxt): await async `callWithNuxt` calls (#18443) --- packages/nuxt/src/app/components/nuxt-root.vue | 3 ++- packages/nuxt/src/app/nuxt.ts | 4 ++-- packages/nuxt/src/pages/runtime/plugins/router.ts | 6 +++--- test/fixtures/basic/types.ts | 9 ++++++++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-root.vue b/packages/nuxt/src/app/components/nuxt-root.vue index 3cc5167472..789afd31e4 100644 --- a/packages/nuxt/src/app/components/nuxt-root.vue +++ b/packages/nuxt/src/app/components/nuxt-root.vue @@ -33,7 +33,8 @@ const error = useError() onErrorCaptured((err, target, info) => { nuxtApp.hooks.callHook('vue:error', err, target, info).catch(hookError => console.error('[nuxt] Error in `vue:error` hook', hookError)) if (process.server || (isNuxtError(err) && (err.fatal || err.unhandled))) { - callWithNuxt(nuxtApp, showError, [err]) + const p = callWithNuxt(nuxtApp, showError, [err]) + onServerPrefetch(() => p) } }) diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 93243efc8c..3b3ad1f165 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -284,9 +284,9 @@ export function isNuxtPlugin (plugin: unknown) { * @param setup The function to call */ export function callWithNuxt any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters) { - const fn = () => args ? setup(...args as Parameters) : setup() + const fn: () => ReturnType = () => args ? setup(...args as Parameters) : setup() if (process.server) { - return nuxtAppCtx.callAsync>(nuxt, fn) + return nuxtAppCtx.callAsync(nuxt, fn) } else { // In client side we could assume nuxt app is singleton nuxtAppCtx.set(nuxt) diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 396945b7ab..f2622b9a2d 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -113,7 +113,7 @@ export default defineNuxtPlugin(async (nuxtApp) => { await router.isReady() } catch (error: any) { // We'll catch 404s here - callWithNuxt(nuxtApp, showError, [error]) + await callWithNuxt(nuxtApp, showError, [error]) } const initialLayout = useState('_layout') @@ -171,7 +171,7 @@ export default defineNuxtPlugin(async (nuxtApp) => { await callWithNuxt(nuxtApp, clearError) } if (to.matched.length === 0) { - callWithNuxt(nuxtApp, showError, [createError({ + await callWithNuxt(nuxtApp, showError, [createError({ statusCode: 404, fatal: false, statusMessage: `Page not found: ${to.fullPath}` @@ -193,7 +193,7 @@ export default defineNuxtPlugin(async (nuxtApp) => { }) } catch (error: any) { // We'll catch middleware errors or deliberate exceptions here - callWithNuxt(nuxtApp, showError, [error]) + await callWithNuxt(nuxtApp, showError, [error]) } }) diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 7f84779e31..e1772e7623 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -5,7 +5,7 @@ import type { AppConfig } from '@nuxt/schema' import type { FetchError } from 'ofetch' import type { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router' -import { isVue3 } from '#app' +import { callWithNuxt, isVue3 } from '#app' import type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router' import { defineNuxtConfig } from '~~/../../../packages/nuxt/config' import { useRouter } from '#imports' @@ -224,3 +224,10 @@ describe('extends type declarations', () => { expectTypeOf().toEqualTypeOf<{ foo: 'bar' }>() }) }) + +describe('composables inference', () => { + it('callWithNuxt', () => { + const bob = callWithNuxt({} as any, () => true) + expectTypeOf().toEqualTypeOf>() + }) +})