@adobe/helix-rum-enhancer 2.31.0-beta.2 → 2.31.0-beta.3

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.
@@ -0,0 +1,411 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ (function () {
13
+ 'use strict';
14
+
15
+ (function (e) {
16
+
17
+ var n,
18
+ t,
19
+ r,
20
+ i,
21
+ o,
22
+ a = -1,
23
+ c = function (e) {
24
+ addEventListener("pageshow", function (n) {
25
+ n.persisted && (a = n.timeStamp, e(n));
26
+ }, true);
27
+ },
28
+ u = function () {
29
+ var e = self.performance && performance.getEntriesByType && performance.getEntriesByType("navigation")[0];
30
+ if (e && e.responseStart > 0 && e.responseStart < performance.now()) return e;
31
+ },
32
+ s = function () {
33
+ var e = u();
34
+ return e && e.activationStart || 0;
35
+ },
36
+ f = function (e, n) {
37
+ var t = u(),
38
+ r = "navigate";
39
+ a >= 0 ? r = "back-forward-cache" : t && (document.prerendering || s() > 0 ? r = "prerender" : document.wasDiscarded ? r = "restore" : t.type && (r = t.type.replace(/_/g, "-")));
40
+ return {
41
+ name: e,
42
+ value: undefined === n ? -1 : n,
43
+ rating: "good",
44
+ delta: 0,
45
+ entries: [],
46
+ id: "v4-".concat(Date.now(), "-").concat(Math.floor(8999999999999 * Math.random()) + 1e12),
47
+ navigationType: r
48
+ };
49
+ },
50
+ d = function (e, n, t) {
51
+ try {
52
+ if (PerformanceObserver.supportedEntryTypes.includes(e)) {
53
+ var r = new PerformanceObserver(function (e) {
54
+ Promise.resolve().then(function () {
55
+ n(e.getEntries());
56
+ });
57
+ });
58
+ return r.observe(Object.assign({
59
+ type: e,
60
+ buffered: !0
61
+ }, t || {})), r;
62
+ }
63
+ } catch (e) {}
64
+ },
65
+ l = function (e, n, t, r) {
66
+ var i, o;
67
+ return function (a) {
68
+ n.value >= 0 && (a || r) && ((o = n.value - (i || 0)) || undefined === i) && (i = n.value, n.delta = o, n.rating = function (e, n) {
69
+ return e > n[1] ? "poor" : e > n[0] ? "needs-improvement" : "good";
70
+ }(n.value, t), e(n));
71
+ };
72
+ },
73
+ p = function (e) {
74
+ requestAnimationFrame(function () {
75
+ return requestAnimationFrame(function () {
76
+ return e();
77
+ });
78
+ });
79
+ },
80
+ v = function (e) {
81
+ document.addEventListener("visibilitychange", function () {
82
+ "hidden" === document.visibilityState && e();
83
+ });
84
+ },
85
+ m = function (e) {
86
+ var n = false;
87
+ return function () {
88
+ n || (e(), n = true);
89
+ };
90
+ },
91
+ h = -1,
92
+ g = function () {
93
+ return "hidden" !== document.visibilityState || document.prerendering ? 1 / 0 : 0;
94
+ },
95
+ T = function (e) {
96
+ "hidden" === document.visibilityState && h > -1 && (h = "visibilitychange" === e.type ? e.timeStamp : 0, E());
97
+ },
98
+ y = function () {
99
+ addEventListener("visibilitychange", T, true), addEventListener("prerenderingchange", T, true);
100
+ },
101
+ E = function () {
102
+ removeEventListener("visibilitychange", T, true), removeEventListener("prerenderingchange", T, true);
103
+ },
104
+ C = function () {
105
+ return h < 0 && (h = g(), y(), c(function () {
106
+ setTimeout(function () {
107
+ h = g(), y();
108
+ }, 0);
109
+ })), {
110
+ get firstHiddenTime() {
111
+ return h;
112
+ }
113
+ };
114
+ },
115
+ L = function (e) {
116
+ document.prerendering ? addEventListener("prerenderingchange", function () {
117
+ return e();
118
+ }, true) : e();
119
+ },
120
+ S = [1800, 3e3],
121
+ b = function (e, n) {
122
+ n = n || {}, L(function () {
123
+ var t,
124
+ r = C(),
125
+ i = f("FCP"),
126
+ o = d("paint", function (e) {
127
+ e.forEach(function (e) {
128
+ "first-contentful-paint" === e.name && (o.disconnect(), e.startTime < r.firstHiddenTime && (i.value = Math.max(e.startTime - s(), 0), i.entries.push(e), t(!0)));
129
+ });
130
+ });
131
+ o && (t = l(e, i, S, n.reportAllChanges), c(function (r) {
132
+ i = f("FCP"), t = l(e, i, S, n.reportAllChanges), p(function () {
133
+ i.value = performance.now() - r.timeStamp, t(true);
134
+ });
135
+ }));
136
+ });
137
+ },
138
+ w = [.1, .25],
139
+ I = 0,
140
+ P = 1 / 0,
141
+ A = 0,
142
+ F = function (e) {
143
+ e.forEach(function (e) {
144
+ e.interactionId && (P = Math.min(P, e.interactionId), A = Math.max(A, e.interactionId), I = A ? (A - P) / 7 + 1 : 0);
145
+ });
146
+ },
147
+ M = function () {
148
+ return n ? I : performance.interactionCount || 0;
149
+ },
150
+ k = function () {
151
+ "interactionCount" in performance || n || (n = d("event", F, {
152
+ type: "event",
153
+ buffered: true,
154
+ durationThreshold: 0
155
+ }));
156
+ },
157
+ D = [],
158
+ B = new Map(),
159
+ R = 0,
160
+ x = function () {
161
+ var e = Math.min(D.length - 1, Math.floor((M() - R) / 50));
162
+ return D[e];
163
+ },
164
+ H = [],
165
+ N = function (e) {
166
+ if (H.forEach(function (n) {
167
+ return n(e);
168
+ }), e.interactionId || "first-input" === e.entryType) {
169
+ var n = D[D.length - 1],
170
+ t = B.get(e.interactionId);
171
+ if (t || D.length < 10 || e.duration > n.latency) {
172
+ if (t) e.duration > t.latency ? (t.entries = [e], t.latency = e.duration) : e.duration === t.latency && e.startTime === t.entries[0].startTime && t.entries.push(e);else {
173
+ var r = {
174
+ id: e.interactionId,
175
+ latency: e.duration,
176
+ entries: [e]
177
+ };
178
+ B.set(r.id, r), D.push(r);
179
+ }
180
+ D.sort(function (e, n) {
181
+ return n.latency - e.latency;
182
+ }), D.length > 10 && D.splice(10).forEach(function (e) {
183
+ return B.delete(e.id);
184
+ });
185
+ }
186
+ }
187
+ },
188
+ q = function (e) {
189
+ var n = self.requestIdleCallback || self.setTimeout,
190
+ t = -1;
191
+ return e = m(e), "hidden" === document.visibilityState ? e() : (t = n(e), v(e)), t;
192
+ },
193
+ O = [200, 500],
194
+ j = [2500, 4e3],
195
+ V = {},
196
+ _ = [800, 1800],
197
+ z = function e(n) {
198
+ document.prerendering ? L(function () {
199
+ return e(n);
200
+ }) : "complete" !== document.readyState ? addEventListener("load", function () {
201
+ return e(n);
202
+ }, true) : setTimeout(n, 0);
203
+ },
204
+ G = {
205
+ passive: true,
206
+ capture: true
207
+ },
208
+ J = new Date(),
209
+ K = function (e, n) {
210
+ t || (t = n, r = e, i = new Date(), W(removeEventListener), Q());
211
+ },
212
+ Q = function () {
213
+ if (r >= 0 && r < i - J) {
214
+ var e = {
215
+ entryType: "first-input",
216
+ name: t.type,
217
+ target: t.target,
218
+ cancelable: t.cancelable,
219
+ startTime: t.timeStamp,
220
+ processingStart: t.timeStamp + r
221
+ };
222
+ o.forEach(function (n) {
223
+ n(e);
224
+ }), o = [];
225
+ }
226
+ },
227
+ U = function (e) {
228
+ if (e.cancelable) {
229
+ var n = (e.timeStamp > 1e12 ? new Date() : performance.now()) - e.timeStamp;
230
+ "pointerdown" == e.type ? function (e, n) {
231
+ var t = function () {
232
+ K(e, n), i();
233
+ },
234
+ r = function () {
235
+ i();
236
+ },
237
+ i = function () {
238
+ removeEventListener("pointerup", t, G), removeEventListener("pointercancel", r, G);
239
+ };
240
+ addEventListener("pointerup", t, G), addEventListener("pointercancel", r, G);
241
+ }(n, e) : K(n, e);
242
+ }
243
+ },
244
+ W = function (e) {
245
+ ["mousedown", "keydown", "touchstart", "pointerdown"].forEach(function (n) {
246
+ return e(n, U, G);
247
+ });
248
+ },
249
+ X = [100, 300];
250
+ return e.CLSThresholds = w, e.FCPThresholds = S, e.FIDThresholds = X, e.INPThresholds = O, e.LCPThresholds = j, e.TTFBThresholds = _, e.onCLS = function (e, n) {
251
+ n = n || {}, b(m(function () {
252
+ var t,
253
+ r = f("CLS", 0),
254
+ i = 0,
255
+ o = [],
256
+ a = function (e) {
257
+ e.forEach(function (e) {
258
+ if (!e.hadRecentInput) {
259
+ var n = o[0],
260
+ t = o[o.length - 1];
261
+ i && e.startTime - t.startTime < 1e3 && e.startTime - n.startTime < 5e3 ? (i += e.value, o.push(e)) : (i = e.value, o = [e]);
262
+ }
263
+ }), i > r.value && (r.value = i, r.entries = o, t());
264
+ },
265
+ u = d("layout-shift", a);
266
+ u && (t = l(e, r, w, n.reportAllChanges), v(function () {
267
+ a(u.takeRecords()), t(true);
268
+ }), c(function () {
269
+ i = 0, r = f("CLS", 0), t = l(e, r, w, n.reportAllChanges), p(function () {
270
+ return t();
271
+ });
272
+ }), setTimeout(t, 0));
273
+ }));
274
+ }, e.onFCP = b, e.onFID = function (e, n) {
275
+ n = n || {}, L(function () {
276
+ var i,
277
+ a = C(),
278
+ u = f("FID"),
279
+ s = function (e) {
280
+ e.startTime < a.firstHiddenTime && (u.value = e.processingStart - e.startTime, u.entries.push(e), i(true));
281
+ },
282
+ p = function (e) {
283
+ e.forEach(s);
284
+ },
285
+ h = d("first-input", p);
286
+ i = l(e, u, X, n.reportAllChanges), h && (v(m(function () {
287
+ p(h.takeRecords()), h.disconnect();
288
+ })), c(function () {
289
+ var a;
290
+ u = f("FID"), i = l(e, u, X, n.reportAllChanges), o = [], r = -1, t = null, W(addEventListener), a = s, o.push(a), Q();
291
+ }));
292
+ });
293
+ }, e.onINP = function (e, n) {
294
+ "PerformanceEventTiming" in self && "interactionId" in PerformanceEventTiming.prototype && (n = n || {}, L(function () {
295
+ var t;
296
+ k();
297
+ var r,
298
+ i = f("INP"),
299
+ o = function (e) {
300
+ q(function () {
301
+ e.forEach(N);
302
+ var n = x();
303
+ n && n.latency !== i.value && (i.value = n.latency, i.entries = n.entries, r());
304
+ });
305
+ },
306
+ a = d("event", o, {
307
+ durationThreshold: null !== (t = n.durationThreshold) && undefined !== t ? t : 40
308
+ });
309
+ r = l(e, i, O, n.reportAllChanges), a && (a.observe({
310
+ type: "first-input",
311
+ buffered: true
312
+ }), v(function () {
313
+ o(a.takeRecords()), r(true);
314
+ }), c(function () {
315
+ R = M(), D.length = 0, B.clear(), i = f("INP"), r = l(e, i, O, n.reportAllChanges);
316
+ }));
317
+ }));
318
+ }, e.onLCP = function (e, n) {
319
+ n = n || {}, L(function () {
320
+ var t,
321
+ r = C(),
322
+ i = f("LCP"),
323
+ o = function (e) {
324
+ n.reportAllChanges || (e = e.slice(-1)), e.forEach(function (e) {
325
+ e.startTime < r.firstHiddenTime && (i.value = Math.max(e.startTime - s(), 0), i.entries = [e], t());
326
+ });
327
+ },
328
+ a = d("largest-contentful-paint", o);
329
+ if (a) {
330
+ t = l(e, i, j, n.reportAllChanges);
331
+ var u = m(function () {
332
+ V[i.id] || (o(a.takeRecords()), a.disconnect(), V[i.id] = true, t(true));
333
+ });
334
+ ["keydown", "click"].forEach(function (e) {
335
+ addEventListener(e, function () {
336
+ return q(u);
337
+ }, {
338
+ once: true,
339
+ capture: true
340
+ });
341
+ }), v(u), c(function (r) {
342
+ i = f("LCP"), t = l(e, i, j, n.reportAllChanges), p(function () {
343
+ i.value = performance.now() - r.timeStamp, V[i.id] = true, t(true);
344
+ });
345
+ });
346
+ }
347
+ });
348
+ }, e.onTTFB = function (e, n) {
349
+ n = n || {};
350
+ var t = f("TTFB"),
351
+ r = l(e, t, _, n.reportAllChanges);
352
+ z(function () {
353
+ var i = u();
354
+ i && (t.value = Math.max(i.responseStart - s(), 0), t.entries = [i], r(true), c(function () {
355
+ t = f("TTFB", 0), (r = l(e, t, _, n.reportAllChanges))(true);
356
+ }));
357
+ });
358
+ }, e;
359
+ })({});
360
+
361
+ function addCWVTracking({
362
+ sampleRUM,
363
+ sourceSelector,
364
+ targetSelector,
365
+ fflags
366
+ }) {
367
+ setTimeout(() => {
368
+ try {
369
+ const cwvScript = new URL('.rum/web-vitals/dist/web-vitals.iife.js', sampleRUM.baseURL).href;
370
+ if (document.querySelector(`script[src="${cwvScript}"]`)) {
371
+ return;
372
+ }
373
+ const script = document.createElement('script');
374
+ script.src = cwvScript;
375
+ script.onload = () => {
376
+ const storeCWV = measurement => {
377
+ const data = {
378
+ cwv: {}
379
+ };
380
+ data.cwv[measurement.name] = measurement.value;
381
+ if (measurement.name === 'LCP' && measurement.entries.length > 0) {
382
+ const {
383
+ element
384
+ } = measurement.entries.pop();
385
+ data.target = targetSelector(element);
386
+ data.source = sourceSelector(element) || element && element.outerHTML.slice(0, 30);
387
+ }
388
+ sampleRUM('cwv', data);
389
+ };
390
+ const isEager = metric => ['CLS', 'LCP'].includes(metric);
391
+ ['INP', 'TTFB', 'CLS', 'LCP'].forEach(metric => {
392
+ const metricFn = window.webVitals[`on${metric}`];
393
+ if (typeof metricFn === 'function') {
394
+ let opts = {};
395
+ fflags.enabled('eagercwv', () => {
396
+ opts = {
397
+ reportAllChanges: isEager(metric)
398
+ };
399
+ });
400
+ metricFn(storeCWV, opts);
401
+ }
402
+ });
403
+ };
404
+ document.head.appendChild(script);
405
+ } catch (error) {}
406
+ }, 2000);
407
+ }
408
+
409
+ return addCWVTracking;
410
+
411
+ })();