nitrojs / nitrojs/nitro

[RFC] Server-timing utility

Open
#651 4 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement p2-nice-to-have
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/runtime/timing.ts and the documented timing option to understand the existing globalThis API and middleware behavior. Compare the RFC's withServerTiming, startServerTiming, and stopServerTiming proposal against the disabled-timing and request-cleanup cases. Done means the utility has typed usage, safely does nothing when timing is disabled, and includes an example or guide.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.