summaryrefslogtreecommitdiff
path: root/assets/js/includes.js
blob: 4af99f4d745eb310c80df0d7d0f0dd7841f5ef3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
  }
})();