htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Htmx + alpine problems

Open Shadowfaxenator opened this issue 1 year ago • 8 comments

I was so hyped on alpine+htmx, but the reality is not that good 😦 I have 2 main problems 1. Id attribute cannot be swapped correctly 2. hx-boost works but history saves conflict with alpine and also with id attributes, it’s total mess and I can’t use it for real projects because of these issues

Shadowfaxenator avatar Sep 15 '24 18:09 Shadowfaxenator

Hey, could you provide code examples so we can investigate ? Hard to debug otherwise! In the meantime, you might want to look into the following docs if you haven't already:

Hope this helps!

Telroshan avatar Sep 16 '24 08:09 Telroshan

You mentioned alpine morph, how can I set hx-swap=‘morph’ to work only on histore swaps but not on others?

Shadowfaxenator avatar Sep 16 '24 09:09 Shadowfaxenator

As far as I know, htmx uses the hx-history-elt (that is the body by default) but doesn't allow you to swap with another method than innerHTML: https://github.com/bigskysoftware/htmx/blob/0023cd85f01987dfa0add4101e08d98c7801d88e/src/htmx.js#L3156 https://github.com/bigskysoftware/htmx/blob/0023cd85f01987dfa0add4101e08d98c7801d88e/src/htmx.js#L3179

So I don't think you can change the swap method here for history restores, but you can change the hx-history-elt if that is of any help?

Telroshan avatar Sep 17 '24 08:09 Telroshan

There is a problem now with using alpine. If a component uses ID and settles attributes by default. You must turn off settle to make it work and in this case css animations wont work correctly. There is no official docs to make it work from the box :(

Shadowfaxenator avatar Sep 17 '24 08:09 Shadowfaxenator

Never used alpine so I won't be able to help that much more I'm afraid! Except the 2 links above I don't think we have any other docs about alpine indeed. Looking at issues, seems #1081 also requested history swap method customization for alpine. Anyway, sounds reasonable to me to expose this so users can use another swap method for history restores if they want to (after all, we let them customize the swap method for everything else). If you're interested in diving into this topic and opening PRs to address those issues (exposing the history swap method, and/or adding documentation to work with alpine), please feel free to do so!

Telroshan avatar Sep 17 '24 08:09 Telroshan

One option to handle history issues with alpine may be to use the history config items like historyCacheSize=0 and refreshOnHistoryMiss=true So that it will not use htmx to store and restore history. It will then do full page loads when using back button and while it will be a little slower it will then load all alpine enabled items fresh each time

MichaelWest22 avatar Sep 18 '24 06:09 MichaelWest22

When I've had an issue with boost I've got Alpine to listen to HTMX events, e.g.

x-on:htmx:before-history-save.window="console.log('cleanup before change')"

scrhartley avatar Sep 21 '24 20:09 scrhartley

I wrote an extension to help with history restores that will prevent Alpine errors on back/forward navigation:

https://gist.github.com/croxton/e2c33bd22591f9a5bd8c9d23a56c9edc

It also helps to use inline directives over runtime declarations, for example use x‑data over Alpine.data.

croxton avatar Sep 24 '24 06:09 croxton