release: WRNexusJS 0.8.2
This commit is contained in:
@@ -10,6 +10,9 @@ export function webVitalsClient(options: WebVitalsClientOptions = {}): string {
|
||||
if (!window.PerformanceObserver || Math.random() > ${sampleRate}) return;
|
||||
var endpoint = ${endpoint};
|
||||
var cls = 0;
|
||||
var lcp = 0;
|
||||
var inp = 0;
|
||||
var flushed = false;
|
||||
function rating(name, value) {
|
||||
if (name === "LCP") return value <= 2500 ? "good" : value <= 4000 ? "needs-improvement" : "poor";
|
||||
if (name === "INP") return value <= 200 ? "good" : value <= 500 ? "needs-improvement" : "poor";
|
||||
@@ -21,9 +24,17 @@ export function webVitalsClient(options: WebVitalsClientOptions = {}): string {
|
||||
if (navigator.sendBeacon) navigator.sendBeacon(endpoint, new Blob([body], { type: "application/json" }));
|
||||
else fetch(endpoint, { method: "POST", headers: { "content-type": "application/json" }, body: body, keepalive: true }).catch(function(){});
|
||||
}
|
||||
try { new PerformanceObserver(function(list){ var entries=list.getEntries(); var last=entries[entries.length-1]; if(last) send("LCP", last.startTime); }).observe({type:"largest-contentful-paint",buffered:true}); } catch(_) {}
|
||||
function flush() {
|
||||
if (flushed) return;
|
||||
flushed = true;
|
||||
if (lcp) send("LCP", lcp);
|
||||
if (inp) send("INP", inp);
|
||||
if (cls) send("CLS", cls);
|
||||
}
|
||||
try { new PerformanceObserver(function(list){ var entries=list.getEntries(); var last=entries[entries.length-1]; if(last) lcp = Math.max(lcp, last.startTime); }).observe({type:"largest-contentful-paint",buffered:true}); } catch(_) {}
|
||||
try { new PerformanceObserver(function(list){ list.getEntries().forEach(function(e){ if(!e.hadRecentInput) cls += e.value; }); }).observe({type:"layout-shift",buffered:true}); } catch(_) {}
|
||||
try { new PerformanceObserver(function(list){ var max=0; list.getEntries().forEach(function(e){ max=Math.max(max,e.duration||0); }); if(max) send("INP",max); }).observe({type:"event",durationThreshold:40,buffered:true}); } catch(_) {}
|
||||
addEventListener("visibilitychange", function(){ if(document.visibilityState === "hidden" && cls) send("CLS", cls); }, { once: true });
|
||||
try { new PerformanceObserver(function(list){ list.getEntries().forEach(function(e){ inp=Math.max(inp,e.duration||0); }); }).observe({type:"event",durationThreshold:40,buffered:true}); } catch(_) {}
|
||||
addEventListener("visibilitychange", function(){ if(document.visibilityState === "hidden") flush(); });
|
||||
addEventListener("pagehide", flush, { once: true });
|
||||
})();`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user