|
4 | 4 | - Moves theme palette toggle to far-right |
5 | 5 | - Opens external tabs in new window |
6 | 6 | */ |
7 | | -document.addEventListener('DOMContentLoaded', function () { |
| 7 | +function initHeader() { |
8 | 8 |
|
9 | 9 | /* ── 1. Version chip — live from GitHub API ─────────── */ |
10 | 10 | var title = document.querySelector('.md-header__title'); |
11 | | - var chip; |
12 | | - if (title) { |
| 11 | + var chip = document.querySelector('.nx-version-chip'); |
| 12 | + |
| 13 | + // Skip if already injected (instant navigation guard) |
| 14 | + if (!chip && title) { |
13 | 15 | chip = document.createElement('span'); |
14 | 16 | chip.className = 'nx-version-chip'; |
15 | 17 | chip.textContent = '…'; |
16 | 18 | title.insertAdjacentElement('afterend', chip); |
17 | 19 | } |
18 | 20 |
|
19 | 21 | /* Fetch the LATEST tag from GitHub — single source of truth */ |
20 | | - fetch('https://api.github.com/repos/Prescott-Data/nexus-framework/tags') |
21 | | - .then(function (r) { return r.json(); }) |
22 | | - .then(function (tags) { |
23 | | - if (Array.isArray(tags) && tags.length > 0) { |
24 | | - var latestVersion = tags[0].name; |
25 | | - if (chip) chip.textContent = latestVersion; |
| 22 | + if (chip) { |
| 23 | + fetch('https://api.github.com/repos/Prescott-Data/nexus-framework/tags') |
| 24 | + .then(function (r) { return r.json(); }) |
| 25 | + .then(function (tags) { |
| 26 | + if (Array.isArray(tags) && tags.length > 0) { |
| 27 | + var latestVersion = tags[0].name; |
| 28 | + if (chip) chip.textContent = latestVersion; |
26 | 29 |
|
27 | | - /* Also update hero badge if present */ |
28 | | - var heroBadge = document.getElementById('nx-hero-version-badge'); |
29 | | - if (heroBadge) { |
30 | | - heroBadge.innerHTML = latestVersion + ' · Apache 2.0 · Production Ready'; |
| 30 | + /* Also update hero badge if present */ |
| 31 | + var heroBadge = document.getElementById('nx-hero-version-badge'); |
| 32 | + if (heroBadge) { |
| 33 | + heroBadge.innerHTML = latestVersion + ' · Apache 2.0 · Production Ready'; |
| 34 | + } |
31 | 35 | } |
32 | | - } |
33 | | - }) |
34 | | - .catch(function () { |
35 | | - /* On network failure, hide chip entirely rather than showing stale data */ |
36 | | - if (chip) chip.style.display = 'none'; |
37 | | - }); |
| 36 | + }) |
| 37 | + .catch(function () { |
| 38 | + /* On network failure, hide chip entirely rather than showing stale data */ |
| 39 | + if (chip) chip.style.display = 'none'; |
| 40 | + }); |
| 41 | + } |
38 | 42 |
|
39 | 43 | /* ── 2. GitHub stars widget ─────────────────────────── */ |
40 | 44 | var inner = document.querySelector('.md-header__inner'); |
41 | 45 | var palette = document.querySelector('form[data-md-component="palette"]'); |
42 | | - if (inner && palette) { |
| 46 | + var existingGhBtn = document.querySelector('.nx-gh-btn'); |
| 47 | + |
| 48 | + // Skip if already injected (instant navigation guard) |
| 49 | + if (inner && palette && !existingGhBtn) { |
43 | 50 | var ghBtn = document.createElement('a'); |
44 | 51 | ghBtn.href = 'https://github.com/Prescott-Data/nexus-framework'; |
45 | 52 | ghBtn.target = '_blank'; |
@@ -77,4 +84,12 @@ document.addEventListener('DOMContentLoaded', function () { |
77 | 84 | } |
78 | 85 | }); |
79 | 86 |
|
80 | | -}); |
| 87 | +} |
| 88 | + |
| 89 | +// Run on first load |
| 90 | +document.addEventListener('DOMContentLoaded', initHeader); |
| 91 | + |
| 92 | +// Re-run on every instant navigation (Material for MkDocs SPA mode) |
| 93 | +if (typeof document$ !== 'undefined') { |
| 94 | + document$.subscribe(initHeader); |
| 95 | +} |
0 commit comments