Skip to content

Commit 9b52f99

Browse files
committed
feat (img): add fallback for cloudinary
1 parent 93d7a8a commit 9b52f99

9 files changed

Lines changed: 88 additions & 68 deletions

File tree

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<nav>
1414
<div class="logo" onclick="showPage('home')">
15-
<img src="/https://res.cloudinary.com/duij1lw6u/image/upload/f_auto,q_auto/public/logo/Paws Logo.jpg" alt="Pawsitive Logo" style="height:32px;vertical-align:middle;margin-right:8px;">
15+
<img src="/https://res.cloudinary.com/duij1lw6u/image/upload/f_auto,q_auto/public/logo/Paws Logo.jpg" alt="Pawsitive Logo" style="height:32px;vertical-align:middle;margin-right:8px;" onerror="_cldImgError(this)">
1616
pawsitive
1717
</div>
1818
<div class="nav-links" id="nav-links">
@@ -51,7 +51,7 @@
5151
<div class="img-lightbox" id="imgLightbox" onclick="closeLightbox(event)">
5252
<button class="lb-nav lb-prev" onclick="event.stopPropagation();lightboxNav(-1)">&#8249;</button>
5353
<div class="lb-inner" onclick="event.stopPropagation()">
54-
<img id="lbImg" src="" alt="Pawsitive photo">
54+
<img id="lbImg" src="" alt="Pawsitive photo" onerror="_cldImgError(this)">
5555
<div class="lb-counter" id="lbCounter"></div>
5656
</div>
5757
<button class="lb-nav lb-next" onclick="event.stopPropagation();lightboxNav(1)">&#8250;</button>

js/app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
/* ─── app.js ─── page navigation, page loading, theme ─── */
22

3+
/* ── Cloudinary image fallback — falls back to local /public/... copy ── */
4+
function _cldImgError(img) {
5+
img.onerror = null; // prevent infinite loop
6+
const src = img.src;
7+
const m = src.match(/\/public\/.+/);
8+
if (m) { img.src = m[0]; return; }
9+
if (img.dataset.fallback) { img.src = img.dataset.fallback; delete img.dataset.fallback; }
10+
}
11+
312
/* ── Shared scroll-lock utility (used by all modals) ── */
413
function lockScroll() {
514
document.body.style.overflow = 'hidden';

js/dogs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function initDogPhotos() {
230230
applyDogOrientation(img);
231231
img.classList.add('loaded');
232232
});
233-
img.addEventListener('error', () => { img.style.display = 'none'; });
233+
img.addEventListener('error', () => { _cldImgError(img); });
234234
}
235235
});
236236
}
@@ -315,7 +315,7 @@ function initDogModals() {
315315
.join('');
316316

317317
const photoSection = card.dataset.image
318-
? `<div class="dog-modal-photo"><img src="${card.dataset.image}" alt="${card.dataset.name}" style="opacity:0;transition:opacity 0.3s" onload="this.style.opacity=1"></div>`
318+
? `<div class="dog-modal-photo"><img src="${card.dataset.image}" alt="${card.dataset.name}" style="opacity:0;transition:opacity 0.3s" onload="this.style.opacity=1" onerror="_cldImgError(this)"></div>`
319319
: `<div class="dog-modal-emoji" style="background:${bg}"><span style="font-size:7rem">${card.dataset.emoji || '🐕'}</span></div>`;
320320

321321
const statusBadges = [];

js/gallery.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,16 @@ function renderMediaGrid() {
340340
tile.className = 'media-tile media-tile-loading';
341341
const img = document.createElement('img');
342342
img.dataset.src = _imgUrl(filename);
343+
img.dataset.fallback = '/public/gallery/' + filename;
343344
img.alt = 'Pawsitive campus photo';
344345
img.onload = () => {
345346
img.classList.add('loaded');
346347
tile.classList.remove('media-tile-loading');
347348
};
348-
img.onerror = () => tile.classList.remove('media-tile-loading');
349+
img.onerror = () => {
350+
if (img.dataset.fallback) { const fb = img.dataset.fallback; delete img.dataset.fallback; img.src = fb; }
351+
else { tile.classList.remove('media-tile-loading'); }
352+
};
349353
const overlay = document.createElement('div');
350354
overlay.className = 'media-tile-overlay';
351355
overlay.innerHTML = '<span>🔍</span>';
@@ -377,7 +381,9 @@ function openLightbox(i) {
377381
_lbImages = null;
378382
_lbIndex = i;
379383
const lb = document.getElementById('imgLightbox');
380-
document.getElementById('lbImg').src = _imgUrl(mediaImages[_lbIndex]);
384+
const lbImg = document.getElementById('lbImg');
385+
lbImg.dataset.fallback = '/public/gallery/' + mediaImages[_lbIndex];
386+
lbImg.src = _imgUrl(mediaImages[_lbIndex]);
381387
document.getElementById('lbCounter').textContent = (_lbIndex + 1) + ' / ' + mediaImages.length;
382388
lb.classList.add('open');
383389
lockScroll();
@@ -408,7 +414,12 @@ function lightboxNav(dir) {
408414
const img = document.getElementById('lbImg');
409415
img.style.opacity = '0';
410416
setTimeout(() => {
411-
img.src = _lbImages ? _lbImages[_lbIndex] : _imgUrl(mediaImages[_lbIndex]);
417+
if (_lbImages) {
418+
img.src = _lbImages[_lbIndex];
419+
} else {
420+
img.dataset.fallback = '/public/gallery/' + mediaImages[_lbIndex];
421+
img.src = _imgUrl(mediaImages[_lbIndex]);
422+
}
412423
document.getElementById('lbCounter').textContent = (_lbIndex + 1) + ' / ' + pool.length;
413424
img.style.opacity = '1';
414425
}, 150);

0 commit comments

Comments
 (0)