Skip to content

Commit 02c6964

Browse files
More timing debugging in production.
1 parent e866a13 commit 02c6964

1 file changed

Lines changed: 95 additions & 104 deletions

File tree

_pages/media.html

Lines changed: 95 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -136,120 +136,111 @@ <h3 class="archive__item-title">
136136
{% endfor %}
137137
</section>
138138

139-
<script async src="https://www.instagram.com/embed.js"></script>
140139
<script>
141-
let masonryAttempts = 0;
142-
let lastHeights = [];
140+
// Simplified version with error handling
141+
try {
142+
let masonryAttempts = 0;
143+
144+
function updateDebug(message) {
145+
try {
146+
const debug = document.getElementById('debug-info');
147+
if (debug) {
148+
debug.innerHTML = `${message}<br>Time: ${new Date().toLocaleTimeString()}`;
149+
}
150+
} catch (e) {
151+
console.error('Debug update error:', e);
152+
}
153+
}
143154

144-
function updateDebug(message) {
145-
const debug = document.getElementById('debug-info');
146-
if (debug) {
147-
debug.innerHTML = `Attempt ${masonryAttempts}: ${message}<br>Time: ${new Date().toLocaleTimeString()}`;
155+
// Test basic functionality first
156+
updateDebug('Script loaded successfully');
157+
158+
function masonryLayout() {
159+
try {
160+
const grid = document.querySelector('.media-grid');
161+
if (!grid) {
162+
updateDebug('Grid not found');
163+
return;
164+
}
165+
166+
const items = grid.querySelectorAll('.media-card');
167+
masonryAttempts++;
168+
169+
updateDebug(`Attempt ${masonryAttempts}: Found ${items.length} items`);
170+
171+
const rowHeight = 10;
172+
const gap = 12;
173+
174+
// Reset all items
175+
items.forEach(item => {
176+
item.style.gridRowEnd = 'auto';
177+
});
178+
179+
// Force reflow
180+
grid.offsetHeight;
181+
182+
// Calculate row spans
183+
items.forEach((item, index) => {
184+
const rect = item.getBoundingClientRect();
185+
const itemHeight = rect.height;
186+
const rowSpan = Math.ceil((itemHeight + gap) / (rowHeight + gap));
187+
item.style.gridRowEnd = `span ${Math.max(1, rowSpan)}`;
188+
});
189+
190+
updateDebug(`Attempt ${masonryAttempts}: Layout applied`);
191+
192+
} catch (e) {
193+
updateDebug(`Error in masonry: ${e.message}`);
194+
console.error('Masonry error:', e);
195+
}
148196
}
149-
}
150197

151-
function masonryLayout() {
152-
const grid = document.querySelector('.media-grid');
153-
if (!grid) {
154-
updateDebug('Grid not found');
155-
return;
198+
// Simple initialization
199+
function initMasonry() {
200+
updateDebug('Initializing masonry...');
201+
setTimeout(masonryLayout, 100);
202+
setTimeout(masonryLayout, 500);
203+
setTimeout(masonryLayout, 1000);
204+
setTimeout(masonryLayout, 2000);
205+
setTimeout(masonryLayout, 4000);
156206
}
157-
158-
const items = grid.querySelectorAll('.media-card');
159-
const rowHeight = 10;
160-
const gap = 12;
161-
162-
masonryAttempts++;
163-
164-
// Reset all items
165-
items.forEach(item => {
166-
item.style.gridRowEnd = 'auto';
207+
208+
// Event listeners
209+
if (document.readyState === 'loading') {
210+
document.addEventListener('DOMContentLoaded', initMasonry);
211+
} else {
212+
initMasonry();
213+
}
214+
215+
window.addEventListener('load', () => {
216+
updateDebug('Window loaded');
217+
setTimeout(initMasonry, 200);
167218
});
168-
169-
// Force reflow
170-
grid.offsetHeight;
171-
172-
// Wait a bit for content to settle, then calculate
219+
220+
// Handle Instagram embeds if available
173221
setTimeout(() => {
174-
const currentHeights = [];
175-
176-
items.forEach((item, index) => {
177-
const rect = item.getBoundingClientRect();
178-
const itemHeight = rect.height;
179-
currentHeights.push(itemHeight);
180-
181-
const rowSpan = Math.ceil((itemHeight + gap) / (rowHeight + gap));
182-
item.style.gridRowEnd = `span ${Math.max(1, rowSpan)}`;
183-
});
184-
185-
// Check if heights are still changing (content still loading)
186-
const heightsChanged = JSON.stringify(currentHeights) !== JSON.stringify(lastHeights);
187-
lastHeights = currentHeights.slice();
188-
189-
updateDebug(`Heights changed: ${heightsChanged}, Max height: ${Math.max(...currentHeights)}px`);
190-
191-
// If heights are still changing and we haven't tried too many times, try again
192-
if (heightsChanged && masonryAttempts < 20) {
193-
setTimeout(masonryLayout, 1000);
222+
try {
223+
if (window.instgrm && window.instgrm.Embeds) {
224+
updateDebug('Processing Instagram embeds');
225+
window.instgrm.Embeds.process();
226+
setTimeout(masonryLayout, 2000);
227+
}
228+
} catch (e) {
229+
updateDebug(`Instagram error: ${e.message}`);
194230
}
195-
}, 100);
196-
}
231+
}, 2000);
197232

198-
// More aggressive initialization for production
199-
function initMasonry() {
200-
updateDebug('Starting masonry initialization');
201-
202-
// Run immediately
203-
masonryLayout();
204-
205-
// Then run at multiple intervals to catch late-loading content
206-
const delays = [100, 300, 500, 1000, 2000, 3000, 5000, 8000, 12000];
207-
208-
delays.forEach(delay => {
209-
setTimeout(() => {
210-
masonryLayout();
211-
}, delay);
212-
});
213-
}
233+
updateDebug('Script initialization complete');
214234

215-
// Multiple event listeners to catch different loading states
216-
document.addEventListener('DOMContentLoaded', () => {
217-
updateDebug('DOM loaded');
218-
initMasonry();
219-
});
220-
221-
window.addEventListener('load', () => {
222-
updateDebug('Window loaded');
223-
setTimeout(initMasonry, 200);
224-
});
225-
226-
// Instagram embed handling
227-
function handleInstagramEmbeds() {
228-
if (window.instgrm && window.instgrm.Embeds) {
229-
updateDebug('Processing Instagram embeds');
230-
window.instgrm.Embeds.process();
231-
setTimeout(masonryLayout, 1500); // Give embeds time to render
235+
} catch (e) {
236+
// Fallback error reporting
237+
const debug = document.getElementById('debug-info');
238+
if (debug) {
239+
debug.innerHTML = `Script error: ${e.message}`;
232240
}
241+
console.error('Script initialization error:', e);
233242
}
234-
235-
// Try to process Instagram embeds multiple times
236-
setTimeout(handleInstagramEmbeds, 1000);
237-
setTimeout(handleInstagramEmbeds, 3000);
238-
setTimeout(handleInstagramEmbeds, 6000);
239-
240-
// Handle window resize
241-
window.addEventListener('resize', function() {
242-
clearTimeout(window.resizeTimer);
243-
window.resizeTimer = setTimeout(() => {
244-
updateDebug('Window resized');
245-
masonryLayout();
246-
}, 300);
247-
});
248-
249-
// Keep checking for new content periodically
250-
setInterval(() => {
251-
if (masonryAttempts < 50) { // Don't run forever
252-
masonryLayout();
253-
}
254-
}, 5000);
255243
</script>
244+
245+
<!-- Load Instagram script after our script -->
246+
<script async src="https://www.instagram.com/embed.js"></script>

0 commit comments

Comments
 (0)