Skip to content

[RFC] Server-timing utility #651

Open

Description

@TheAlexLichter

Describe the feature

Hey all 👋🏻

right now, Nitro has very low-level support for adding Server-Timing headers. While it is possible to use, it feels like DX could be improved a lot here. Thus, I'd like to suggest this RFC!

Current state

As mentioned, the support for adding server timing already exists and is realized via globalThis. This works, but has some shortcomings:

export default defineEventHandler(async () => {
  globalThis.__timing__.logStart('MY-LABEL')
  someFunction() // Do something here
  globalThis.__timing__.logEnd('MY-LABEL')
})
  1. TypeScript support only work by defining types on your own via addition .d.ts file:
// env.d.ts

declare global {
  var __timing__: {
    logStart: (id: string) => void
    logEnd: (id: string) => void
  }
}

export { }
  1. No documentation. While the timing option is documented, there is no example or guide on how to actually use it.

  2. When timing is disabled, the above code will fail because no __timing__ object will be injected via timing middleware.

  3. Lengthy code. Because the current API is rather low-level, the code is repetitive.

  4. With the above code, server timings are not cleared and will persist through requests every now and then, mainly in dev.

Idea

Instead of using the low-level API directly, I propose to add a utility which will use the API under the hood but has improved DX, full TS support and will do nothing when timing is disabled. Ideally, it will even be treeshaken in this case (and replaced with a no-op function)

API

For the API, I was thinking about the following:

  • withServerTiming(fn, label) - A "wrapper" function, that will start server timing tracking before the function and will stop it after it was executed.

  • startServerTiming(label) - Starts server timing with certain label

  • stopServerTiming(label) - Stops server timing with certain label

Summary

This RFC proposes a Nitro utility to improve DX when adding Server-Timing headers. It will do nothing, when timing is disabled and will add info otherwise.

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. added
    enhancement
    on Nov 14, 2022
  2. danielroe commented on Nov 14, 2022

    @danielroe
    Member

    I love it! Seems good to me. @pi0?

  3. atinux commented on Jan 27, 2023

    @atinux
    Member

    Looks great to me!

  4. TheAlexLichter commented on Feb 10, 2023

    @TheAlexLichter
    MemberAuthor

    Short bullet points from in-person discussion with @pi0:

    • Revamp consola to work at runtime
    • Add timing util into consola
    • At some point, introduce event.$log (like event.$fetch) utilizing consola
  5. or2e commented on Aug 6, 2023

    @or2e
    Contributor

    It would be great to control timings in prod-mode with an env such as NITRO_TIMING

  6. deleted a comment from pi0x on Sep 8, 2026
  7. added
    dx
    on Oct 15, 2026
  8. deleted a comment from pi0x on Oct 18, 2026
  9. reopened this on Oct 18, 2026
  10. removed
    enhancement
    on Nov 10, 2026
  11. 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

    dx

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions