Skip to content

Make plugins async #915

Closed
Closed

Description

@MarijnMensinga

For the benefit of bootstrapping plugins it is usefull to make plugins load async.

https://github.com/unjs/nitro/blob/ed3a48026e8a27ecfb86e406ba39bdeb418f8573/src/runtime/app.ts#L89-L91

Can someone explain to me if there is a good reason to load plugins synchronously?

Activity

  1. Harm-Nullix commented on Feb 7, 2023

    @Harm-Nullix
  2. ruslankonev commented on Apr 24, 2023

    @ruslankonev
  3. added
    enhancement
    discussion
    on Aug 6, 2023
  4. fabis94 commented on Sep 27, 2023

    @fabis94
  5. pi0 commented on Sep 27, 2023

    @pi0
    Member

    This issue is on track and I might have some ideas to support it in a nonbreaking change version with slight behavior-only changes. In the meantime, please consider issue 👍🏼 on main issue if you also want this feature 🙏🏼

  6. robertpatrick commented on Oct 17, 2023

    @robertpatrick

    It all seems relatively straightforward. The last step is the only tricky one

    • Convert createNitroApp into an async function that returns Promise<NitroApp>
    • Change the plugin invocations to catch the return value from the plugin
    • If the return value is a Promise, call await Promise.all([pluginPromise])
    • change the export for nitroApp so that it is initialized when the promise is resolved.

    This will ensure that the semantics of order and waiting for one plugin to finish before the next runs are retained. I guess I don't understand why this is complicated...

  7. pi0 commented on Oct 17, 2023

    @pi0
    Member

    Sadly, it is not that straightforward without behavior changes / overhead because not all nitro presets are compatible with an async entry today but thanks for your feedback dear @robertpatrick.

  8. added
    breaking change
    on May 16, 2024
  9. ElYaiko commented on Dec 7, 2025

    @ElYaiko

    Still not an update?
    I can't await for plugins in order even with prefix 0,1...

  10. fabis94 commented on Aug 21, 2026

    @fabis94

    any update around this regarding the new major version? its seriously very odd that you can not asynchronously initialize the server through plugins - where are you expected to run async DB/redis init etc.?

  11. pi0 commented on Aug 21, 2026

    @pi0
    Member

    you can always use top level await in server.ts entrypoint now or inside any plugin or handler (they lazyily init). just be-aware by doing this, server startup will be blocked and you limit runtime portability since sadly till today top level await is not widely supported.

  12. boenrobot commented on Sep 1, 2026

    @boenrobot

    Is there a good way to obtain the NitroApp instance and pass it to an async function though?

    I do want to block server startup until my async handler is done. That's the intention. Limited runtime portability is unfortunate, but acceptable tradeoff... but I also need to be able to hook into nitro to add a request hook, which in turn needs access to the already awaited async function (think DB connection, but also applicable to any network connection you want to initialize once and hook into on request).

    The only workaround I can think of, with top level await in mind, would be to have two plugins. First one to feed the NitroApp to an object sitting at a 3rd file. Then a second plugin to then get it from that same object, and actually block itself with a top level await. The plugin function of the second plugin would be a no-op or otherwise register hooks with access to any values awaited during the top level await... But that seems super clunky, not to mention error prone (as you're relying on order of plugins).

    What if there was a hook for server start (that can be async)? Called once after all plugins are initialized, but before listen (and thus before any invocations of any request hooks or routes). It's certainly backwards compatible, but is it portable?

  13. added and removed on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changediscussionenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions