Heroes Overview

Heroine Hero

The thumbnails do not take long to load. If you click on a thumbnail, a page with a larger version of the image is displayed. If the larger image is not in the cache, the morph effect looks bad. To avoid this, we can retrieve the larger image in advance by using the loader function of the astro:before-preparation event.

The extended version calls the original loader and then preloads the hero image after checking the newDocument for the source.

If the larger images are not in your cache, the page may appear to be unresponsive to your click. It would be a good idea to show a loading indicator to signal that the click has been detected, but it takes a second for the next page to occur.

hero-prefetch.js
document.addEventListener('astro:before-preparation', e => {
if (e.from.href.endsWith('/overview/')) {
const originalLoader = event.loader;
event.loader = async () => {
await originalLoader();
const img = new Image();
img.src = event.newDocument.querySelector('img[class*="hero"]').src;
await new Promise((resolve) => {
img.onload = img.onerror = resolve
});
};
}
});