YouTube strip auto-refreshes every 10 min and on tab focus; instant fallback paint
This commit is contained in:
parent
d44a1118ae
commit
e8d265d7cd
60
index.html
60
index.html
@ -3155,8 +3155,10 @@
|
||||
|
||||
// SOCIAL: latest YouTube uploads from @RobertAbbott-v7o
|
||||
const YT_CHANNEL_ID = 'UCG_ZDOP21i0Ah26FxiuZmkg';
|
||||
// Static snapshot — used only if /api/youtube/latest is unreachable, so the
|
||||
// section never renders empty. Refresh occasionally as videos are posted.
|
||||
// Static snapshot — painted instantly so the section is never empty; the live
|
||||
// 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 = [
|
||||
{ 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' },
|
||||
@ -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>`;
|
||||
}
|
||||
|
||||
async function initYtVideos() {
|
||||
let videos = YT_FALLBACK_VIDEOS;
|
||||
const YT_REFRESH_MS = 10 * 60 * 1000; // re-poll interval for open pages
|
||||
let ytLastVideos = null; // last list successfully rendered
|
||||
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 timer = setTimeout(() => ctrl.abort(), 8000);
|
||||
try {
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => ctrl.abort(), 6000);
|
||||
const res = await fetch('/api/youtube/latest', { signal: ctrl.signal });
|
||||
clearTimeout(timer);
|
||||
const res = await fetch('/api/youtube/latest?_=' + Date.now(), { signal: ctrl.signal });
|
||||
const json = await res.json();
|
||||
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 */ }
|
||||
renderYtVideos(videos);
|
||||
return null;
|
||||
} 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', () => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user