site: address PR review on version badge fetch
- gate DOM update on DOMContentLoaded — inline.js is loaded sync in <head>, so a cache-hit fetch can resolve before the placeholder span is parsed. - hide placeholder span by default (`hidden`) so a failed/blocked fetch doesn't leave a 0.5rem stray gap before the github icon. - log fetch failures (rate limit, offline, blocked) instead of silently swallowing — matches the prior behaviour of build-time github.js.
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
</nav>
|
||||
<div class="order-2 md:order-3 flex space-x-2 ml-auto py-1 px-4">
|
||||
<a class="hover:text-gray-800 dark:hover:text-gray-300 flex items-center" aria-label="Remark42's GitHub Repository" rel="noopener noreferrer" href="{{ site.githubUrl}}/releases" target="_blank">
|
||||
<span class="mr-2" data-remark42-version></span>
|
||||
<span class="mr-2" data-remark42-version hidden></span>
|
||||
<svg width="28" height="28" viewBox="0 0 24 24" fill="none">
|
||||
<path fill="currentColor" d="M12 3C7.0275 3 3 7.12937 3 12.2276C3 16.3109 5.57625 19.7597 9.15374 20.9824C9.60374 21.0631 9.77249 20.7863 9.77249 20.5441C9.77249 20.3249 9.76125 19.5982 9.76125 18.8254C7.5 19.2522 6.915 18.2602 6.735 17.7412C6.63375 17.4759 6.19499 16.6569 5.8125 16.4378C5.4975 16.2647 5.0475 15.838 5.80124 15.8264C6.51 15.8149 7.01625 16.4954 7.18499 16.7723C7.99499 18.1679 9.28875 17.7758 9.80625 17.5335C9.885 16.9337 10.1212 16.53 10.38 16.2993C8.3775 16.0687 6.285 15.2728 6.285 11.7432C6.285 10.7397 6.63375 9.9092 7.20749 9.26326C7.1175 9.03257 6.8025 8.08674 7.2975 6.81794C7.2975 6.81794 8.05125 6.57571 9.77249 7.76377C10.4925 7.55615 11.2575 7.45234 12.0225 7.45234C12.7875 7.45234 13.5525 7.55615 14.2725 7.76377C15.9937 6.56418 16.7475 6.81794 16.7475 6.81794C17.2424 8.08674 16.9275 9.03257 16.8375 9.26326C17.4113 9.9092 17.76 10.7281 17.76 11.7432C17.76 15.2843 15.6563 16.0687 13.6537 16.2993C13.98 16.5877 14.2613 17.1414 14.2613 18.0065C14.2613 19.2407 14.25 20.2326 14.25 20.5441C14.25 20.7863 14.4188 21.0746 14.8688 20.9824C16.6554 20.364 18.2079 19.1866 19.3078 17.6162C20.4077 16.0457 20.9995 14.1611 21 12.2276C21 7.12937 16.9725 3 12 3Z"></path>
|
||||
</svg>
|
||||
|
||||
@@ -7,14 +7,25 @@ if ((theme && theme === 'dark') || (!theme && mq.matches)) {
|
||||
|
||||
// fetch the latest release version client-side so the badge tracks releases
|
||||
// without needing a site rebuild. GitHub serves this with Cache-Control:
|
||||
// public, max-age=60 so the per-visitor cost is bounded. fallback stays
|
||||
// empty on any failure — graceful degradation, no broken UI.
|
||||
// public, max-age=60 so the per-visitor cost is bounded. placeholder is
|
||||
// `hidden` in the template so a failed/blocked fetch leaves no stray gap.
|
||||
fetch('https://api.github.com/repos/umputun/remark42/releases/latest')
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.then((r) => (r.ok ? r.json() : Promise.reject(new Error('HTTP ' + r.status))))
|
||||
.then((d) => {
|
||||
if (!d || !d.tag_name) return
|
||||
document.querySelectorAll('[data-remark42-version]').forEach((el) => {
|
||||
el.textContent = d.tag_name
|
||||
})
|
||||
// script is loaded synchronously in <head>, so a cache hit can resolve
|
||||
// before <body> is parsed and [data-remark42-version] exists. defer the
|
||||
// DOM update until the document is ready.
|
||||
const apply = () => {
|
||||
document.querySelectorAll('[data-remark42-version]').forEach((el) => {
|
||||
el.textContent = d.tag_name
|
||||
el.hidden = false
|
||||
})
|
||||
}
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', apply)
|
||||
} else {
|
||||
apply()
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
.catch((err) => console.warn('remark42-site: latest version fetch failed', err))
|
||||
|
||||
Reference in New Issue
Block a user