Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit da75f0b

Browse files
committed
track perf
1 parent 41e0a76 commit da75f0b

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed

package/src/client.csr.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ describe("when cookie is not set", () => {
5555
}
5656
);
5757

58+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
59+
5860
configure({ envKey: "flags_pub_000000" });
5961
expect(document.cookie).toEqual("");
6062

@@ -143,6 +145,7 @@ describe("when cookie is set", () => {
143145
},
144146
}
145147
);
148+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
146149

147150
configure({ envKey: "flags_pub_000000" });
148151
expect(document.cookie).toEqual(`hkvk=${visitorKeyInCookie}`);
@@ -234,6 +237,8 @@ describe("stories", () => {
234237
}
235238
);
236239

240+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
241+
237242
configure({ envKey: "flags_pub_000000" });
238243
expect(document.cookie).toEqual("");
239244

package/src/client.ssg.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe("when cookie is not set", () => {
5454
};
5555
}
5656
);
57+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
5758

5859
configure({ envKey: "flags_pub_000000" });
5960
expect(document.cookie).toEqual("");
@@ -202,6 +203,7 @@ describe("when cookie is set", () => {
202203
visitor: { key: visitorKeyInCookie },
203204
}
204205
);
206+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
205207

206208
configure({ envKey: "flags_pub_000000" });
207209

package/src/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ObjectMap,
2929
has,
3030
} from "./utils";
31+
import { perfContent } from "./track-perf";
3132

3233
export type {
3334
FlagUser,
@@ -503,6 +504,7 @@ export function useFlags<F extends Flags = Flags>(
503504
);
504505
}
505506

507+
const before = Date.now();
506508
fetch([input.endpoint, input.envKey].join("/"), {
507509
method: "POST",
508510
headers: { "content-type": "application/json" },
@@ -512,6 +514,20 @@ export function useFlags<F extends Flags = Flags>(
512514
(response) => {
513515
clearTimeout(timeoutId);
514516

517+
if (navigator.sendBeacon) {
518+
navigator.sendBeacon(
519+
"https://happykit.dev/api/flags-perf",
520+
perfContent({
521+
duration: Date.now() - before,
522+
location: "client",
523+
envKey: input.envKey,
524+
responseStatusCode: response.status,
525+
serverTiming: response.headers.get("Server-Timing"),
526+
cfRay: response.headers.get("CF-RAY"),
527+
})
528+
);
529+
}
530+
515531
if (!response.ok /* response.status is not 200-299 */) {
516532
dispatch({
517533
type: "settle/failure",

package/src/edge.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe("middleware", () => {
6565
},
6666
}
6767
);
68+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
6869

6970
const request = createNextRequest({
7071
cookies: { hkvk: "V1StGXR8_Z5jdHi6B-myT" },
@@ -132,6 +133,8 @@ describe("middleware", () => {
132133
}
133134
);
134135

136+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
137+
135138
const request = createNextRequest({
136139
cookies: { hkvk: "V1StGXR8_Z5jdHi6B-myT" },
137140
});
@@ -214,6 +217,8 @@ describe("middleware", () => {
214217
}
215218
);
216219

220+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
221+
217222
const request = createNextRequest({
218223
cookies: { foo: "bar" },
219224
});

package/src/edge.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
Input,
1515
} from "./types";
1616
import { combineRawFlagsWithDefaultFlags } from "./utils";
17+
import { trackPerf } from "./track-perf";
1718

1819
export type { EvaluationResponseBody } from "./types";
1920

@@ -125,6 +126,7 @@ export function getEdgeFlags<F extends Flags = Flags>(options: {
125126
{ "x-forwarded-for": requestingIp }
126127
: {};
127128

129+
const before = Date.now();
128130
return fetch([input.endpoint, input.envKey].join("/"), {
129131
method: "POST",
130132
headers: Object.assign(
@@ -138,6 +140,15 @@ export function getEdgeFlags<F extends Flags = Flags>(options: {
138140
):
139141
| Promise<GetFlagsSuccessBag<F> | GetFlagsErrorBag<F>>
140142
| GetFlagsErrorBag<F> => {
143+
trackPerf({
144+
duration: Date.now() - before,
145+
location: "edge",
146+
envKey: input.envKey,
147+
responseStatusCode: workerResponse.status,
148+
serverTiming: workerResponse.headers.get("Server-Timing"),
149+
cfRay: workerResponse.headers.get("CF-RAY"),
150+
});
151+
141152
if (!workerResponse.ok /* status not 200-299 */) {
142153
return {
143154
flags: staticConfig.defaultFlags as F,

package/src/server.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ describe("server-side rendering (pure + hybrid)", () => {
4848
}
4949
);
5050

51+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
52+
5153
const context = {
5254
req: {
5355
headers: { cookie: "hkvk=V1StGXR8_Z5jdHi6B-myT" },
@@ -114,6 +116,8 @@ describe("server-side rendering (pure + hybrid)", () => {
114116
}
115117
);
116118

119+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
120+
117121
const context = {
118122
req: {
119123
headers: { cookie: "hkvk=V1StGXR8_Z5jdHi6B-myT" },
@@ -187,6 +191,8 @@ describe("server-side rendering (pure + hybrid)", () => {
187191
}
188192
);
189193

194+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
195+
190196
const context = {
191197
req: {
192198
headers: { cookie: "foo=bar" },
@@ -308,6 +314,8 @@ describe("static site generation (pure + hybrid)", () => {
308314
}
309315
);
310316

317+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
318+
311319
expect(await getFlags({ context: {} as GetStaticPropsContext })).toEqual({
312320
flags: { meal: "large" },
313321
data: {
@@ -354,6 +362,8 @@ describe("static site generation (pure + hybrid)", () => {
354362
}
355363
);
356364

365+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
366+
357367
expect(
358368
await getFlags({
359369
context: {} as GetStaticPropsContext,
@@ -406,6 +416,8 @@ describe("static site generation (pure + hybrid)", () => {
406416
}
407417
);
408418

419+
fetchMock.post(`https://happykit.dev/api/flags-perf`, {});
420+
409421
expect(
410422
await getFlags({
411423
context: {} as GetStaticPropsContext,

package/src/server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
combineRawFlagsWithDefaultFlags,
2626
getCookie,
2727
} from "./utils";
28+
import { trackPerf } from "./track-perf";
2829

2930
export type { EvaluationResponseBody } from "./types";
3031

@@ -145,6 +146,7 @@ export function getFlags<F extends Flags = Flags>(options: {
145146
? null
146147
: setTimeout(() => controller.abort(), timeoutDuration);
147148

149+
const before = Date.now();
148150
return fetch([input.endpoint, input.envKey].join("/"), {
149151
method: "POST",
150152
headers: Object.assign(
@@ -161,6 +163,15 @@ export function getFlags<F extends Flags = Flags>(options: {
161163
| GetFlagsErrorBag<F> => {
162164
if (timeoutId) clearTimeout(timeoutId);
163165

166+
trackPerf({
167+
duration: Date.now() - before,
168+
location: has(options.context, "req") ? "ssr" : "ssg",
169+
envKey: input.envKey,
170+
responseStatusCode: workerResponse.status,
171+
serverTiming: workerResponse.headers.get("Server-Timing"),
172+
cfRay: workerResponse.headers.get("CF-RAY"),
173+
});
174+
164175
if (!workerResponse.ok /* status not 200-299 */) {
165176
return {
166177
flags: staticConfig.defaultFlags as F,

package/src/track-perf.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Options = {
2+
duration: number;
3+
location: "client" | "edge" | "ssr" | "ssg";
4+
envKey: string;
5+
responseStatusCode: number;
6+
serverTiming: string | null;
7+
cfRay: string | null;
8+
};
9+
10+
export function perfContent(options: Options) {
11+
return JSON.stringify(options);
12+
}
13+
14+
export function trackPerf(options: Options) {
15+
return fetch(`https://happykit.dev/api/flags-perf`, {
16+
method: "POST",
17+
headers: { "content-type": "application/json" },
18+
body: perfContent(options),
19+
}).catch(() => null);
20+
}

0 commit comments

Comments
 (0)