Skip to content

Commit 1c70868

Browse files
committed
refactor: make core CWV scripts agent-first (remove console output)
1 parent 893f8d6 commit 1c70868

3 files changed

Lines changed: 69 additions & 465 deletions

File tree

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,43 @@
1-
// CLS Quick Check
2-
// https://webperf-snippets.nucliweb.net
3-
41
(() => {
52
let cls = 0;
63

74
const valueToRating = (score) =>
85
score <= 0.1 ? "good" : score <= 0.25 ? "needs-improvement" : "poor";
96

10-
const RATING = {
11-
good: { icon: "🟢", color: "#0CCE6A" },
12-
"needs-improvement": { icon: "🟡", color: "#FFA400" },
13-
poor: { icon: "🔴", color: "#FF4E42" },
14-
};
15-
16-
const logCLS = () => {
17-
const rating = valueToRating(cls);
18-
const { icon, color } = RATING[rating];
19-
console.log(
20-
`%cCLS: ${icon} ${cls.toFixed(4)} (${rating})`,
21-
`color: ${color}; font-weight: bold; font-size: 14px;`
22-
);
23-
};
24-
257
const observer = new PerformanceObserver((list) => {
268
for (const entry of list.getEntries()) {
27-
if (!entry.hadRecentInput) {
28-
cls += entry.value;
29-
}
9+
if (!entry.hadRecentInput) cls += entry.value;
3010
}
31-
logCLS();
3211
});
3312

3413
observer.observe({ type: "layout-shift", buffered: true });
3514

36-
// Update on visibility change (final CLS)
3715
document.addEventListener("visibilitychange", () => {
38-
if (document.visibilityState === "hidden") {
39-
observer.takeRecords();
40-
console.log("%c📊 Final CLS (on page hide):", "font-weight: bold;");
41-
logCLS();
42-
}
16+
if (document.visibilityState === "hidden") observer.takeRecords();
4317
});
4418

45-
// Expose function for manual check
46-
window.getCLS = () => {
47-
logCLS();
48-
const rating = valueToRating(cls);
49-
return {
50-
script: "CLS",
51-
status: "ok",
52-
metric: "CLS",
53-
value: Math.round(cls * 10000) / 10000,
54-
unit: "score",
55-
rating,
56-
thresholds: { good: 0.1, needsImprovement: 0.25 },
57-
};
58-
};
59-
60-
console.log(
61-
" Call %cgetCLS()%c anytime to check current value.",
62-
"font-family: monospace; background: #f3f4f6; padding: 2px 4px;",
63-
""
64-
);
19+
window.getCLS = () => ({
20+
script: "CLS",
21+
status: "ok",
22+
metric: "CLS",
23+
value: Math.round(cls * 10000) / 10000,
24+
unit: "score",
25+
rating: valueToRating(cls),
26+
thresholds: { good: 0.1, needsImprovement: 0.25 },
27+
});
6528

6629
// Synchronous return for agent (buffered entries)
67-
const clsSync = performance.getEntriesByType("layout-shift")
68-
.reduce((sum, e) => !e.hadRecentInput ? sum + e.value : sum, 0);
69-
const clsRating = valueToRating(clsSync);
30+
const clsSync = performance
31+
.getEntriesByType("layout-shift")
32+
.reduce((sum, e) => (!e.hadRecentInput ? sum + e.value : sum), 0);
33+
7034
return {
7135
script: "CLS",
7236
status: "ok",
7337
metric: "CLS",
7438
value: Math.round(clsSync * 10000) / 10000,
7539
unit: "score",
76-
rating: clsRating,
40+
rating: valueToRating(clsSync),
7741
thresholds: { good: 0.1, needsImprovement: 0.25 },
7842
};
7943
})();

0 commit comments

Comments
 (0)