|
3 | 3 |
|
4 | 4 | (function() {
|
5 | 5 | 'use strict';
|
6 |
| - |
| 6 | + |
| 7 | + const SAFETY_MARGIN_PX = 120; |
| 8 | + |
| 9 | + function raf(callback) { |
| 10 | + if (typeof window.requestAnimationFrame === 'function') { |
| 11 | + return window.requestAnimationFrame(callback); |
| 12 | + } |
| 13 | + return window.setTimeout(callback, 16); |
| 14 | + } |
| 15 | + |
| 16 | + function whenLayoutSettled(callback) { |
| 17 | + const run = function() { |
| 18 | + raf(callback); |
| 19 | + }; |
| 20 | + |
| 21 | + // Running twice helps ensure fonts/layout have settled |
| 22 | + raf(function() { |
| 23 | + if (document.fonts && typeof document.fonts.ready === 'object' && typeof document.fonts.ready.then === 'function') { |
| 24 | + document.fonts.ready.then(run).catch(run); |
| 25 | + } else { |
| 26 | + run(); |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + |
7 | 31 | // Safe font optimization - only adds font-display: swap to Google Fonts
|
8 | 32 | function optimizeFonts() {
|
9 | 33 | const fontLinks = document.querySelectorAll('link[href*="fonts.googleapis.com"]');
|
|
13 | 37 | }
|
14 | 38 | });
|
15 | 39 | }
|
16 |
| - |
| 40 | + |
| 41 | + function markLazyImages(images) { |
| 42 | + if (!images.length) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + whenLayoutSettled(function() { |
| 47 | + const viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0; |
| 48 | + images.forEach(function(img) { |
| 49 | + if (img.hasAttribute('loading')) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const rect = img.getBoundingClientRect(); |
| 54 | + if (rect.bottom <= 0) { |
| 55 | + return; // Above the viewport, keep eager |
| 56 | + } |
| 57 | + |
| 58 | + const threshold = viewportHeight + SAFETY_MARGIN_PX; |
| 59 | + if (rect.top >= threshold) { |
| 60 | + img.loading = 'lazy'; |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + function markLazyImagesWithObserver(images) { |
| 67 | + const observer = new IntersectionObserver(function(entries, obs) { |
| 68 | + entries.forEach(function(entry) { |
| 69 | + const img = entry.target; |
| 70 | + if (img.hasAttribute('loading')) { |
| 71 | + obs.unobserve(img); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (entry.isIntersecting) { |
| 76 | + obs.unobserve(img); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0; |
| 81 | + if (entry.boundingClientRect.top >= viewportHeight + SAFETY_MARGIN_PX) { |
| 82 | + img.loading = 'lazy'; |
| 83 | + obs.unobserve(img); |
| 84 | + } |
| 85 | + }); |
| 86 | + }, { rootMargin: SAFETY_MARGIN_PX + 'px 0px' }); |
| 87 | + |
| 88 | + images.forEach(function(img) { |
| 89 | + observer.observe(img); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
17 | 93 | // Safe image optimization - only adds lazy loading to images below the fold
|
18 | 94 | function optimizeImages() {
|
19 |
| - const images = document.querySelectorAll('img:not([loading])'); |
20 |
| - images.forEach(img => { |
21 |
| - // Only add lazy loading to images that are below the fold |
22 |
| - if (img.offsetTop > window.innerHeight) { |
23 |
| - img.loading = 'lazy'; |
24 |
| - } |
| 95 | + const images = Array.prototype.slice.call(document.querySelectorAll('img')); |
| 96 | + if (!images.length) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const candidates = images.filter(function(img) { |
| 101 | + return !img.hasAttribute('loading'); |
25 | 102 | });
|
| 103 | + |
| 104 | + if (!candidates.length) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + if ('IntersectionObserver' in window) { |
| 109 | + markLazyImagesWithObserver(candidates); |
| 110 | + } else { |
| 111 | + markLazyImages(candidates); |
| 112 | + } |
26 | 113 | }
|
27 |
| - |
| 114 | + |
28 | 115 | // Safe resource hints - only adds DNS prefetch for external domains
|
29 | 116 | function addResourceHints() {
|
30 | 117 | const externalDomains = [
|
31 | 118 | 'fonts.googleapis.com',
|
32 | 119 | 'fonts.gstatic.com',
|
33 | 120 | 'cloud.umami.is'
|
34 | 121 | ];
|
35 |
| - |
| 122 | + |
| 123 | + const head = document.head || document.getElementsByTagName('head')[0]; |
| 124 | + if (!head) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (!document.querySelector('meta[http-equiv="x-dns-prefetch-control"]')) { |
| 129 | + const meta = document.createElement('meta'); |
| 130 | + meta.httpEquiv = 'x-dns-prefetch-control'; |
| 131 | + meta.content = 'on'; |
| 132 | + head.insertBefore(meta, head.firstChild); |
| 133 | + } |
| 134 | + |
36 | 135 | externalDomains.forEach(domain => {
|
37 |
| - if (!document.querySelector(`link[href="//${domain}"]`)) { |
| 136 | + if (!document.querySelector(`link[rel="dns-prefetch"][href="//${domain}"]`)) { |
38 | 137 | const link = document.createElement('link');
|
39 | 138 | link.rel = 'dns-prefetch';
|
40 | 139 | link.href = `//${domain}`;
|
41 |
| - document.head.appendChild(link); |
| 140 | + head.insertBefore(link, head.firstChild); |
42 | 141 | }
|
43 | 142 | });
|
44 | 143 | }
|
45 |
| - |
| 144 | + |
46 | 145 | // Initialize only safe optimizations
|
47 | 146 | function init() {
|
48 | 147 | // Run immediately
|
49 | 148 | optimizeFonts();
|
50 | 149 | addResourceHints();
|
51 |
| - |
| 150 | + |
52 | 151 | // Run after DOM is ready
|
53 | 152 | if (document.readyState === 'loading') {
|
54 |
| - document.addEventListener('DOMContentLoaded', optimizeImages); |
| 153 | + document.addEventListener('DOMContentLoaded', optimizeImages, { once: true }); |
55 | 154 | } else {
|
56 | 155 | optimizeImages();
|
57 | 156 | }
|
58 | 157 | }
|
59 |
| - |
| 158 | + |
60 | 159 | // Start optimizations
|
61 | 160 | init();
|
62 |
| - |
| 161 | + |
63 | 162 | })();
|
0 commit comments