Skip to content

Commit 6f04926

Browse files
slarsonclaude
andcommitted
Add dedicated full news feed function and improve error handling
- Created loadFullNewsFeed() in main.js for news.html page - Added 30s timeout to prevent hanging - Added detailed console logging for debugging - Improved error messages with specific failure reasons - Simplified news.html to call shared function from main.js This eliminates code duplication and ensures consistent behavior between homepage (6 items) and news page (25 items with descriptions). Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
1 parent 0a3747c commit 6f04926

2 files changed

Lines changed: 80 additions & 54 deletions

File tree

js/main.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function refreshNews() {
133133
url: 'https://api.allorigins.win/get?url=' + encodeURIComponent('https://openworm.tumblr.com/rss'),
134134
method: 'GET',
135135
dataType: 'json',
136+
timeout: 30000,
136137
success: function(data) {
137138
var parser = new DOMParser();
138139
var xml = parser.parseFromString(data.contents, 'text/xml');
@@ -167,6 +168,80 @@ function refreshNews() {
167168
});
168169
}
169170

171+
function loadFullNewsFeed() {
172+
// Load full news feed with descriptions for news.html page
173+
console.log('Loading full news feed...');
174+
175+
$.ajax({
176+
url: 'https://api.allorigins.win/get?url=' + encodeURIComponent('https://openworm.tumblr.com/rss'),
177+
method: 'GET',
178+
dataType: 'json',
179+
timeout: 30000,
180+
success: function(data) {
181+
console.log('Feed loaded, parsing...');
182+
183+
var parser = new DOMParser();
184+
var xml = parser.parseFromString(data.contents, 'text/xml');
185+
var items = xml.querySelectorAll('item');
186+
187+
console.log('Found ' + items.length + ' items');
188+
189+
var html = '';
190+
var count = 0;
191+
192+
for (var i = 0; i < items.length && count < 25; i++) {
193+
var item = items[i];
194+
195+
var title = item.querySelector('title').textContent;
196+
var link = item.querySelector('link').textContent;
197+
var pubDate = new Date(item.querySelector('pubDate').textContent);
198+
var dateStr = pubDate.toLocaleDateString('en-US', {
199+
month: 'long',
200+
day: 'numeric',
201+
year: 'numeric'
202+
});
203+
204+
var descNode = item.querySelector('description');
205+
var description = descNode ? descNode.textContent : '';
206+
207+
var borderStyle = (count < 24) ? 'border-bottom: 1px solid #eee;' : '';
208+
209+
html += '<li style="margin-bottom: 30px; padding-bottom: 20px; ' + borderStyle + '">';
210+
html += '<h3 style="margin-top: 0;"><a href="' + link + '" target="_blank">' + title + '</a></h3>';
211+
html += '<p class="muted" style="font-size: 14px; margin-bottom: 10px;">' + dateStr + '</p>';
212+
html += '<div style="line-height: 1.6;">' + description + '</div>';
213+
html += '</li>';
214+
count++;
215+
}
216+
217+
$("#news-feed-full").html(html);
218+
console.log('Rendered ' + count + ' items');
219+
220+
// Make images responsive
221+
$("#news-feed-full img").css({
222+
"max-width": "100%",
223+
"height": "auto",
224+
"margin": "15px 0",
225+
"display": "block"
226+
});
227+
},
228+
error: function(xhr, status, err) {
229+
console.error('Error loading full feed - Status:', status, 'Error:', err);
230+
231+
var errorMsg = 'Unable to load news feed. ';
232+
if (status === 'timeout') {
233+
errorMsg += 'Request timed out.';
234+
} else if (xhr.status === 0) {
235+
errorMsg += 'Network or CORS error.';
236+
} else {
237+
errorMsg += 'Error: ' + status;
238+
}
239+
240+
$("#news-feed-full").html('<li class="muted" style="text-align: center; padding: 40px;">' + errorMsg + ' <a href="https://openworm.tumblr.com" target="_blank">View blog directly &raquo;</a></li>');
241+
}
242+
});
243+
}
244+
170245

171246
// connections to outside resources (social + GA)
172247

news.html

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -90,61 +90,12 @@ <h2>Latest News</h2>
9090
================================================== -->
9191
<!-- Placed at the end of the document so the pages load faster -->
9292
<script src="js/bootstrap.js"></script>
93+
<script src="js/main.js"></script>
9394
<script>
94-
// Load full news feed from Tumblr RSS using allOrigins CORS proxy
95-
$(function() {
96-
$.ajax({
97-
url: 'https://api.allorigins.win/get?url=' + encodeURIComponent('https://openworm.tumblr.com/rss'),
98-
method: 'GET',
99-
dataType: 'json',
100-
success: function(data) {
101-
var parser = new DOMParser();
102-
var xml = parser.parseFromString(data.contents, 'text/xml');
103-
var items = xml.querySelectorAll('item');
104-
105-
var html = '';
106-
var count = 0;
107-
items.forEach(function(item) {
108-
if (count >= 25) return;
109-
110-
var title = item.querySelector('title').textContent;
111-
var link = item.querySelector('link').textContent;
112-
var pubDate = new Date(item.querySelector('pubDate').textContent);
113-
var dateStr = pubDate.toLocaleDateString('en-US', {
114-
month: 'long',
115-
day: 'numeric',
116-
year: 'numeric'
117-
});
118-
119-
// Get description/content
120-
var descNode = item.querySelector('description');
121-
var description = descNode ? descNode.textContent : '';
122-
123-
var borderStyle = (count < 24) ? 'border-bottom: 1px solid #eee;' : '';
124-
125-
html += '<li style="margin-bottom: 30px; padding-bottom: 20px; ' + borderStyle + '">';
126-
html += '<h3 style="margin-top: 0;"><a href="' + link + '" target="_blank">' + title + '</a></h3>';
127-
html += '<p class="muted" style="font-size: 14px; margin-bottom: 10px;">' + dateStr + '</p>';
128-
html += '<div style="line-height: 1.6;">' + description + '</div>';
129-
html += '</li>';
130-
count++;
131-
});
132-
133-
$("#news-feed-full").html(html);
134-
135-
// Make images responsive
136-
$("#news-feed-full img").css({
137-
"max-width": "100%",
138-
"height": "auto",
139-
"margin": "15px 0",
140-
"display": "block"
141-
});
142-
},
143-
error: function(err) {
144-
console.error('Error loading news feed:', err);
145-
$("#news-feed-full").html('<li class="muted" style="text-align: center; padding: 40px;">Unable to load news feed. Please try again later.</li>');
146-
}
147-
});
95+
// Load full news feed when page is ready
96+
$(window).on('load', function() {
97+
console.log('news.html loaded, calling loadFullNewsFeed()');
98+
loadFullNewsFeed();
14899
});
149100
</script>
150101

0 commit comments

Comments
 (0)