| 1 | (() => { |
| 2 | |
| 3 | if (typeof navigation === 'undefined') return; |
| 4 | |
| 5 | let api; |
| 6 | let activeCount = 0; |
| 7 | let activeAborts = new Set(); |
| 8 | |
| 9 | // FIX: removed `historyUpdating` flag. The original used this to distinguish |
| 10 | // between the intercept being aborted by htmx's pushState vs the user clicking |
| 11 | // the stop button, and would re-hijack listenForNavigate() on history updates. |
| 12 | // This was fragile — the abort from navigation.navigate() fires asynchronously |
| 13 | // (as a microtask), so the flag was already reset to false by the time the abort |
| 14 | // handler ran. Instead we now call stopIndicator() pre-emptively in |
| 15 | // htmx_before_history_update before the pushState abort can fire, making the |
| 16 | // flag unnecessary entirely. |
| 17 | |
| 18 | let cleanupNavigation = null; |
| 19 | |
| 20 | function shouldShowIndicator(elt) { |
| 21 | if (api.attributeValue(elt, 'hx-browser-indicator') === 'true') return true; |
| 22 | if (htmx.config.boostBrowserIndicator && elt._htmx?.boosted) return true; |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | function listenForNavigate() { |
| 27 | navigation.addEventListener('navigate', (event) => { |
| 28 | // FIX: added canIntercept guard. Without this, calling event.intercept() |
| 29 | // on a non-interceptable navigation (e.g. cross-origin) throws an error. |
| 30 | if (!event.canIntercept) return; |
| 31 | |
| 32 | // FIX: capture history.state before the intercept. navigation.navigate() |
| 33 | // with {history:'replace'} wipes history.state to null on the replaced |
| 34 | // entry. This meant that when the user pressed Back, popstate fired with |
| 35 | // event.state === null, htmx's check `event.state.htmx` failed, and |
| 36 | // __restoreHistory() was never called — leaving stale page content. |
| 37 | // We save it here so we can restore it in cleanupNavigation. |
| 38 | let savedState = history.state; |
| 39 | |
| 40 | let hideBrowserIndicator; |
| 41 | event.intercept({ |
| 42 | handler: () => new Promise(r => { hideBrowserIndicator = r }), |
| 43 | scroll: 'manual', |
| 44 | focusReset: 'manual' |
| 45 | }); |
| 46 | |
| 47 | let abortHandler = () => { |
| 48 | // FIX: simplified abort handler — no more historyUpdating branch. |
| 49 | // The only time we reach here now is if the user clicks the browser |
| 50 | // stop button, because htmx_before_history_update calls stopIndicator() |
| 51 | // (which nulls cleanupNavigation and resolves the indicator) before |
| 52 | // htmx's pushState fires its own navigate event that would abort this |
| 53 | // signal. So if we get here with activeCount > 0, it's a genuine stop. |
| 54 | if (activeCount > 0) { |
| 55 | activeAborts.forEach(abort => abort()); |
| 56 | activeAborts.clear(); |
| 57 | activeCount = 0; |
| 58 | } |
| 59 | // FIX: always null cleanupNavigation on abort regardless of activeCount, |
| 60 | // so stopIndicator() doesn't try to call an already-aborted handler. |
| 61 | cleanupNavigation = null; |
| 62 | }; |
| 63 | event.signal.addEventListener('abort', abortHandler); |
| 64 | |
| 65 | cleanupNavigation = () => { |
| 66 | hideBrowserIndicator(); |
| 67 | // FIX: restore the saved history.state after resolving the intercept. |
| 68 | // We do this AFTER calling hideBrowserIndicator() because calling |
| 69 | // history.replaceState() while the Navigation API intercept is still |
| 70 | // pending immediately aborts the intercept signal — killing the browser |
| 71 | // loading indicator prematurely. By resolving first then restoring, |
| 72 | // the indicator completes its full lifecycle before we touch history. |
| 73 | history.replaceState(savedState, ''); |
| 74 | }; |
| 75 | }, {once: true}); |
| 76 | } |
| 77 | |
| 78 | function startIndicator() { |
| 79 | listenForNavigate(); |
| 80 | navigation.navigate(location.href, { history: 'replace' }); |
| 81 | } |
| 82 | |
| 83 | function stopIndicator() { |
| 84 | if (cleanupNavigation) { |
| 85 | cleanupNavigation(); |
| 86 | cleanupNavigation = null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | htmx.registerExtension('browser-indicator', { |
| 91 | init: (internalAPI) => { |
| 92 | api = internalAPI; |
| 93 | }, |
| 94 | |
| 95 | htmx_before_history_update: () => { |
| 96 | // FIX: replaced historyUpdating=true with a direct stopIndicator() call. |
| 97 | // htmx is about to call history.pushState() for the new URL. That pushState |
| 98 | // fires a navigate event of type 'push' which aborts our current replace |
| 99 | // intercept's signal. If we let that abort fire naturally, the abort handler |
| 100 | // would see activeCount > 0 and incorrectly treat it as a stop-button press, |
| 101 | // aborting all in-flight requests. By calling stopIndicator() here first we: |
| 102 | // 1. Resolve hideBrowserIndicator() — stops the browser spinner cleanly |
| 103 | // 2. Restore history.state on the current entry BEFORE htmx pushes the |
| 104 | // new entry, so the current entry retains {htmx:true} and Back works |
| 105 | // 3. Null cleanupNavigation so the subsequent abort is a no-op |
| 106 | stopIndicator(); |
| 107 | }, |
| 108 | |
| 109 | // FIX: removed htmx_after_history_update entirely — it only reset historyUpdating |
| 110 | // to false which is no longer needed. |
| 111 | |
| 112 | htmx_before_request: (elt, detail) => { |
| 113 | if (!shouldShowIndicator(elt)) return; |
| 114 | detail.ctx._browserIndicator = true; |
| 115 | activeCount++; |
| 116 | if (activeCount === 1) startIndicator(); |
| 117 | // FIX: moved activeAborts.add to AFTER startIndicator(). Previously it was |
| 118 | // before startIndicator(), which meant when navigation.navigate() fired its |
| 119 | // navigate event synchronously and then aborted the previous intercept, the |
| 120 | // abort handler would find the new request's abort already in activeAborts |
| 121 | // and call it immediately — cancelling the request before it even started. |
| 122 | // Adding it after startIndicator() means activeAborts is empty when the |
| 123 | // abort fires during navigation.navigate(), so nothing gets incorrectly cancelled. |
| 124 | if (detail.ctx.request?.abort) activeAborts.add(detail.ctx.request.abort); |
| 125 | }, |
| 126 | |
| 127 | htmx_finally_request: (elt, detail) => { |
| 128 | if (!detail.ctx._browserIndicator) return; |
| 129 | if (detail.ctx.request?.abort) activeAborts.delete(detail.ctx.request.abort); |
| 130 | if (activeCount === 0) return; |
| 131 | activeCount--; |
| 132 | if (activeCount === 0) stopIndicator(); |
| 133 | } |
| 134 | }); |
| 135 | })(); |