javascripthttp-redirecthtmx

How to implement a redirect with HTMX?


HTMX is great and using AJAX for most of the stuff is awesome! But from time to time I just need a real "phyiscal" redirect to another page.

Any ideas how to achieve this without starting to write custom JS code?

Thx!


Solution

  • Luckily, after googleing for ages I found this post at Reddit: https://www.reddit.com/r/htmx/comments/ot6kai/comment/h6v5cn9/?utm_source=share&utm_medium=web2x&context=3

    You can easily set the HX-Redirect header in your backend and on the return of the response, your browser will magically redirect.

    The docs state this here but unfortunately don't explain it in more detail on other pages.

    Supplemantal: I experienced an issue with JS errors occuring after the redirect - even though everything works fine.