Skip to content

Commit feb77ac

Browse files
committed
Coveo: Fix vertical overflow race condition
Noticed that on hard page refreshes, and slower connections that I would still see the extra vertical overflow caused by coveo. I assumed it was a race condition, and wrapping the removal of `atomic-relevance-inspector` in a timeout, would generally resolve it. I didn't want a timeout to be the "fix" here, so I added the Mutation Observer (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) which appears to be the safest approach. There's an overall 5 second timeout still, in case search never renders for whatever reason.
1 parent acfe896 commit feb77ac

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

assets/js/coveo.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
const hideShadowElement = (shadowRoot, selector, timeout = 5000) => {
2+
if (!shadowRoot) return;
3+
4+
const hideElement = () => {
5+
const el = shadowRoot.querySelector(selector);
6+
if (el) {
7+
el.style.display = 'none';
8+
return true;
9+
}
10+
return false;
11+
};
12+
13+
if (hideElement()) {
14+
return;
15+
}
16+
17+
const observer = new MutationObserver((_mutations, obs) => {
18+
if (hideElement()) {
19+
obs.disconnect();
20+
}
21+
});
22+
23+
observer.observe(shadowRoot, { childList: true, subtree: true });
24+
25+
setTimeout(() => observer.disconnect(), timeout);
26+
};
27+
128
function isJwtExpired(token) {
229
const parts = token.split('.');
330
if (parts.length !== 3) {
@@ -90,14 +117,8 @@ async function atomicCoveo() {
90117
await searchBarSidebar.executeFirstSearch();
91118
}
92119

93-
/* Hide atomic-relevance-inspector */
94-
const shadowElements = searchBarHeader.shadowRoot.childNodes;
95-
for (let i = 0; i < shadowElements.length; i++) {
96-
const val = shadowElements[i];
97-
if (val.localName === 'atomic-relevance-inspector') {
98-
val.style.display = 'none';
99-
break;
100-
}
120+
if (searchBarHeader?.shadowRoot) {
121+
hideShadowElement(searchBarHeader.shadowRoot, 'atomic-relevance-inspector');
101122
}
102123
}
103124

0 commit comments

Comments
 (0)