Skip to content

Commit e62c374

Browse files
sh3pikp0deje
authored andcommitted
fix: exclude loading="lazy" resources from waiting for
1 parent 4b63c8f commit e62c374

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

scripts/waiter.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
const state = {
1616
pendingRequests: 0,
17-
pendingResources: 0,
17+
resources: new Set(),
1818
activeAt: Date.now(),
1919
initialLoad: false,
2020
};
@@ -29,24 +29,34 @@
2929
hookXHR();
3030
hookFetch();
3131

32-
window[symbol] = { waitForStability };
32+
window[symbol] = {
33+
waitForStability,
34+
state,
35+
};
3336

3437
function waitForStability(options) {
3538
const idle = options?.idle ?? 500;
3639
const timeout = options?.timeout ?? 10000;
40+
const log = options?.log ?? false;
3741

3842
return new Promise((resolve, reject) => {
3943
const startTime = Date.now();
44+
let lastLogged = startTime;
4045

4146
checkStability();
4247

4348
function checkStability() {
4449
const now = Date.now();
4550

4651
const noRequests = !state.pendingRequests;
47-
const noResources = !state.pendingResources;
52+
const noResources = !state.resources.size;
4853
const isIdle = now - state.activeAt >= idle;
4954

55+
if (log && now - lastLogged >= 1000) {
56+
console.log("Alumnium waiter state:", state);
57+
lastLogged = now;
58+
}
59+
5060
if (state.initialLoad && noRequests && noResources && isIdle)
5161
return resolve();
5262

@@ -68,12 +78,13 @@
6878
const tag = el.tagName.toLowerCase();
6979

7080
const isLoaded =
81+
el.loading === "lazy" || // lazy loading
7182
el.complete || // img
7283
el.readyState === "complete" || // media
7384
(tag === "link" && el.sheet); // CSS
7485
if (isLoaded) return;
7586

76-
state.pendingResources++;
87+
state.resources.add(el);
7788
updateActiveAt();
7889

7990
el.addEventListener("load", onDone);
@@ -83,7 +94,7 @@
8394
el.removeEventListener("load", onDone);
8495
el.removeEventListener("error", onDone);
8596

86-
state.pendingResources--;
97+
state.resources.delete(el);
8798
updateActiveAt();
8899
}
89100
}

0 commit comments

Comments
 (0)