htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Headers defined with hx-headers not sent with preload

Open partok opened this issue 4 years ago • 9 comments

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.

partok avatar Aug 29 '21 17:08 partok

This is with the preload extension?

@benpate what do you think, bug or feature? Seems like a rabbit hole...

1cg avatar Sep 06 '21 14:09 1cg

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?

benpate avatar Sep 06 '21 15:09 benpate

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.

partok avatar Sep 06 '21 21:09 partok

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.

benpate avatar Sep 07 '21 21:09 benpate

Seems like the preload requests should include the correct headers so caching works correctly. @benpate please lmk if you want to chat...

1cg avatar Nov 02 '21 22:11 1cg

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.

benpate avatar Nov 02 '21 22:11 benpate

https://github.com/bigskysoftware/htmx/blob/e67e5ba41b4de7286adf3b6a6bceb1c0ae94c88b/src/htmx.js#L2071

yeesh :expressionless:

1cg avatar Nov 02 '21 22:11 1cg

Git blame is my new best friend. It lets me take literally no responsibility for this function 😉

benpate avatar Nov 03 '21 17:11 benpate

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.

checketts avatar Jun 15 '23 03:06 checketts