htmx
htmx copied to clipboard
Trigger refresh/redirect after SSE message
I'm trying to convert my current basic javascript websockets implementation to HTMX's SSE. Is there a way to trigger a redirect or page refresh after SSE messages? There's the occasional event for which the standard swapping of content would not work for in my use case.
You can probably add a JS event listener for the htmx:sseMessage
event and trigger a page refresh based on that.
Thanks!
For those searching:
<div hx-ext="sse" sse-connect="/events" sse-swap="refresh"
_="on htmx:sseMessage location.replace(location.href)" />
Note: This does result in the body of the refresh
message displaying in the div before the page refreshes.
I have another problem with the redirect.
After a JWT token is expired a server is sending the 307 Redirect
response. It turned out, that an sse-handler can't interpret normal responses and keep trying to reestablish connection while receieving 307
again and again.
It would be great to handle the case and trigger redirectin with a location in a header.
I have another problem with the redirect.
After a JWT token is expired a server is sending the
307 Redirect
response. It turned out, that an sse-handler can't interpret normal responses and keep trying to reestablish connection while receieving307
again and again.It would be great to handle the case and trigger redirectin with a location in a header.
Did you work out a way to resolve this? I was wondering whether to try and send through a HX-REDIRECT header in my response too. I'm getting the same issue when JWT expires.