YouTube strip auto-refreshes every 10 min and on tab focus; instant fallback paint

This commit is contained in:
Z User 2026-11-05 20:34:35 +00:00
parent d44a1118ae
commit e8d265d7cd

View File

@ -3155,8 +3155,10 @@
// SOCIAL: latest YouTube uploads from @RobertAbbott-v7o // SOCIAL: latest YouTube uploads from @RobertAbbott-v7o
const YT_CHANNEL_ID = 'UCG_ZDOP21i0Ah26FxiuZmkg'; const YT_CHANNEL_ID = 'UCG_ZDOP21i0Ah26FxiuZmkg';
// Static snapshot — used only if /api/youtube/latest is unreachable, so the // Static snapshot — painted instantly so the section is never empty; the live
// section never renders empty. Refresh occasionally as videos are posted. // list from /api/youtube/latest replaces it as soon as it arrives, and the
// strip re-polls itself every 10 min (and on tab focus) so new uploads appear
// without a reload.
const YT_FALLBACK_VIDEOS = [ const YT_FALLBACK_VIDEOS = [
{ id: 'Bw0tDBVo_XI', title: 'GLM 5.2 runs without a graphics card?!', published: '2026-11-04T01:11:20+00:00' }, { id: 'Bw0tDBVo_XI', title: 'GLM 5.2 runs without a graphics card?!', published: '2026-11-04T01:11:20+00:00' },
{ id: 'RyDbc8bQ1o0', title: 'Run GLM 5.2 on a laptop', published: '2026-11-03T01:23:02+00:00' }, { id: 'RyDbc8bQ1o0', title: 'Run GLM 5.2 on a laptop', published: '2026-11-03T01:23:02+00:00' },
@ -3212,19 +3214,55 @@
thumb.innerHTML = `<iframe class="yt-embed" src="https://www.youtube-nocookie.com/embed/${videoId}?autoplay=1&rel=0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`; thumb.innerHTML = `<iframe class="yt-embed" src="https://www.youtube-nocookie.com/embed/${videoId}?autoplay=1&rel=0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`;
} }
async function initYtVideos() { const YT_REFRESH_MS = 10 * 60 * 1000; // re-poll interval for open pages
let videos = YT_FALLBACK_VIDEOS; let ytLastVideos = null; // last list successfully rendered
try { let ytLastFetchAt = 0; // when the backend was last asked
let ytFetching = null; // in-flight fetch (dedupes interval + visibility)
async function fetchYtVideos() {
const ctrl = new AbortController(); const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 6000); const timer = setTimeout(() => ctrl.abort(), 8000);
const res = await fetch('/api/youtube/latest', { signal: ctrl.signal }); try {
clearTimeout(timer); const res = await fetch('/api/youtube/latest?_=' + Date.now(), { signal: ctrl.signal });
const json = await res.json(); const json = await res.json();
if (res.ok && json && json.success && Array.isArray(json.data && json.data.videos) && json.data.videos.length) { if (res.ok && json && json.success && Array.isArray(json.data && json.data.videos) && json.data.videos.length) {
videos = json.data.videos; return json.data.videos;
} }
} catch (err) { /* backend unreachable -> static snapshot */ } return null;
renderYtVideos(videos); } catch (err) {
return null; // backend unreachable — keep whatever is on screen
} finally {
clearTimeout(timer);
}
}
function ytListKey(videos) {
return videos.map(v => v.id + ':' + v.title).join('|');
}
async function refreshYtVideos() {
if (ytFetching) return ytFetching;
ytFetching = fetchYtVideos().then(videos => {
ytLastFetchAt = Date.now();
if (videos && (!ytLastVideos || ytListKey(videos) !== ytListKey(ytLastVideos))) {
ytLastVideos = videos;
renderYtVideos(videos); // re-render only when something changed
}
return videos;
}).finally(() => { ytFetching = null; });
return ytFetching;
}
async function initYtVideos() {
renderYtVideos(YT_FALLBACK_VIDEOS); // instant paint, swapped for live data
ytLastVideos = YT_FALLBACK_VIDEOS;
refreshYtVideos();
setInterval(refreshYtVideos, YT_REFRESH_MS); // new uploads appear live
document.addEventListener('visibilitychange', () => {
if (!document.hidden && Date.now() - ytLastFetchAt > YT_REFRESH_MS) {
refreshYtVideos();
}
});
} }
window.addEventListener('load', () => { window.addEventListener('load', () => {