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

feat: add plugin resolved hook #27618

Open
3 of 4 tasks
hendrikheil opened this issue Jun 14, 2024 · 1 comment · May be fixed by #27619
Open
3 of 4 tasks

feat: add plugin resolved hook #27618

hendrikheil opened this issue Jun 14, 2024 · 1 comment · May be fixed by #27619

Comments

@hendrikheil
Copy link
Contributor

Describe the feature

I've recently worked on support for static analysis of nuxt plugin hooks when using the object syntax. This massively improved hooking for us and allowed us to discover one short-coming of plugin hooks / plugin execution order.

Imagine the following situation: Your Nuxt App uses two modules, one is the official @pinia/nuxt module which is used for creating a store that hold the locale of the application. The other module is one you write yourself and handles authentication. The authentication module allows you to hook into an event whenever the signed in user identity changes. You now want to update your stored locale whenever the locale of the identity changes to reflect the preferences of your user.
To make that happen you create a plugin in your app that listens to the event, gets the store and updates the locale if necessary.

You now realize that this won't work, as you depend on the pinia plugin to have been executed before your hook callback is executed, otherwise there is no active pinia instance. Since the authentication module is generic and doesn't need pinia, it doesn't depend on pinia and might run in parallel with it.

To solve this I'm proposing the following solution

  1. Emit an event whenever a plugin has been resolved, app:plugin:resolved, with a callback parameter of the plugin name.
  2. Keep track of the resolved plugins
  3. Add a nuxtApp method runAfterPlugin that allows a user to register a callback that is executed either immediately (if the plugin has been resolved already) or whenever the app:plugin:resolved event for the relevant plugin is called.

This allows a user to side-step any race-conditions like described above. It is basically the runtime inverse of the statically defined dependsOn. Instead of defining which other plugin you might rely on before executing them, it allows for depending on plugins at runtime.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

@danielroe
Copy link
Member

danielroe commented Aug 9, 2024

I worry that this will add code that's only relevant for an edge case.

Does using enforce: 'post' work for your situation? It should ensure that your plugin runs last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants