Changeset 85d9fda in Klonkt for src/views/shell.ejs


Ignore:
Timestamp:
06/18/2026 04:37:16 PM (3 months ago)
Author:
roboburr <roboburr@…>
Branches:
main
Children:
255e3d3
Parents:
b08b5bc
Message:

Fix double page on back/forward (htmx history conflict)

On back/forward two history mechanisms were running: htmx's built-in
history restore (hx-history-elt on #pcms-main) AND our own popstate
listener. htmx restored #pcms-main by dumping the partial — including
the <div id=pcms-chrome hx-swap-oob> — UNFILTERED into #pcms-main
(history restore does not process OOB), while our listener was also
correctly replacing the header → a second, nested header = the entire
page doubled.

Fix: htmx history off (config.historyEnabled=false) + hx-history-elt
removed. Back/forward now runs exclusively through our popstate listener,
which re-fetches the partial via htmx.ajax — that correctly processes
the OOB chrome and the pcmsNav trigger (accent/palette/bodyClass). The
now-dead htmx:historyRestore handler has been removed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/views/shell.ejs

    rb08b5bc r85d9fda  
    270270<%- include('partials/chrome') %>
    271271
    272 <%# hx-history-elt scopes HTMX history snapshots to #pcms-main only, so
    273     when the user navigates back the switcher (which lives in the shell
    274     OUTSIDE this element) keeps its place rather than being replaced
    275     along with the rest of the body. %>
    276 <main id="pcms-main" class="pcms-main" hx-history-elt>
     272<%# Geen hx-history-elt: htmx' eigen history staat uit (zie de link-boost).
     273    Back/forward wordt door onze popstate-listener gedaan, die de partial
     274    her-fetcht (incl. correcte OOB-chrome). Met hx-history-elt + htmx-history
     275    aan dumpte htmx de partial ongefilterd hier → dubbele kop. %>
     276<main id="pcms-main" class="pcms-main">
    277277  <div id="pcms-loading" class="pcms-loading" aria-hidden="true"></div>
    278278  <%- pageContent %>
     
    361361  });
    362362
    363   // Back/forward: htmx herstelt alleen #pcms-main, maar NIET de OOB-chrome of de
    364   // per-site accent/palette (die komen via de HX-Trigger-header, en een history-
    365   // restore vuurt die niet). Zonder dit blijft de kleur van de vorige artiest
    366   // hangen. We halen de partial opnieuw op en passen alleen die twee toe —
    367   // #pcms-main is al hersteld, dus die laten we met rust.
    368   document.body.addEventListener('htmx:historyRestore', function () {
    369     var url = location.pathname + location.search;
    370     var sep = url.indexOf('?') >= 0 ? '&' : '?';
    371     fetch(url + sep + 'partial=1', { headers: { 'HX-Request': 'true' }, credentials: 'same-origin' })
    372       .then(function (r) {
    373         var trig = r.headers.get('HX-Trigger-After-Settle');
    374         if (trig) {
    375           try {
    376             var nav = JSON.parse(trig).pcmsNav;
    377             if (nav) document.body.dispatchEvent(new CustomEvent('pcmsNav', { detail: nav }));
    378           } catch (e) { /* geen/ongeldige trigger */ }
    379         }
    380         return r.text();
    381       })
    382       .then(function (html) {
    383         try {
    384           var fresh = new DOMParser().parseFromString(html, 'text/html').querySelector('#pcms-chrome');
    385           var cur = document.getElementById('pcms-chrome');
    386           if (fresh && cur) cur.replaceWith(fresh);
    387         } catch (e) { /* chrome-update overslaan */ }
    388       })
    389       .catch(function () { /* netwerkfout: laat staan */ });
    390   });
     363  // (Back/forward wordt afgehandeld door de popstate-listener in de link-boost
     364  //  hieronder — die her-fetcht de partial via htmx.ajax, wat de OOB-chrome +
     365  //  de pcmsNav-trigger (accent/palette/bodyClass) correct toepast. De vroegere
     366  //  htmx:historyRestore-handler is vervallen nu htmx-history uitstaat.)
    391367})();
    392368</script>
     
    476452(function () {
    477453  if (!window.htmx) return;
    478   // Back/forward moet her-ophalen, zodat de OOB-site-chrome (kop) meekomt met de
    479   // herstelde pagina — htmx' snapshot-cache zou anders een stale kop tonen.
    480   try { window.htmx.config.historyCacheSize = 0; } catch (_) {}
     454  // htmx' EIGEN history-afhandeling volledig uitzetten. We doen back/forward zelf
     455  // via de popstate-listener hieronder (htmx.ajax → verwerkt de OOB-chrome netjes).
     456  // Lieten we htmx z'n gang gaan, dan herstelde 'ie #pcms-main door de partial
     457  // (inclusief de <div id=pcms-chrome hx-swap-oob>) ONGEFILTERD in #pcms-main te
     458  // dumpen → een tweede, geneste kop = de pagina dubbel. Eén mechanisme nu.
     459  try { window.htmx.config.historyEnabled = false; } catch (_) {}
    481460
    482461  function fullLoad(a, url) {
Note: See TracChangeset for help on using the changeset viewer.