Skip to content

Commit bf40121

Browse files
committed
feat(tideways): Support Tideways Layer Metrics when available
fix(beacon): fix is cached JS detection
1 parent 9ec4704 commit bf40121

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

lib/time-to-first-byte.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export default function(payload: Payload) {
3939

4040
// If waiting time is less than max server metric, it is probably a cache hit
4141
if (ttfb.isCached === false
42-
&& ttfb.waiting > 0.0
43-
&& ttfb.waiting < maxTiming) {
42+
&& ttfb.request > 0.0
43+
&& ttfb.request < maxTiming) {
4444
ttfb.isCached = true;
4545
}
4646
}

src/Plugin/RenderTimingHeader.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public function __construct(
2323

2424
public function afterRenderResult(ResultInterface $subject, $output, ResponseInterface $response)
2525
{
26+
if (\class_exists('Tideways\Profiler', false)
27+
&& \method_exists('Tideways\Profiler', 'getLayerMetrics')) {
28+
foreach (\Tideways\Profiler::getLayerMetrics() as $metric) {
29+
$name = match ($metric->name) {
30+
'rdbms' => 'db',
31+
default => $metric->name,
32+
};
33+
34+
$this->measurementAggregator->setAggregate($name, $metric->wallTimeMicroseconds / 1e3);
35+
}
36+
}
37+
2638
$timingHeader = $this->measurementAggregator->renderTimingHeader();
2739
if ($timingHeader) {
2840
$response->setHeader('Server-Timing', $timingHeader, true);

src/Service/MeasurementAggregator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function recordAggregate(string $type, float $time): void
2929
$this->counters[$type] = ($this->counters[$type] ?? 0) + 1;
3030
}
3131

32+
public function setAggregate(string $type, float $time): void
33+
{
34+
$this->timing[$type] = $time;
35+
}
36+
3237
public function renderTimingHeader(): string
3338
{
3439
$serverTimings = [];

src/view/frontend/web/js/beacon.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ const X = (e) => {
229229
document.visibilityState === "hidden" ? e() : (e = et(e), document.addEventListener("visibilitychange", e, { once: !0 }), t(() => {
230230
e(), document.removeEventListener("visibilitychange", e);
231231
}));
232-
}, pt = [200, 500], Bt = (e, t = {}) => {
232+
}, pt = [200, 500], qt = (e, t = {}) => {
233233
const r = w(t = Object.assign({}, t), mt);
234234
let i = [], n = [], o = 0;
235235
const a = /* @__PURE__ */ new WeakMap(), s = /* @__PURE__ */ new WeakMap();
@@ -278,13 +278,13 @@ const X = (e) => {
278278
if (!v.longAnimationFrameEntries?.length) return;
279279
const D = v.interactionTime, _ = v.inputDelay, nt = v.processingDuration;
280280
let W, H, $ = 0, A = 0, V = 0, z = 0;
281-
for (const B of v.longAnimationFrameEntries) {
282-
A = A + B.startTime + B.duration - B.styleAndLayoutStart;
283-
for (const E of B.scripts) {
281+
for (const q of v.longAnimationFrameEntries) {
282+
A = A + q.startTime + q.duration - q.styleAndLayoutStart;
283+
for (const E of q.scripts) {
284284
const rt = E.startTime + E.duration;
285285
if (rt < D) continue;
286-
const q = rt - Math.max(D, E.startTime), at = E.duration ? q / E.duration * E.forcedStyleAndLayoutDuration : 0;
287-
$ += q - at, A += at, q > z && (H = E.startTime < D + _ ? "input-delay" : E.startTime >= D + _ + nt ? "presentation-delay" : "processing-duration", W = E, z = q);
286+
const B = rt - Math.max(D, E.startTime), at = E.duration ? B / E.duration * E.forcedStyleAndLayoutDuration : 0;
287+
$ += B - at, A += at, B > z && (H = E.startTime < D + _ ? "input-delay" : E.startTime >= D + _ + nt ? "presentation-delay" : "processing-duration", W = E, z = B);
288288
}
289289
}
290290
const J = v.longAnimationFrameEntries.at(-1), it = J ? J.startTime + J.duration : 0;
@@ -321,7 +321,7 @@ class ht {
321321
this.m?.(t);
322322
}
323323
}
324-
const vt = [2500, 4e3], qt = (e, t = {}) => {
324+
const vt = [2500, 4e3], Bt = (e, t = {}) => {
325325
const r = w(t = Object.assign({}, t), ht), i = /* @__PURE__ */ new WeakMap();
326326
r.m = (n) => {
327327
if (n.element) {
@@ -404,7 +404,7 @@ function Nt(e) {
404404
let s = !0;
405405
n > 0 ? (s = n, i = Math.max(i, n)) : n <= 0 && Rt.test(o) ? s = parseInt(o) : a === jt ? (s = o === Ut, r.isCached = s) : o && (s = o), r.serverTiming[a] = s;
406406
}
407-
r.isCached === !1 && r.waiting > 0 && r.waiting < i && (r.isCached = !0);
407+
r.isCached === !1 && r.request > 0 && r.request < i && (r.isCached = !0);
408408
}
409409
e.ttfb = r, e.pageInfo.isCached = r.isCached;
410410
}, {
@@ -422,7 +422,7 @@ function _t(e) {
422422
});
423423
}
424424
function Wt(e) {
425-
qt((t) => {
425+
Bt((t) => {
426426
e.lcp = {
427427
value: t.value,
428428
navigationType: t.navigationType,
@@ -446,7 +446,7 @@ function Ht(e) {
446446
});
447447
}
448448
function $t(e) {
449-
Bt((t) => {
449+
qt((t) => {
450450
const r = t.attribution.longestScript, i = {
451451
value: t.value,
452452
navigationType: t.navigationType,

0 commit comments

Comments
 (0)