Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run one plugin after other #1768

Closed
imcm7 opened this issue Sep 27, 2023 · 8 comments
Closed

How to run one plugin after other #1768

imcm7 opened this issue Sep 27, 2023 · 8 comments
Labels
question Further information is requested

Comments

@imcm7
Copy link

imcm7 commented Sep 27, 2023

Environment

Reproduction

https://nitro.unjs.io/guide/plugins#available-hooks

export default defineNitroPlugin(async (nitroApp: NitroApp) => {
   nitroApp.hooks.hook('request', async (event: H3Event) => {
        event.context.data // <------------------- is empty because that plugin that is second finish first his task
   });
})

Describe the bug

I have 2 plugins, but second finish first (make more easy task) and I can get in second plugin result from first, event.context is empty in request hook.

Additional context

No response

Logs

No response

@imcm7
Copy link
Author

imcm7 commented Sep 27, 2023

Need one more hook like before request to use different hooks in each plugin.
beforeResponse hook is too late

@imcm7
Copy link
Author

imcm7 commented Sep 27, 2023

or to not make it async? whe I need a hook like onInit to await in it what I need.

@imcm7
Copy link
Author

imcm7 commented Sep 27, 2023

And in nitroApp.hooks.hookOnce('close', async () => {} no event, but I want to do some think with objects from event.context when app is closing

@Hebilicious
Copy link
Contributor

Environment

Reproduction

nitro.unjs.io/guide/plugins#available-hooks

export default defineNitroPlugin(async (nitroApp: NitroApp) => {
   nitroApp.hooks.hook('request', async (event: H3Event) => {
        event.context.data // <------------------- is empty because that plugin that is second finish first his task
   });
})

Describe the bug

I have 2 plugins, but second finish first (make more easy task) and I can get in second plugin result from first, event.context is empty in request hook.

Additional context

No response

Logs

No response

You can order the plugins, it's written in the docs you linked :

image

@Hebilicious Hebilicious added question Further information is requested and removed pending triage labels Sep 27, 2023
@imcm7
Copy link
Author

imcm7 commented Sep 27, 2023

@Hebilicious that not help if it is async, and I need to await in it for db connection, and no at leas beforeRequet hook to use in one beforeReques and in other request hook

I will move code from plugin to hook in plugin and make plugin not async

nitroApp.hooks.hookOnce('request', async(event: H3Event) => {
        event.context.orm = awaitconnect to bd
    });

But ca't stop bd

nitroApp.hooks.hookOnce('close', async () => {
  event.context.bd.stop  ???????????????
}
``

@imcm7
Copy link
Author

imcm7 commented Sep 27, 2023

@Hebilicious also if I have 2 hooks in one plugin, event data is not the same (pass by reference or reactivity)

 nitroApp.hooks.hookOnce('request', async(event: H3Event) => {
   event.context.data = 1;
}

 nitroApp.hooks.hook('request', async(event: H3Event) => {
   event.context.data // is empty ???????????????
}

@robertpatrick
Copy link

@Hebilicious I agree with @imcm7 that nitro needs to support async plugins. I have async plugin code that runs at server startup but because nitro does not wait for async plugins to complete, I have a complex race condition that I have to solve to prevent API requests from trying to access the data structures being built by the plugins.

@Hebilicious
Copy link
Contributor

Hebilicious commented Oct 17, 2023

@robertpatrick I agree. For async plugins you can track the progress here :

@imcm7 I think this can be closed now ?

@imcm7 imcm7 closed this as completed Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants