|
| 1 | +/** |
| 2 | + * Utility to preload images for better performance |
| 3 | + * Preloads critical images that are likely to be viewed soon |
| 4 | + */ |
| 5 | + |
| 6 | +import { getImageUrl } from './imageUtils'; |
| 7 | +import contentData from '../data/content'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Preload critical images that appear on the homepage and common pages |
| 11 | + */ |
| 12 | +export function preloadCriticalImages() { |
| 13 | + const criticalImages: string[] = [ |
| 14 | + // Home page image |
| 15 | + getImageUrl("src/assets/ray.png"), |
| 16 | + // Logo |
| 17 | + getImageUrl("/assets/raylogo.png"), |
| 18 | + // Common story thumbnails |
| 19 | + getImageUrl("/assets/aishift.png"), |
| 20 | + getImageUrl("/assets/annapurna.png"), |
| 21 | + getImageUrl("/assets/yose.jpg"), |
| 22 | + // Profile image |
| 23 | + getImageUrl("src/assets/mitregan.png"), |
| 24 | + ]; |
| 25 | + |
| 26 | + criticalImages.forEach((imageUrl) => { |
| 27 | + if (imageUrl && !imageUrl.startsWith('http')) { |
| 28 | + const link = document.createElement('link'); |
| 29 | + link.rel = 'preload'; |
| 30 | + link.as = 'image'; |
| 31 | + link.href = imageUrl; |
| 32 | + document.head.appendChild(link); |
| 33 | + } else if (imageUrl && imageUrl.startsWith('http')) { |
| 34 | + // For external images, use Image object to preload |
| 35 | + const img = new Image(); |
| 36 | + img.src = imageUrl; |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Preload images for a specific story |
| 43 | + */ |
| 44 | +export function preloadStoryImages(storyId: string) { |
| 45 | + const allStories = [ |
| 46 | + ...contentData.storiesOfAdventure.stories, |
| 47 | + ...contentData.impact.stories, |
| 48 | + ]; |
| 49 | + |
| 50 | + const story = allStories.find((s) => s.id === storyId); |
| 51 | + if (!story || !story.content.images) return; |
| 52 | + |
| 53 | + story.content.images.forEach((image) => { |
| 54 | + const imageUrl = typeof image === 'string' ? image : image.url; |
| 55 | + const fullUrl = getImageUrl(imageUrl); |
| 56 | + |
| 57 | + if (fullUrl && !fullUrl.startsWith('http')) { |
| 58 | + const link = document.createElement('link'); |
| 59 | + link.rel = 'preload'; |
| 60 | + link.as = 'image'; |
| 61 | + link.href = fullUrl; |
| 62 | + document.head.appendChild(link); |
| 63 | + } else if (fullUrl && fullUrl.startsWith('http')) { |
| 64 | + const img = new Image(); |
| 65 | + img.src = fullUrl; |
| 66 | + } |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Preload all photography images |
| 72 | + */ |
| 73 | +export function preloadPhotographyImages() { |
| 74 | + const images = contentData.photography.images; |
| 75 | + // Preload first 10 images (most likely to be viewed) |
| 76 | + images.slice(0, 10).forEach((img) => { |
| 77 | + const imageUrl = getImageUrl(img.url); |
| 78 | + if (imageUrl && !imageUrl.startsWith('http')) { |
| 79 | + const link = document.createElement('link'); |
| 80 | + link.rel = 'preload'; |
| 81 | + link.as = 'image'; |
| 82 | + link.href = imageUrl; |
| 83 | + document.head.appendChild(link); |
| 84 | + } else if (imageUrl && imageUrl.startsWith('http')) { |
| 85 | + const imgElement = new Image(); |
| 86 | + imgElement.src = imageUrl; |
| 87 | + } |
| 88 | + }); |
| 89 | +} |
0 commit comments