@logbrew/next 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/client.cjs +131 -2
- package/client.d.cts +3 -1
- package/client.d.ts +3 -1
- package/client.js +131 -2
- package/edge.cjs +1 -1
- package/examples/client-route-spans.mjs +1 -1
- package/examples/readme-example.mjs +1 -1
- package/examples/real-user-smoke.mjs +1 -1
- package/index.cjs +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -270,7 +270,7 @@ import {
|
|
|
270
270
|
const client = createLogBrewNextBrowserClient({
|
|
271
271
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
272
272
|
sdkName: "my-next-app",
|
|
273
|
-
sdkVersion: "0.1.
|
|
273
|
+
sdkVersion: "0.1.18"
|
|
274
274
|
});
|
|
275
275
|
|
|
276
276
|
const routePatterns = [
|
|
@@ -293,7 +293,7 @@ export function LogBrewNextNavigationSpans({ traceparent }) {
|
|
|
293
293
|
}
|
|
294
294
|
```
|
|
295
295
|
|
|
296
|
-
`useLogBrewNextNavigation` records one `browser.navigation` span named `next.route <routeTemplate>` when `pathname` changes to a route that matches one of your stable `routePatterns`. Use `createNextRouteTemplate` directly when you want to preview how a concrete `pathname` maps to `/projects/[projectId]/settings`, `[...catchAll]`, `[[...optionalCatchAll]]`, or a route-group pattern such as `/(app)/dashboard/[teamId]`.
|
|
296
|
+
`useLogBrewNextNavigation` records one `browser.navigation` span named `next.route <routeTemplate>` when `pathname` changes to a route that matches one of your stable `routePatterns`. LogBrew counts each of these spans as a page view of its route template. Use `createNextRouteTemplate` directly when you want to preview how a concrete `pathname` maps to `/projects/[projectId]/settings`, `[...catchAll]`, `[[...optionalCatchAll]]`, or a route-group pattern such as `/(app)/dashboard/[teamId]`.
|
|
297
297
|
|
|
298
298
|
Pass W3C trace context through `traceparent`, `traceId`, or `traceId` plus `spanId`. A missing span ID comes from `spanIdFactory`, and a navigation without trace input starts a new trace. The hook never throws into your page: when a capture fails, for example on a malformed `traceparent`, it drops that span and calls `onCaptureError` if you pass one. It does not patch `fetch`, `XMLHttpRequest`, browser history, or the Next router. It never records the concrete pathname, query string, hash, headers, cookies, request body, or raw `traceparent` value. If a pathname cannot be matched to a stable route template, it skips the span instead of emitting a high-cardinality URL. Metadata is primitive-only.
|
|
299
299
|
|
|
@@ -339,6 +339,15 @@ query, status), and route changes. Requests to the LogBrew endpoint are
|
|
|
339
339
|
skipped. Pass `breadcrumbs: false` to turn them off. `uninstall()` removes the
|
|
340
340
|
listeners and puts back every patched method.
|
|
341
341
|
|
|
342
|
+
The same call sends the page's Web Vitals as histogram metrics when the page is
|
|
343
|
+
first hidden: `browser.web_vital.lcp`, `browser.web_vital.inp`,
|
|
344
|
+
`browser.web_vital.fcp`, and `browser.web_vital.ttfb` in milliseconds, and
|
|
345
|
+
`browser.web_vital.cls` as a score. Each metric carries a `rating` (`good`,
|
|
346
|
+
`needs-improvement`, or `poor`) and the route template the page loaded on, from
|
|
347
|
+
`useLogBrewNextNavigation`, never the concrete path. The SDK reads the
|
|
348
|
+
browser's own performance entries and adds no dependency. Pass
|
|
349
|
+
`webVitals: false` to turn them off.
|
|
350
|
+
|
|
342
351
|
## Server Client Helper
|
|
343
352
|
|
|
344
353
|
```js
|
|
@@ -347,7 +356,7 @@ import { createLogBrewNextClient } from "@logbrew/next";
|
|
|
347
356
|
const client = createLogBrewNextClient({
|
|
348
357
|
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
349
358
|
sdkName: "my-next-app",
|
|
350
|
-
sdkVersion: "0.1.
|
|
359
|
+
sdkVersion: "0.1.18"
|
|
351
360
|
});
|
|
352
361
|
```
|
|
353
362
|
|
package/client.cjs
CHANGED
|
@@ -13,6 +13,16 @@ const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
|
|
|
13
13
|
// Send 1 second after the first queued event; pagehide flushes the rest when the page closes.
|
|
14
14
|
const DEFAULT_BROWSER_DELIVERY_INTERVAL_MS = 1000;
|
|
15
15
|
const clientEndpoints = new WeakMap();
|
|
16
|
+
// The first route template each client's navigation hook saw: the route the page loaded on.
|
|
17
|
+
const landingRouteTemplates = new WeakMap();
|
|
18
|
+
// Good and poor limits from https://web.dev/articles/vitals.
|
|
19
|
+
const WEB_VITALS = {
|
|
20
|
+
cls: { description: "Cumulative Layout Shift of one page load.", good: 0.1, poor: 0.25, unit: "1" },
|
|
21
|
+
fcp: { description: "First Contentful Paint of one page load.", good: 1800, poor: 3000, unit: "ms" },
|
|
22
|
+
inp: { description: "Interaction to Next Paint of one page load.", good: 200, poor: 500, unit: "ms" },
|
|
23
|
+
lcp: { description: "Largest Contentful Paint of one page load.", good: 2500, poor: 4000, unit: "ms" },
|
|
24
|
+
ttfb: { description: "Time to First Byte of one page load.", good: 800, poor: 1800, unit: "ms" }
|
|
25
|
+
};
|
|
16
26
|
|
|
17
27
|
function createLogBrewNextBrowserClient({
|
|
18
28
|
apiKey,
|
|
@@ -22,7 +32,7 @@ function createLogBrewNextBrowserClient({
|
|
|
22
32
|
endpoint = DEFAULT_ENDPOINT,
|
|
23
33
|
eventFilter,
|
|
24
34
|
sdkName = "logbrew-next-client",
|
|
25
|
-
sdkVersion = "0.1.
|
|
35
|
+
sdkVersion = "0.1.18",
|
|
26
36
|
maxQueueSize,
|
|
27
37
|
maxRetries = 2,
|
|
28
38
|
onEventDropped,
|
|
@@ -72,7 +82,8 @@ function captureNextClientError(client, error, { digest, routeTemplate, flush =
|
|
|
72
82
|
|
|
73
83
|
// Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide.
|
|
74
84
|
// Console, fetch, XHR, and route-change breadcrumbs ride along with each issue; breadcrumbs: false turns them off.
|
|
75
|
-
|
|
85
|
+
// Web Vitals go out as metrics when the page is first hidden; webVitals: false turns them off.
|
|
86
|
+
function installLogBrewNextClientErrorCapture({ breadcrumbs = true, browserWindow = globalThis.window, client, webVitals = true } = {}) {
|
|
76
87
|
if (!client || !browserWindow || typeof browserWindow.addEventListener !== "function") {
|
|
77
88
|
throw new SdkError("configuration_error", "installLogBrewNextClientErrorCapture requires a client and a browser window");
|
|
78
89
|
}
|
|
@@ -100,9 +111,11 @@ function installLogBrewNextClientErrorCapture({ breadcrumbs = true, browserWindo
|
|
|
100
111
|
const uninstallBreadcrumbs = breadcrumbs === false
|
|
101
112
|
? () => undefined
|
|
102
113
|
: installBrowserBreadcrumbs(browserWindow, client, { ignoredUrls: [clientEndpoints.get(client) ?? DEFAULT_ENDPOINT] });
|
|
114
|
+
const uninstallWebVitals = webVitals === false ? () => undefined : installWebVitals(browserWindow, client);
|
|
103
115
|
return {
|
|
104
116
|
uninstall() {
|
|
105
117
|
uninstallBreadcrumbs();
|
|
118
|
+
uninstallWebVitals();
|
|
106
119
|
for (const [type, listener] of Object.entries(listeners)) {
|
|
107
120
|
browserWindow.removeEventListener?.(type, listener);
|
|
108
121
|
}
|
|
@@ -110,6 +123,116 @@ function installLogBrewNextClientErrorCapture({ breadcrumbs = true, browserWindo
|
|
|
110
123
|
};
|
|
111
124
|
}
|
|
112
125
|
|
|
126
|
+
// Observe the page's performance entries the way https://github.com/GoogleChrome/web-vitals does,
|
|
127
|
+
// without the library, and record each Web Vital once, when the page is first hidden.
|
|
128
|
+
function installWebVitals(browserWindow, client) {
|
|
129
|
+
const { PerformanceObserver: Observer, document, performance } = browserWindow;
|
|
130
|
+
if (typeof Observer !== "function" || !document || typeof document.addEventListener !== "function") {
|
|
131
|
+
return () => undefined;
|
|
132
|
+
}
|
|
133
|
+
const navigation = performance?.getEntriesByType?.("navigation")?.[0];
|
|
134
|
+
const sinceActivation = (time) => Math.max(time - (navigation?.activationStart ?? 0), 0);
|
|
135
|
+
const values = {};
|
|
136
|
+
const layoutShift = { first: 0, last: 0, session: 0 };
|
|
137
|
+
const interactions = new Map();
|
|
138
|
+
const observers = [];
|
|
139
|
+
const observe = (type, onEntry, options) => {
|
|
140
|
+
try {
|
|
141
|
+
const observer = new Observer((list) => { list.getEntries().forEach(onEntry); });
|
|
142
|
+
observer.observe({ type, buffered: true, ...options });
|
|
143
|
+
observers.push([observer, onEntry]);
|
|
144
|
+
return true;
|
|
145
|
+
} catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
if (navigation?.responseStart > 0) {
|
|
150
|
+
values.ttfb = sinceActivation(navigation.responseStart);
|
|
151
|
+
}
|
|
152
|
+
// A page that loads in the background has no meaningful paint timings.
|
|
153
|
+
if (document.visibilityState !== "hidden") {
|
|
154
|
+
observe("paint", (entry) => {
|
|
155
|
+
if (entry.name === "first-contentful-paint") {
|
|
156
|
+
values.fcp = sinceActivation(entry.startTime);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
observe("largest-contentful-paint", (entry) => { values.lcp = sinceActivation(entry.startTime); });
|
|
160
|
+
}
|
|
161
|
+
// CLS is the largest session window: shifts less than 1 s apart, at most 5 s long.
|
|
162
|
+
if (observe("layout-shift", (entry) => {
|
|
163
|
+
if (entry.hadRecentInput) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (layoutShift.session > 0 && (entry.startTime - layoutShift.last >= 1000 || entry.startTime - layoutShift.first >= 5000)) {
|
|
167
|
+
layoutShift.session = 0;
|
|
168
|
+
}
|
|
169
|
+
if (layoutShift.session === 0) {
|
|
170
|
+
layoutShift.first = entry.startTime;
|
|
171
|
+
}
|
|
172
|
+
layoutShift.session += entry.value;
|
|
173
|
+
layoutShift.last = entry.startTime;
|
|
174
|
+
values.cls = Math.max(values.cls ?? 0, layoutShift.session);
|
|
175
|
+
})) {
|
|
176
|
+
values.cls = 0;
|
|
177
|
+
}
|
|
178
|
+
observe("event", (entry) => {
|
|
179
|
+
if (entry.interactionId > 0) {
|
|
180
|
+
interactions.set(entry.interactionId, Math.max(interactions.get(entry.interactionId) ?? 0, entry.duration));
|
|
181
|
+
}
|
|
182
|
+
}, { durationThreshold: 40 });
|
|
183
|
+
|
|
184
|
+
let reported = false;
|
|
185
|
+
const report = (event) => {
|
|
186
|
+
if (reported || (document.visibilityState !== "hidden" && event?.type !== "pagehide")) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
reported = true;
|
|
190
|
+
uninstall();
|
|
191
|
+
// INP skips 1 slowest interaction per 50, as the web-vitals library does.
|
|
192
|
+
const durations = [...interactions.values()].sort((left, right) => right - left);
|
|
193
|
+
if (durations.length > 0) {
|
|
194
|
+
const interactionCount = performance?.interactionCount ?? durations.length;
|
|
195
|
+
values.inp = durations[Math.min(durations.length - 1, Math.floor(interactionCount / 50))];
|
|
196
|
+
}
|
|
197
|
+
const routeTemplate = landingRouteTemplates.get(client);
|
|
198
|
+
for (const [vital, rawValue] of Object.entries(values)) {
|
|
199
|
+
const { description, good, poor, unit } = WEB_VITALS[vital];
|
|
200
|
+
const value = vital === "cls" ? Number(rawValue.toFixed(4)) : Math.round(rawValue);
|
|
201
|
+
try {
|
|
202
|
+
client.metric(`evt_next_web_vital_${randomHex(8)}`, new Date().toISOString(), {
|
|
203
|
+
description,
|
|
204
|
+
kind: "histogram",
|
|
205
|
+
metadata: compactMetadata({
|
|
206
|
+
framework: "nextjs",
|
|
207
|
+
navigationType: navigation?.type,
|
|
208
|
+
rating: value <= good ? "good" : value <= poor ? "needs-improvement" : "poor",
|
|
209
|
+
routeTemplate
|
|
210
|
+
}),
|
|
211
|
+
name: `browser.web_vital.${vital}`,
|
|
212
|
+
temporality: "delta",
|
|
213
|
+
unit,
|
|
214
|
+
value
|
|
215
|
+
});
|
|
216
|
+
} catch {
|
|
217
|
+
// Capture failures never reach the page.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
void client.flush?.().catch(() => undefined);
|
|
221
|
+
};
|
|
222
|
+
document.addEventListener("visibilitychange", report);
|
|
223
|
+
browserWindow.addEventListener("pagehide", report);
|
|
224
|
+
function uninstall() {
|
|
225
|
+
for (const [observer, onEntry] of observers) {
|
|
226
|
+
observer.takeRecords?.().forEach(onEntry);
|
|
227
|
+
observer.disconnect();
|
|
228
|
+
}
|
|
229
|
+
observers.length = 0;
|
|
230
|
+
document.removeEventListener?.("visibilitychange", report);
|
|
231
|
+
browserWindow.removeEventListener?.("pagehide", report);
|
|
232
|
+
}
|
|
233
|
+
return uninstall;
|
|
234
|
+
}
|
|
235
|
+
|
|
113
236
|
function createNextBrowserTransport(endpoint) {
|
|
114
237
|
return {
|
|
115
238
|
async send(apiKey, body) {
|
|
@@ -222,6 +345,9 @@ function createNextNavigationSpanEvent({
|
|
|
222
345
|
status,
|
|
223
346
|
traceId: trace.traceId,
|
|
224
347
|
metadata: compactMetadata({
|
|
348
|
+
analyticsKind: "page_view",
|
|
349
|
+
analyticsSchemaVersion: 1,
|
|
350
|
+
analyticsSurface: safeRouteTemplate,
|
|
225
351
|
framework: "nextjs",
|
|
226
352
|
navigationIndex,
|
|
227
353
|
navigationType,
|
|
@@ -243,6 +369,9 @@ function captureNextNavigation(client, input = {}) {
|
|
|
243
369
|
return undefined;
|
|
244
370
|
}
|
|
245
371
|
client.span(event.id, event.timestamp, event.attributes);
|
|
372
|
+
if (!landingRouteTemplates.has(client)) {
|
|
373
|
+
landingRouteTemplates.set(client, event.attributes.metadata.routeTemplate);
|
|
374
|
+
}
|
|
246
375
|
return event;
|
|
247
376
|
}
|
|
248
377
|
|
package/client.d.cts
CHANGED
|
@@ -43,12 +43,14 @@ export declare function captureNextClientError(
|
|
|
43
43
|
options?: NextClientErrorOptions
|
|
44
44
|
): Promise<TransportResponse | undefined>;
|
|
45
45
|
|
|
46
|
-
/** Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide. */
|
|
46
|
+
/** Report uncaught browser errors and unhandled rejections, send Web Vitals, and flush queued events on pagehide. */
|
|
47
47
|
export declare function installLogBrewNextClientErrorCapture(options: {
|
|
48
48
|
client: LogBrewClient;
|
|
49
49
|
browserWindow?: Window;
|
|
50
50
|
/** Console, fetch, XHR, and route-change breadcrumbs on each issue. Defaults to true. */
|
|
51
51
|
breadcrumbs?: boolean;
|
|
52
|
+
/** LCP, INP, CLS, FCP, and TTFB as `browser.web_vital.*` metrics when the page is first hidden. Defaults to true. */
|
|
53
|
+
webVitals?: boolean;
|
|
52
54
|
}): { uninstall(): void };
|
|
53
55
|
|
|
54
56
|
export type NextRoutePattern = string;
|
package/client.d.ts
CHANGED
|
@@ -43,12 +43,14 @@ export declare function captureNextClientError(
|
|
|
43
43
|
options?: NextClientErrorOptions
|
|
44
44
|
): Promise<TransportResponse | undefined>;
|
|
45
45
|
|
|
46
|
-
/** Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide. */
|
|
46
|
+
/** Report uncaught browser errors and unhandled rejections, send Web Vitals, and flush queued events on pagehide. */
|
|
47
47
|
export declare function installLogBrewNextClientErrorCapture(options: {
|
|
48
48
|
client: LogBrewClient;
|
|
49
49
|
browserWindow?: Window;
|
|
50
50
|
/** Console, fetch, XHR, and route-change breadcrumbs on each issue. Defaults to true. */
|
|
51
51
|
breadcrumbs?: boolean;
|
|
52
|
+
/** LCP, INP, CLS, FCP, and TTFB as `browser.web_vital.*` metrics when the page is first hidden. Defaults to true. */
|
|
53
|
+
webVitals?: boolean;
|
|
52
54
|
}): { uninstall(): void };
|
|
53
55
|
|
|
54
56
|
export type NextRoutePattern = string;
|
package/client.js
CHANGED
|
@@ -13,6 +13,16 @@ const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
|
|
|
13
13
|
// Send 1 second after the first queued event; pagehide flushes the rest when the page closes.
|
|
14
14
|
const DEFAULT_BROWSER_DELIVERY_INTERVAL_MS = 1000;
|
|
15
15
|
const clientEndpoints = new WeakMap();
|
|
16
|
+
// The first route template each client's navigation hook saw: the route the page loaded on.
|
|
17
|
+
const landingRouteTemplates = new WeakMap();
|
|
18
|
+
// Good and poor limits from https://web.dev/articles/vitals.
|
|
19
|
+
const WEB_VITALS = {
|
|
20
|
+
cls: { description: "Cumulative Layout Shift of one page load.", good: 0.1, poor: 0.25, unit: "1" },
|
|
21
|
+
fcp: { description: "First Contentful Paint of one page load.", good: 1800, poor: 3000, unit: "ms" },
|
|
22
|
+
inp: { description: "Interaction to Next Paint of one page load.", good: 200, poor: 500, unit: "ms" },
|
|
23
|
+
lcp: { description: "Largest Contentful Paint of one page load.", good: 2500, poor: 4000, unit: "ms" },
|
|
24
|
+
ttfb: { description: "Time to First Byte of one page load.", good: 800, poor: 1800, unit: "ms" }
|
|
25
|
+
};
|
|
16
26
|
|
|
17
27
|
export function createLogBrewNextBrowserClient({
|
|
18
28
|
apiKey,
|
|
@@ -22,7 +32,7 @@ export function createLogBrewNextBrowserClient({
|
|
|
22
32
|
endpoint = DEFAULT_ENDPOINT,
|
|
23
33
|
eventFilter,
|
|
24
34
|
sdkName = "logbrew-next-client",
|
|
25
|
-
sdkVersion = "0.1.
|
|
35
|
+
sdkVersion = "0.1.18",
|
|
26
36
|
maxQueueSize,
|
|
27
37
|
maxRetries = 2,
|
|
28
38
|
onEventDropped,
|
|
@@ -72,7 +82,8 @@ export function captureNextClientError(client, error, { digest, routeTemplate, f
|
|
|
72
82
|
|
|
73
83
|
// Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide.
|
|
74
84
|
// Console, fetch, XHR, and route-change breadcrumbs ride along with each issue; breadcrumbs: false turns them off.
|
|
75
|
-
|
|
85
|
+
// Web Vitals go out as metrics when the page is first hidden; webVitals: false turns them off.
|
|
86
|
+
export function installLogBrewNextClientErrorCapture({ breadcrumbs = true, browserWindow = globalThis.window, client, webVitals = true } = {}) {
|
|
76
87
|
if (!client || !browserWindow || typeof browserWindow.addEventListener !== "function") {
|
|
77
88
|
throw new SdkError("configuration_error", "installLogBrewNextClientErrorCapture requires a client and a browser window");
|
|
78
89
|
}
|
|
@@ -100,9 +111,11 @@ export function installLogBrewNextClientErrorCapture({ breadcrumbs = true, brows
|
|
|
100
111
|
const uninstallBreadcrumbs = breadcrumbs === false
|
|
101
112
|
? () => undefined
|
|
102
113
|
: installBrowserBreadcrumbs(browserWindow, client, { ignoredUrls: [clientEndpoints.get(client) ?? DEFAULT_ENDPOINT] });
|
|
114
|
+
const uninstallWebVitals = webVitals === false ? () => undefined : installWebVitals(browserWindow, client);
|
|
103
115
|
return {
|
|
104
116
|
uninstall() {
|
|
105
117
|
uninstallBreadcrumbs();
|
|
118
|
+
uninstallWebVitals();
|
|
106
119
|
for (const [type, listener] of Object.entries(listeners)) {
|
|
107
120
|
browserWindow.removeEventListener?.(type, listener);
|
|
108
121
|
}
|
|
@@ -110,6 +123,116 @@ export function installLogBrewNextClientErrorCapture({ breadcrumbs = true, brows
|
|
|
110
123
|
};
|
|
111
124
|
}
|
|
112
125
|
|
|
126
|
+
// Observe the page's performance entries the way https://github.com/GoogleChrome/web-vitals does,
|
|
127
|
+
// without the library, and record each Web Vital once, when the page is first hidden.
|
|
128
|
+
function installWebVitals(browserWindow, client) {
|
|
129
|
+
const { PerformanceObserver: Observer, document, performance } = browserWindow;
|
|
130
|
+
if (typeof Observer !== "function" || !document || typeof document.addEventListener !== "function") {
|
|
131
|
+
return () => undefined;
|
|
132
|
+
}
|
|
133
|
+
const navigation = performance?.getEntriesByType?.("navigation")?.[0];
|
|
134
|
+
const sinceActivation = (time) => Math.max(time - (navigation?.activationStart ?? 0), 0);
|
|
135
|
+
const values = {};
|
|
136
|
+
const layoutShift = { first: 0, last: 0, session: 0 };
|
|
137
|
+
const interactions = new Map();
|
|
138
|
+
const observers = [];
|
|
139
|
+
const observe = (type, onEntry, options) => {
|
|
140
|
+
try {
|
|
141
|
+
const observer = new Observer((list) => { list.getEntries().forEach(onEntry); });
|
|
142
|
+
observer.observe({ type, buffered: true, ...options });
|
|
143
|
+
observers.push([observer, onEntry]);
|
|
144
|
+
return true;
|
|
145
|
+
} catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
if (navigation?.responseStart > 0) {
|
|
150
|
+
values.ttfb = sinceActivation(navigation.responseStart);
|
|
151
|
+
}
|
|
152
|
+
// A page that loads in the background has no meaningful paint timings.
|
|
153
|
+
if (document.visibilityState !== "hidden") {
|
|
154
|
+
observe("paint", (entry) => {
|
|
155
|
+
if (entry.name === "first-contentful-paint") {
|
|
156
|
+
values.fcp = sinceActivation(entry.startTime);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
observe("largest-contentful-paint", (entry) => { values.lcp = sinceActivation(entry.startTime); });
|
|
160
|
+
}
|
|
161
|
+
// CLS is the largest session window: shifts less than 1 s apart, at most 5 s long.
|
|
162
|
+
if (observe("layout-shift", (entry) => {
|
|
163
|
+
if (entry.hadRecentInput) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (layoutShift.session > 0 && (entry.startTime - layoutShift.last >= 1000 || entry.startTime - layoutShift.first >= 5000)) {
|
|
167
|
+
layoutShift.session = 0;
|
|
168
|
+
}
|
|
169
|
+
if (layoutShift.session === 0) {
|
|
170
|
+
layoutShift.first = entry.startTime;
|
|
171
|
+
}
|
|
172
|
+
layoutShift.session += entry.value;
|
|
173
|
+
layoutShift.last = entry.startTime;
|
|
174
|
+
values.cls = Math.max(values.cls ?? 0, layoutShift.session);
|
|
175
|
+
})) {
|
|
176
|
+
values.cls = 0;
|
|
177
|
+
}
|
|
178
|
+
observe("event", (entry) => {
|
|
179
|
+
if (entry.interactionId > 0) {
|
|
180
|
+
interactions.set(entry.interactionId, Math.max(interactions.get(entry.interactionId) ?? 0, entry.duration));
|
|
181
|
+
}
|
|
182
|
+
}, { durationThreshold: 40 });
|
|
183
|
+
|
|
184
|
+
let reported = false;
|
|
185
|
+
const report = (event) => {
|
|
186
|
+
if (reported || (document.visibilityState !== "hidden" && event?.type !== "pagehide")) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
reported = true;
|
|
190
|
+
uninstall();
|
|
191
|
+
// INP skips 1 slowest interaction per 50, as the web-vitals library does.
|
|
192
|
+
const durations = [...interactions.values()].sort((left, right) => right - left);
|
|
193
|
+
if (durations.length > 0) {
|
|
194
|
+
const interactionCount = performance?.interactionCount ?? durations.length;
|
|
195
|
+
values.inp = durations[Math.min(durations.length - 1, Math.floor(interactionCount / 50))];
|
|
196
|
+
}
|
|
197
|
+
const routeTemplate = landingRouteTemplates.get(client);
|
|
198
|
+
for (const [vital, rawValue] of Object.entries(values)) {
|
|
199
|
+
const { description, good, poor, unit } = WEB_VITALS[vital];
|
|
200
|
+
const value = vital === "cls" ? Number(rawValue.toFixed(4)) : Math.round(rawValue);
|
|
201
|
+
try {
|
|
202
|
+
client.metric(`evt_next_web_vital_${randomHex(8)}`, new Date().toISOString(), {
|
|
203
|
+
description,
|
|
204
|
+
kind: "histogram",
|
|
205
|
+
metadata: compactMetadata({
|
|
206
|
+
framework: "nextjs",
|
|
207
|
+
navigationType: navigation?.type,
|
|
208
|
+
rating: value <= good ? "good" : value <= poor ? "needs-improvement" : "poor",
|
|
209
|
+
routeTemplate
|
|
210
|
+
}),
|
|
211
|
+
name: `browser.web_vital.${vital}`,
|
|
212
|
+
temporality: "delta",
|
|
213
|
+
unit,
|
|
214
|
+
value
|
|
215
|
+
});
|
|
216
|
+
} catch {
|
|
217
|
+
// Capture failures never reach the page.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
void client.flush?.().catch(() => undefined);
|
|
221
|
+
};
|
|
222
|
+
document.addEventListener("visibilitychange", report);
|
|
223
|
+
browserWindow.addEventListener("pagehide", report);
|
|
224
|
+
function uninstall() {
|
|
225
|
+
for (const [observer, onEntry] of observers) {
|
|
226
|
+
observer.takeRecords?.().forEach(onEntry);
|
|
227
|
+
observer.disconnect();
|
|
228
|
+
}
|
|
229
|
+
observers.length = 0;
|
|
230
|
+
document.removeEventListener?.("visibilitychange", report);
|
|
231
|
+
browserWindow.removeEventListener?.("pagehide", report);
|
|
232
|
+
}
|
|
233
|
+
return uninstall;
|
|
234
|
+
}
|
|
235
|
+
|
|
113
236
|
function createNextBrowserTransport(endpoint) {
|
|
114
237
|
return {
|
|
115
238
|
async send(apiKey, body) {
|
|
@@ -222,6 +345,9 @@ export function createNextNavigationSpanEvent({
|
|
|
222
345
|
status,
|
|
223
346
|
traceId: trace.traceId,
|
|
224
347
|
metadata: compactMetadata({
|
|
348
|
+
analyticsKind: "page_view",
|
|
349
|
+
analyticsSchemaVersion: 1,
|
|
350
|
+
analyticsSurface: safeRouteTemplate,
|
|
225
351
|
framework: "nextjs",
|
|
226
352
|
navigationIndex,
|
|
227
353
|
navigationType,
|
|
@@ -243,6 +369,9 @@ export function captureNextNavigation(client, input = {}) {
|
|
|
243
369
|
return undefined;
|
|
244
370
|
}
|
|
245
371
|
client.span(event.id, event.timestamp, event.attributes);
|
|
372
|
+
if (!landingRouteTemplates.has(client)) {
|
|
373
|
+
landingRouteTemplates.set(client, event.attributes.metadata.routeTemplate);
|
|
374
|
+
}
|
|
246
375
|
return event;
|
|
247
376
|
}
|
|
248
377
|
|
package/edge.cjs
CHANGED
|
@@ -25,7 +25,7 @@ function createLogBrewNextEdgeRequestErrorHandler(options = {}) {
|
|
|
25
25
|
apiKey: authKey,
|
|
26
26
|
context: deploymentContext,
|
|
27
27
|
sdkName: options.sdkName ?? "logbrew-next",
|
|
28
|
-
sdkVersion: options.sdkVersion ?? "0.1.
|
|
28
|
+
sdkVersion: options.sdkVersion ?? "0.1.18",
|
|
29
29
|
maxRetries: options.maxRetries ?? 2
|
|
30
30
|
});
|
|
31
31
|
const event = typeof options.errorEvent === "function"
|
|
@@ -24,7 +24,7 @@ const event = captureNextNavigation(client, {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
const payload = JSON.parse(client.previewJson());
|
|
27
|
-
if (!event || event.attributes.metadata.routeTemplate !== "/projects/[projectId]/settings" || event.attributes.metadata.operation !== "browser.navigation" || payload.sdk.version !== "0.1.
|
|
27
|
+
if (!event || event.attributes.metadata.routeTemplate !== "/projects/[projectId]/settings" || event.attributes.metadata.operation !== "browser.navigation" || payload.sdk.version !== "0.1.18") {
|
|
28
28
|
throw new Error(`unexpected Next client route span: ${JSON.stringify(event)}`);
|
|
29
29
|
}
|
|
30
30
|
|
package/index.cjs
CHANGED
|
@@ -23,7 +23,7 @@ function createLogBrewNextClient({
|
|
|
23
23
|
serverApiKey,
|
|
24
24
|
context,
|
|
25
25
|
sdkName = "logbrew-next",
|
|
26
|
-
sdkVersion = "0.1.
|
|
26
|
+
sdkVersion = "0.1.18",
|
|
27
27
|
maxRetries = 2
|
|
28
28
|
} = {}) {
|
|
29
29
|
const authKey = serverApiKey ?? apiKey ?? readEnvServerApiKey() ?? readEnvApiKey();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logbrew/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "Next.js App Router helpers for the public LogBrew JavaScript SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
"node": ">=18"
|
|
89
89
|
},
|
|
90
90
|
"license": "MIT",
|
|
91
|
+
"homepage": "https://logbrew.co",
|
|
91
92
|
"repository": {
|
|
92
93
|
"type": "git",
|
|
93
94
|
"url": "git+https://github.com/LogBrewCo/sdk.git"
|