The boost-attrs extension for htmx 2 allows using boost functionality without the problems related to inheritance that come with using other htmx attributes alongside hx-boost. This extension adds boost-specific replacements for hx-swap, hx-target and hx-select.
With stock hx-boost, if you use hx-swap, hx-target or hx-select for your boosts, all normal non-boosted elements will need to undo these attributes by overriding or unsetting them each time. This means that adding hx-boost to an htmx project can easily break existing functionality. The alternative to this is having to specify these attributes for every boosted element, which mostly defeats the point.
Without the extension:
<body hx-boost="true" hx-swap="outerHTML transition:true" hx-target="#main" hx-select="#main"> ... <div id="main"> ... <div hx-get="/lazy" hx-trigger="load" hx-swap="innerHTML transition:false" hx-target="this" hx-select="unset"></div> ... </div> ... </body>
Existing workaround:
document.addEventListener('htmx:beforeSwap', function(evt) { if (evt.detail.boosted) { evt.detail.swapOverride = 'outerHTML transition:true' // for default: 'innerHTML' evt.detail.selectOverride = '#main' // for default: 'unset' evt.detail.target = document.querySelector('#main') // for default: document.body } })
<body hx-boost="true" hx-ext="boost-attrs" hx-boost-swap="outerHTML transition:true" hx-boost-target="#main" hx-boost-select="#main"> ... </body>
Important
You must enable the extension specifically on the body tag, regardless of where you use its features. If hx-ext="boost-attrs" is specified elsewhere, then the extension is unlikely to work. This limitation only applies to its use of hx-ext and does not apply to the additional attributes it provides, nor hx-boost.
- Attribute —
hx-boost-swap(optional) - Attribute —
hx-boost-target(optional)
When a bad selector is used, the eventhtmx:boostTargetErrorwill fire for boosted elements. This event is designed to behave similarly tohtmx:targetError. - Attribute —
hx-boost-select(optional) - Event —
htmx:beforeBoostSwap(conditional)
This is designed to behave similarly tohtmx:beforeSwapbut only fires for boosted elements. It fires afterhtmx:beforeSwapand allows boost-only overriding of values. This extension can overwrite changes made inhtmx:beforeSwapfor boosted swaps and hence this event is necessary to reliably set overrides for boosts.
Boost attributes take precedence over their non-boost equivalents. A non-boost attribute will still apply to a boosted element only if its boost-specific counterpart hasn't been used. A boost attribute will not apply when its corresponding server response header is used (HX-Reswap, HX-Retarget and HX-Reselect).
The fastest way to install boost-attrs is to load it via a CDN. Remember to always include the core htmx library before the extension and enable the extension. This extension only applies to boost swaps and so hx-boost="true" also needs to be used.
<head> <script src="https://cdn.jsdelivr.net/npm/[email protected]" integrity="sha384-H5SrcfygHmAuTDZphMHqBJLc3FhssKjG7w/CeCpFReSfwBWDTKpkzPP8c+cLsK+V" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/gh/scrhartley/[email protected]/dist/boost-attrs.min.js" integrity="sha384-WpP43L0tEPsUHSFWUjwetq2K9CB3OBZicJGGvd1oCk7pIAEVJo2nstXUgYAzAnNK" crossorigin="anonymous"></script> </head> <body hx-boost="true" hx-ext="boost-attrs"> ...
An unminified version is also available at:
https://cdn.jsdelivr.net/gh/scrhartley/htmx-ext-boost-attrs/dist/boost-attrs.js
For npm-style build systems, you can install boost-attrs via npm:
npm install "scrhartley/htmx-ext-boost-attrs#semver:^0.0.1"If you are using a bundler to manage your javascript:
- Install
htmx.organdscrhartley/htmx-ext-boost-attrsvia npm - Import both packages in your
index.js/main.js:
import 'htmx.org'; import 'htmx-ext-boost-attrs';