NuxtN
Nuxt6mo ago
9 replies
Norbiros

Access nitro/nuxt hooks from nuxt module

I cannot use await (because it means breaking changes).
function getHooks(): Hookable<GlobalFetchHooks & ClientFetchHooks> | null {
  try {
    // @ts-ignore
    const nuxtApp = useNuxtApp()
    if (nuxtApp) {
      return nuxtApp.hooks as Hookable<GlobalFetchHooks | ClientFetchHooks>
    }
  }
  catch (err) {
    console.error('abc', err)
  }

  try {
    // @ts-ignore
    const nitroApp = useNitroApp()
    return nitroApp.hooks as Hookable<GlobalFetchHooks | ClientFetchHooks>
  }
  catch (err) {
    console.error('abc', err)
  }

  return null
}

I have code like this but it kinda does not work without imports, if I add
import { useNitroApp, useNuxtApp } from '#app'

i get
[nitro 5:15:34 PM]  ERROR  RollupError: [plugin impound] Vue app aliases are not allowed in server runtime. [importing #app from ../../Development/SideProjects/nuxt-open-fetch/dist/runtime/fetch.js]
so how to access the hooks and be able to register them
Was this page helpful?