afterResponse and beforeResponse hook doesnt trigger on API call

these hooks, doesnt triggered on API call, only work when user visit on browser
nitroApp.hooks.hook('beforeResponse', (event) => {
consola.info('on before response', event.path)
})

nitroApp.hooks.hook('afterResponse', (event) => {
consola.info('on after response', event.path)
})
nitroApp.hooks.hook('beforeResponse', (event) => {
consola.info('on before response', event.path)
})

nitroApp.hooks.hook('afterResponse', (event) => {
consola.info('on after response', event.path)
})
is there a hook for API, that will executed afterResponse?
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @putrafajarh. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The knowledge sources indicate that the Nitro server in Nuxt provides several hooks, including beforeResponse and afterResponse, which are designed to be called before and after sending the response, respectively. These hooks are available for server-side runtime and can be used in server plugins. The documentation shows usage like this:
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
console.log("on response", event.path, { body });
});

nitroApp.hooks.hook("afterResponse", (event, { body }) => {
console.log("on after response", event.path, { body });
});
});
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
console.log("on response", event.path, { body });
});

nitroApp.hooks.hook("afterResponse", (event, { body }) => {
console.log("on after response", event.path, { body });
});
});
However, the documentation does not explicitly state that these hooks are limited to only page (HTML) responses or that they do not trigger for API routes. There is no clear information in the provided sources about a dedicated hook that is guaranteed to run after every API response, regardless of whether it's a page or an API endpoint. The hooks listed (such as render:response and render:html) are more focused on SSR and HTML rendering, not API responses specifically. If beforeResponse and afterResponse are not triggering for your API routes, the knowledge sources do not provide an alternative hook specifically for API responses. You may want to review the Nitro documentation on hooks for any updates, but based on the current sources, there is no explicit mention of a hook that is guaranteed to run after every API response. In summary:
I'm sorry, but the knowledge sources do not explicitly mention a Nitro hook that is guaranteed to execute after every API response if beforeResponse and afterResponse are not triggering for API calls. The available documentation only shows these hooks in the context of general server responses, and there is no mention of a dedicated API-specific post-response hook in the provided sources Docs: Nitro App Hooks (runtime, server-side) Docs: addServerPlugin usage. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@458124545057095681> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?