fasthtml
fasthtml copied to clipboard
[BUG] HX-Trigger-Name Request Header Stripped from Websocket Requests
I'm working with websockets in FastHTML and noticed I do not have access to the name of the element that initiated the message within my route handler function as I would expect. In HTMX the Hx-Trigger and Hx-Trigger-Name request headers are added for the id and name of the triggering element to the websocket message within the websocket data under the 'HEADERS' key as described here: https://v1.htmx.org/extensions/web-sockets/#sending-messages-to-a-websocket
This appears to be getting stripped out by FastHTML. I have looked to see if these get bumped to the special htmx
argument or elsewhere and it seems the headers are getting stripped.
Minimal reproducible example:
from fasthtml.common import *
app, rt = fast_app(ws_hdr=True)
@rt("/")
def get():
return Div(
P(
"Click This Text to Send WebSocket",
ws_send=True,
id="clickabletextid",
name="clickabletextname",
hx_ext="ws",
ws_connect="/ws",
),
P("Text to Update", id="updatetext"),
)
@app.ws("/ws")
async def ws(ws, app, htmx, data, send):
print(data)
print(htmx)
await send(
Div(
"Text Updated",
id="updatetext",
hx_swap_oob="true",
)
)
serve()
When monitoring the data sent in the websockets in devtools I can see the client is sending the headers:
{"HEADERS":{"HX-Request":"true","HX-Trigger":"clickabletextid","HX-Trigger-Name":"clickabletextname","HX-Target":"clickabletextid","HX-Current-URL":"http://0.0.0.0:5001/"}}
When looking at the htmx headers in my route handler I see:
HtmxHeaders(boosted=None, current_url=None, history_restore_request=None, prompt=None, request=None, target=None, trigger_name=None, trigger=None)
And when looking at my data I receive:
{}
This seems to be unintended behavior to me. I know I can add HX-Vals to my element to include this and it shows up in the data dict but would guess this would be better to align with the HTMX expected behavior...
Thoughts would be appreciated! Thanks!