Skip to content

Commit 455228e

Browse files
committed
Fix icons loading x
1 parent fcaa211 commit 455228e

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

packages/gitbook/e2e/util.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,25 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
345345
*/
346346
async function waitForIcons(page: Page) {
347347
await page.waitForFunction(() => {
348-
const urls = new Set<string>();
348+
const urlStates: Record<string, 'pending' | 'loaded'> = {};
349+
350+
const loadUrl = (url: string) => {
351+
// Mark the URL as pending.
352+
urlStates[url] = 'pending';
353+
354+
const img = new Image();
355+
img.onload = () => {
356+
urlStates[url] = 'loaded';
357+
};
358+
img.src = url;
359+
};
360+
349361
const icons = Array.from(document.querySelectorAll('svg.gb-icon'));
350362
const results = icons.map((icon) => {
351363
if (!(icon instanceof SVGElement)) {
352364
throw new Error('Icon is not an SVGElement');
353365
}
354366

355-
// If loaded, good it passes the test.
356-
if (icon.dataset.loadingState === 'loaded') {
357-
return true;
358-
}
359-
360-
// If not loaded yet, we need to load it.
361-
if (icon.dataset.loadingState === 'pending') {
362-
return false;
363-
}
364-
365367
// Ignore icons that are not visible.
366368
if (!icon.checkVisibility()) {
367369
return true;
@@ -370,36 +372,25 @@ async function waitForIcons(page: Page) {
370372
// url("https://ka-p.fontawesome.com/releases/v6.6.0/svgs/light/moon.svg?v=2&token=a463935e93")
371373
const maskImage = window.getComputedStyle(icon).getPropertyValue('mask-image');
372374
const urlMatch = maskImage.match(/url\("([^"]+)"\)/);
373-
const url = urlMatch ? urlMatch[1] : null;
375+
const url = urlMatch?.[1];
374376

375377
// If URL is invalid we throw an error.
376378
if (!url) {
377379
throw new Error('No mask-image');
378380
}
379381

380-
// If the URL is already loaded, we just mark it as loaded.
381-
if (urls.has(url)) {
382-
icon.dataset.loadingState = 'loaded';
383-
return true;
384-
}
385-
386-
// Mark the icon as pending and load the image.
387-
icon.dataset.loadingState = 'pending';
388-
389-
// Mark the URL as seen.
390-
urls.add(url);
382+
// If the URL is already queued for loading, we return the state.
383+
if (urlStates[url]) {
384+
if (urlStates[url] === 'loaded') {
385+
// Trigger the icon to re-render.
386+
icon.style.maskImage = `url(${url})`;
387+
return true;
388+
}
391389

392-
const img = new Image();
393-
img.src = url;
394-
img.decode().then(() => {
395-
// Wait two frames to let the time to the icon to repaint.
396-
requestAnimationFrame(() => {
397-
requestAnimationFrame(() => {
398-
icon.dataset.loadingState = 'loaded';
399-
});
400-
});
401-
});
390+
return false;
391+
}
402392

393+
loadUrl(url);
403394
return false;
404395
});
405396

0 commit comments

Comments
 (0)