[RFC] Server-timing utility
Nobody has claimed this yet.
- 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')
})
- TypeScript support only work by defining types on your own via addition
.d.tsfile:
// env.d.ts
declare global {
var __timing__: {
logStart: (id: string) => void
logEnd: (id: string) => void
}
}
export { }
-
No documentation. While the timing option is documented, there is no example or guide on how to actually use it.
-
When
timingis disabled, the above code will fail because no__timing__object will be injected via timing middleware. -
Lengthy code. Because the current API is rather low-level, the code is repetitive.
-
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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