bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
37.99k stars 1.29k forks source link

hx-get with hx-target causes Duplicate Tab in browser to only load last fetched content when no cache headers are emitted by backend #705

Closed fearofcode closed 2 years ago

fearofcode commented 2 years ago

I have the beginning of a small app with a few links. They have hx-get annotations to insert their responses into a content section like:

<ul>
<li><a hx-get="/register" hx-target=".content">Register</a></li>
<li><a hx-get="/login" hx-target=".content">Log in</a></li>
</ul>

...

<section class="content">
</section>

My backend detects HTMX requests by looking for the HX-Request header and, when it's detected, only returns the content that needs to be swapped out. If it's not an HTMX request, however, it returns a full template. That way, going to /register loads the whole page fine, and clicking the /register link swaps out the content with the backend response as intended. If the header is present, the backend adds a HX-Push response header with the correct respective URL to push to history.

The problem is that if I do the following:

Then only the part that is being swapped out in the content section is being loaded. I.e., you only get a template fragment saying <h2>Hello from the register.html template</h2> rather than the full registration page the way I was expecting.

I can provide a repo URL if would help. Other operations, like refreshing, back, forward, etc. work.

I'm probably doing something wrong since I'm new to HTMX so sorry if that's the case.

fearofcode commented 2 years ago

I tried this with both latest Firefox and latest Chrome on Windows 10.

fearofcode commented 2 years ago

If I add headers to the backend to not cache the response, this stops happening.

Is it expected practice not to cache server-side responses in order to stop such issues from occurring?

fearofcode commented 2 years ago

This looks like it's a duplicate of #421. Closing so I don't waste anyone's time.

fearofcode commented 2 years ago

So, I think the main options are:

I guess I didn't understand that Duplicate Tab duplicates the current tab from cache where possible and that a cached HTMX response of a URL /foo would cause duplicating the tab when at the URL /foo to just load the HTMX fragment rather than the whole page. I feel kind of silly now.