Skip to content

Commit 7faa5cd

Browse files
📝 Add docstrings to codex/implement-web-vitals-tracking-and-api-jt9zi9 (#284)
Docstrings generation was requested by @shayancoin. * #125 (comment) The following files were modified: * `frontend/src/app/api/otel/webvital/route.ts` * `frontend/src/app/providers.tsx` * `frontend/src/app/web-vitals.ts` Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Shayan <shayan@coin.link>
1 parent 27e045d commit 7faa5cd

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

frontend/src/app/providers.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ const theme = extendTheme({
2828
},
2929
});
3030

31+
/**
32+
* Wraps the application UI with a ChakraProvider using the project theme and initializes web vitals on mount.
33+
*
34+
* Calls `initWebVitals` once when mounted to start web vitals collection.
35+
*
36+
* @param children - The React node(s) to render within the themed provider
37+
* @returns The React element tree where `children` are wrapped by a `ChakraProvider` configured with the module theme
38+
*/
3139
export function Providers({ children }: { children: React.ReactNode }) {
3240
useEffect(() => {
3341
initWebVitals();
3442
}, []);
3543

3644
return <ChakraProvider theme={theme}>{children}</ChakraProvider>;
37-
}
45+
}

frontend/src/app/web-vitals.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ function scheduleFlush() {
5353
}, FLUSH_INTERVAL_MS)
5454
}
5555

56+
/**
57+
* Sends all queued web-vital and navigation-timing events to the server and clears the in-memory queue.
58+
*
59+
* If the queue is empty this is a no-op. Attempts to deliver using `navigator.sendBeacon` when available;
60+
* otherwise falls back to a POST request with `keepalive`. On network failure, logs a warning in non-production
61+
* environments and restores the events to the front of the queue so they are not lost.
62+
*/
5663
async function flushQueue() {
5764
if (queue.length === 0) {
5865
return
@@ -223,6 +230,12 @@ function installLifecycleFlush() {
223230
window.addEventListener('beforeunload', flush)
224231
}
225232

233+
/**
234+
* Initializes collection and reporting of web-vitals and navigation timing data.
235+
*
236+
* This function is idempotent: calling it multiple times has no additional effect.
237+
* It is a no-op outside of a browser environment.
238+
*/
226239
export function initWebVitals() {
227240
if (typeof window === 'undefined') {
228241
return

0 commit comments

Comments
 (0)