htmx
htmx copied to clipboard
Headers defined with hx-headers not sent with preload
First of all, thank you for HTMX, this is a really great project.
It seems that headers added with hx-headers are not sent with the preload request. But I don't know if it's a bug or a feature.
This is with the preload extension?
@benpate what do you think, bug or feature? Seems like a rabbit hole...
First of all, yes. htmx is a really great project. Thank you @1cg.
This may be solvable. The preload extension tries to mimic what will eventually happen when the link is actually triggered. So, if we're looking at a tag with an hx-get, preload just calls htmx.ajax() to issue the request. And if it's a regular <a href=""> tag, preload generates its own XMLHttpRequest so that no special htmx headers are sent. This is so that we don't break the pattern of a server sniffing the hx-request header to return either a full page or a partial HTML fragment.
So, if you're not seeing htmx headers added to an <a href=""> tag, that's by design. We want to cache the content that will actually be loaded once the user clicks the link.
If you're not seeing the right headers when you click something like <a hx-get="">, then it's probably something extra that we'll need to account for when we call htmx.ajax()
Can you help us dig deeper into this?
I'm using the preload extension and hx-get, not href.
When the preload extension calls htmx.ajax, the provided context only has a handler but no headers, values, source, target or event.
Cool. That makes sense. So, we need to figure out how to pass in the context, which it's not doing currently.
@1cg, is there any voodoo here that I need to know about? I think a lot of the AJAX underpinnings have shifted. Currently, preload is making a "fake" context that includes its own callback handler. I think I can take a shot at making this update, but I'm just looking for your guidance before I break anything.
Seems like the preload requests should include the correct headers so caching works correctly. @benpate please lmk if you want to chat...
Yes, we probably do need to chat. To keep the discussion here (at least for now) is there some way that the extension can get (or recreate) the correct context value without using the htmx:beforeRequest event? What goes into it? I'm guessing we might be able to just duplicate this work in the extension for now, and possibly expose it as a function in the new "internal api" value that extensions will hopefully have access to in 1.7.
https://github.com/bigskysoftware/htmx/blob/e67e5ba41b4de7286adf3b6a6bceb1c0ae94c88b/src/htmx.js#L2071
yeesh :expressionless:
Git blame is my new best friend. It lets me take literally no responsibility for this function 😉
Fixed via #1493 . The element wasn't being included in the htmx.ajax call so the introspection for hx-headers was only looking on the body for the attribute.