From 16b769d6b9455674f531a0d23ce73c6a62d12d28 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 26 Aug 2021 20:57:36 +0200 Subject: [PATCH] fix(nuxt3): add types for runtime hooks (and fix `page:finished`) (#455) Co-authored-by: Pooya Parsa --- packages/nuxt3/src/app/entry.ts | 2 +- packages/nuxt3/src/app/nuxt.ts | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/nuxt3/src/app/entry.ts b/packages/nuxt3/src/app/entry.ts index 8c51edf92c..2b1e5106a0 100644 --- a/packages/nuxt3/src/app/entry.ts +++ b/packages/nuxt3/src/app/entry.ts @@ -44,7 +44,7 @@ if (process.client) { await nuxt.hooks.callHook('app:created', app) await nuxt.hooks.callHook('app:beforeMount', app) - nuxt.hooks.hookOnce('page:finished', () => { + nuxt.hooks.hookOnce('page:finish', () => { nuxt.isHydrating = false }) diff --git a/packages/nuxt3/src/app/nuxt.ts b/packages/nuxt3/src/app/nuxt.ts index 5b3b7d22f9..8349d8fea3 100644 --- a/packages/nuxt3/src/app/nuxt.ts +++ b/packages/nuxt3/src/app/nuxt.ts @@ -1,5 +1,5 @@ import { getCurrentInstance } from 'vue' -import type { App } from 'vue' +import type { App, VNode } from 'vue' import Hookable from 'hookable' import { defineGetter } from './utils' import { legacyPlugin, LegacyContext } from './legacy' @@ -13,13 +13,32 @@ type NuxtMeta = { bodyScripts?: string } +type HookResult = Promise | void +export interface RuntimeNuxtHooks { + 'app:created': (app: App) => HookResult + 'app:beforeMount': (app: App) => HookResult + 'app:mounted': (app: App) => HookResult + 'app:rendered': () => HookResult + 'page:start': (Component?: VNode) => HookResult + 'page:finish': (Component?: VNode) => HookResult +} +type NuxtAppHookName = keyof RuntimeNuxtHooks + export interface Nuxt { app: App globalName: string - hooks: Hookable - hook: Hookable['hook'] - callHook: Hookable['callHook'] + hooks: { + /** Register a function to be run when the named Nuxt hook is called. */ + hook (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult + hookOnce (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult + /** Run all Nuxt hooks that have been registered against the hook name. */ + callHook (hookName: Hook, ...args: Parameters): ReturnType + /** Add all hooks in the object passed in. */ + addHooks (hooks: Partial): void + } + hook: Nuxt['hooks']['hook'] + callHook: Nuxt['hooks']['callHook'] [key: string]: any