@xingwangzhe/stalux 1.28.3 → 1.28.4
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/package.json
CHANGED
|
@@ -12,6 +12,36 @@ declare global {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
function appendWhenIdle(script: HTMLScriptElement): void {
|
|
16
|
+
const append = () => document.head.appendChild(script);
|
|
17
|
+
const requestIdle = (
|
|
18
|
+
window as Window & {
|
|
19
|
+
requestIdleCallback?: (callback: () => void, options?: { timeout: number }) => number;
|
|
20
|
+
}
|
|
21
|
+
).requestIdleCallback;
|
|
22
|
+
|
|
23
|
+
if (requestIdle) requestIdle.call(window, append, { timeout: 1200 });
|
|
24
|
+
else window.setTimeout(append, 0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function appendAfterPageLoad(script: HTMLScriptElement): void {
|
|
28
|
+
let appended = false;
|
|
29
|
+
const schedule = () => {
|
|
30
|
+
if (appended) return;
|
|
31
|
+
appended = true;
|
|
32
|
+
appendWhenIdle(script);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (document.readyState === "complete") {
|
|
36
|
+
schedule();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
window.addEventListener("load", schedule, { once: true });
|
|
41
|
+
// Avoid indefinitely delaying analytics if an image or other resource stalls load.
|
|
42
|
+
window.setTimeout(schedule, 5000);
|
|
43
|
+
}
|
|
44
|
+
|
|
15
45
|
function load() {
|
|
16
46
|
const gaID = document.body?.dataset.staluxGaId;
|
|
17
47
|
if (!gaID) return;
|
|
@@ -21,6 +51,7 @@ function load() {
|
|
|
21
51
|
window.__staluxGoogleScriptLoaded = true;
|
|
22
52
|
const script = document.createElement("script");
|
|
23
53
|
script.async = true;
|
|
54
|
+
script.fetchPriority = "low";
|
|
24
55
|
script.dataset.staluxGoogle = "true";
|
|
25
56
|
script.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(gaID)}`;
|
|
26
57
|
script.addEventListener("load", () => logger.debug("external script loaded"), {
|
|
@@ -31,8 +62,8 @@ function load() {
|
|
|
31
62
|
() => logger.warn("external script failed to load (network or content blocker)"),
|
|
32
63
|
{ once: true },
|
|
33
64
|
);
|
|
34
|
-
logger.debug("
|
|
35
|
-
|
|
65
|
+
logger.debug("deferred external script until page load and idle time");
|
|
66
|
+
appendAfterPageLoad(script);
|
|
36
67
|
}
|
|
37
68
|
if (!window.__staluxAnalyticsLoaded) {
|
|
38
69
|
window.__staluxAnalyticsLoaded = true;
|