Skip to content

provide new hook to be triggered before closing nitro #4428

Closed

Description

@userquin

Describe the feature

Some integrations requires write/build some assets after copying public assets and/or prerender process. My example is PWA integration at tanstack start where we're using nitro v3 vite plugin.

Right now, nitro prerender process can be skipped even being triggered, missing hook about cancelling, for context: since any nitro module can remove all the prerender routes (without crawling), the prerender process is silently removed from the pipeline, but some prerender hooks triggered.

My integration at tanstack start uses nitro compiled hook, but nitro has been closed, and so I have no idea if we can have some side effects.

I guess we should add a new hook before:close to be triggered after prerender:done but before closing nitro here https://github.com/nitrojs/nitro/blob/main/src/build/vite/prod.ts#L110

For more context: at nuxt integration (nuxt <5), we use this logic in the nuxt module:

Nuxt PWA module and nitro hooks to build the service worker
  if (!nuxt.options.dev) {
    if (semver.gte(ctx.nuxt.nuxtVersion, '3.8.0')) {
      nuxt.hook('nitro:build:public-assets', async () => {
        await buildPwaAssets(ctx as unknown as any)
      })
    }
    else {
      nuxt.hook('nitro:init', (nitro) => {
        nitro.hooks.hook('rollup:before', async () => {
          await buildPwaAssets(ctx as unknown as any)
        })
      })
      if (nuxt.options.nitro.static || (nuxt.options as any)._generate /* TODO: remove in future */) {
        nuxt.hook('close', async () => {
          await buildPwaAssets(ctx as unknown as any)
        })
      }
    }
  }

You can see nuxt nitro:build:public-assets hook here (not sure yet how nuxt integrates nitro v3): https://github.com/nuxt/nuxt/blob/c8d7662e549177da82d6b50501b24bf5c08a9155/packages/nitro-server/src/legacy.ts#L17

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. added
    pending triage
    on Jul 14, 2026
  2. added
    discussion
    and removed
    pending triage
    on Jul 14, 2026
  3. userquin commented on Jul 16, 2026

    @userquin
    ContributorAuthor

    We also need to:

    • review crawler, nitro seems to do some work after prerender process (maybe it is just the log)
    • move compressPublicAssets at prerender module after the new before:close hook: any assets written after prerender but before closing nitro won't be compressed
  4. added
    nuxt
    tanstack
    on Sep 2, 2026
  5. pi0x commented on Sep 7, 2026

    @pi0x
    Contributor

    Confirmed, and it looks like a plain ordering bug rather than a missing hook. In the Vite production build Nitro is closed before the compiled hook runs (src/build/vite/prod.ts: await nitro.close() then callHook("compiled")). The CLI/rollup/rolldown path does the opposite — compiled fires inside build(), and nitro.close() comes after. So your compiled handler only sees a closed instance under Vite.

    Posted by an AI triage agent on behalf of a maintainer.

  6. added
    bug
    on Sep 7, 2026
  7. added theissue type on Nov 10, 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

    bugdiscussionnuxttanstack

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions