diff options
Diffstat (limited to 'assets/js/includes.js')
| -rw-r--r-- | assets/js/includes.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/assets/js/includes.js b/assets/js/includes.js new file mode 100644 index 0000000..4af99f4 --- /dev/null +++ b/assets/js/includes.js @@ -0,0 +1,32 @@ +(async function () { + async function loadInto(selector, url) { + const mount = document.querySelector(selector); + if (!mount) return; + + try { + const res = await fetch(url); + if (!res.ok) throw new Error("HTTP " + res.status); + const html = await res.text(); + mount.innerHTML = html; + } catch (e) { + // Fail open: if includes fail, keep page usable. + // Intentionally no console noise. + } + } + + // Pages opt-in by including: <div data-include="header"></div> + // and/or <div data-include="footer"></div> + await Promise.all([ + loadInto('[data-include="header"]', "/partials/header.html"), + loadInto('[data-include="footer"]', "/partials/footer.html"), + ]); + + // If a page uses includes, it likely removed the built-in header/footer. + // Re-run site.js behaviors after injection. + if (document.querySelector('[data-include="header"], [data-include="footer"]')) { + const s = document.createElement("script"); + s.src = "/assets/js/site.js"; + s.defer = true; + document.head.appendChild(s); + } +})(); |
