fix(nuxt): await async callWithNuxt calls (#18443)

This commit is contained in:
Daniel Roe 2023-01-23 11:13:21 +00:00 committed by GitHub
parent 022c95269e
commit c5d6db7fd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 7 deletions

View File

@ -33,7 +33,8 @@ const error = useError()
onErrorCaptured((err, target, info) => { onErrorCaptured((err, target, info) => {
nuxtApp.hooks.callHook('vue:error', err, target, info).catch(hookError => console.error('[nuxt] Error in `vue:error` hook', hookError)) 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))) { if (process.server || (isNuxtError(err) && (err.fatal || err.unhandled))) {
callWithNuxt(nuxtApp, showError, [err]) const p = callWithNuxt(nuxtApp, showError, [err])
onServerPrefetch(() => p)
} }
}) })

View File

@ -284,9 +284,9 @@ export function isNuxtPlugin (plugin: unknown) {
* @param setup The function to call * @param setup The function to call
*/ */
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) { export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
const fn = () => args ? setup(...args as Parameters<T>) : setup() const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
if (process.server) { if (process.server) {
return nuxtAppCtx.callAsync<ReturnType<T>>(nuxt, fn) return nuxtAppCtx.callAsync(nuxt, fn)
} else { } else {
// In client side we could assume nuxt app is singleton // In client side we could assume nuxt app is singleton
nuxtAppCtx.set(nuxt) nuxtAppCtx.set(nuxt)

View File

@ -113,7 +113,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
await router.isReady() await router.isReady()
} catch (error: any) { } catch (error: any) {
// We'll catch 404s here // We'll catch 404s here
callWithNuxt(nuxtApp, showError, [error]) await callWithNuxt(nuxtApp, showError, [error])
} }
const initialLayout = useState('_layout') const initialLayout = useState('_layout')
@ -171,7 +171,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
await callWithNuxt(nuxtApp, clearError) await callWithNuxt(nuxtApp, clearError)
} }
if (to.matched.length === 0) { if (to.matched.length === 0) {
callWithNuxt(nuxtApp, showError, [createError({ await callWithNuxt(nuxtApp, showError, [createError({
statusCode: 404, statusCode: 404,
fatal: false, fatal: false,
statusMessage: `Page not found: ${to.fullPath}` statusMessage: `Page not found: ${to.fullPath}`
@ -193,7 +193,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
}) })
} catch (error: any) { } catch (error: any) {
// We'll catch middleware errors or deliberate exceptions here // We'll catch middleware errors or deliberate exceptions here
callWithNuxt(nuxtApp, showError, [error]) await callWithNuxt(nuxtApp, showError, [error])
} }
}) })

View File

@ -5,7 +5,7 @@ import type { AppConfig } from '@nuxt/schema'
import type { FetchError } from 'ofetch' import type { FetchError } from 'ofetch'
import type { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router' 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 type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router'
import { defineNuxtConfig } from '~~/../../../packages/nuxt/config' import { defineNuxtConfig } from '~~/../../../packages/nuxt/config'
import { useRouter } from '#imports' import { useRouter } from '#imports'
@ -224,3 +224,10 @@ describe('extends type declarations', () => {
expectTypeOf<import('bing').BingInterface>().toEqualTypeOf<{ foo: 'bar' }>() expectTypeOf<import('bing').BingInterface>().toEqualTypeOf<{ foo: 'bar' }>()
}) })
}) })
describe('composables inference', () => {
it('callWithNuxt', () => {
const bob = callWithNuxt({} as any, () => true)
expectTypeOf<typeof bob>().toEqualTypeOf<boolean | Promise<boolean>>()
})
})