Skip to content

Commit

Permalink
fix(preload): fixed preloading function implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 14, 2025
1 parent 83bc8f6 commit 714f54f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
28 changes: 23 additions & 5 deletions lib/static/modules/utils/imageEntity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import {preloadImage} from '.';
import {ImageEntity, ImageEntityCommitted, ImageEntityError, ImageEntityFail, ImageEntityStaged, ImageEntitySuccess, ImageEntityUpdated} from '../../new-ui/types/store';

function preloadImage(url: string): HTMLElement {
const link = document.createElement('link');

link.rel = 'preload';
link.as = 'image';
link.href = url;
link.onload;

document.head.appendChild(link);

return link;
}

function hasExpectedImage(image: ImageEntity): image is ImageEntityFail | ImageEntitySuccess | ImageEntityUpdated {
return Object.hasOwn(image, 'expectedImg');
}
Expand All @@ -13,16 +25,22 @@ function hasDiffImage(image: ImageEntity): image is ImageEntityFail {
return Object.hasOwn(image, 'diffImg');
}

export function preloadImageEntity(image: ImageEntity): void {
export function preloadImageEntity(image: ImageEntity): () => void {
const elements: HTMLElement[] = [];

if (hasExpectedImage(image)) {
preloadImage(image.expectedImg.path);
elements.push(preloadImage(image.expectedImg.path));
}

if (hasActualImage(image)) {
preloadImage(image.actualImg.path);
elements.push(preloadImage(image.actualImg.path));
}

if (hasDiffImage(image)) {
preloadImage(image.diffImg.path);
elements.push(preloadImage(image.diffImg.path));
}

return (): void => {
elements.forEach(element => element.remove());
};
}
4 changes: 4 additions & 0 deletions lib/static/modules/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ export function parseKeyToGroupTestsBy(key) {
return [groupSection, groupKey];
}

/** @deprecated - this is just not working.
* @link https://stackoverflow.com/questions/3646036/preloading-images-with-javascript
* @see preloadImage from lib/static/modules/utils/imageEntity.ts instead
*/
export function preloadImage(url) {
new Image().src = url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const PRELOAD_IMAGES_COUNT = 3;
const usePreloadImages = (
currentNamedImageIndex: number,
visibleNamedImageIds: string[]): void => {
const preloaded = useRef<Record<string, boolean | undefined>>({});
const preloaded = useRef<Record<string, () => void | undefined>>({});

const namedImageIdsToPreload: string[] = visibleNamedImageIds.slice(
Math.max(0, currentNamedImageIndex - 1 - PRELOAD_IMAGES_COUNT),
Expand All @@ -51,10 +51,13 @@ const usePreloadImages = (
return;
}

preloadImageEntity(image);
preloaded.current[image.id] = true;
preloaded.current[image.id] = preloadImageEntity(image);
});
}, [currentNamedImageIndex]);

useEffect(() => () => {
Object.values(preloaded.current).forEach(preload => preload?.());
}, []);
};

export function VisualChecksPage(): ReactNode {
Expand Down

0 comments on commit 714f54f

Please sign in to comment.