Document fragment/full response render logic#3311
Conversation
|
#3278 has already merged into dev branch and this will likely change how we do this documentation when the next release ships. I would suggest re-targeting these changes to dev and re-writing them to account for the new config item |
|
The new config doesn't matter. The logic I documented will work either way whether the config is switched on or off. |
|
I usually just go with |
|
It might be expensive to render a full page in some cases. Maybe you're loading a bunch of stuff from some database or third-party system. |
Yeah there is already a section just above with this text talking about hx-select as an alternative instead of HTML fragments. So maybe we could just reuse some the terminology at the start of your new documentation like: Just adding the word |
And cross-link with HTTP caching section which mentions the `Vary` header.
|
I added the word 'normally' and linked to the immediate above section which talks about Also cross-linked against the section that talks about HTTP caching. |
And warn that it won't work for hx-select.
And go back to using only HX-Request and HX-History-Restore-Request headers for the render logic.
|
One interesting idea I had to resolve this properly is to add a new htmx response header. dev...MichaelWest22:htmx:new-header if (target !== getDocument().body) {
const selectOOB = getClosestAttributeValue(elt, 'hx-select-oob')
const select = getClosestAttributeValue(elt, 'hx-select')
if (select) {
headers['HX-Response-Type'] = 'hx-select:' + select + (selectOOB ? ',' + selectOOB : '')
} else {
headers['HX-Response-Type'] = 'partial'
}
}Here is a quick example of what could be added as another response header to fill in the gap we have in understanding the response we need. This Another cool feature is that users of hx-select could start out using hx-select for things while returning all simple full page responses and then over time update the backend routes to return partial responses based on the id selectors returned from this header to optimize performance without having to rework the templates to move from select pattern to normal partials. It also gives htmx a new declarative descriptive power it didn't have before where you can have one full page template route url and multiple active htmx elements on the page with different hx-select tags defined can request from this one url different partial responses. Anyway just an idea I had and it could easily be made into a JS htmx:configRequest event listener or a htmx extension but it would obviously work better if it was already there standard. |
|
Why not use the existing |
The job of |
|
But that's not even the case any more. There's already a new config added that allows you to disable the And I've already asked this elsewhere but what's even the use of being able to tell only that a request is from htmx or not? Who cares? What we will do with that information? Nothing 🤷 What we really care about is whether we should render a fragment or a full page and |
I can imagine a backend which looks at |
|
My backend looks at |
|
@scrhartley then what's stopping it from directly looking for the headers it actually wants, instead of going through |
|
@yawaramin I'm just saying that the ship and sailed and we don't know what people have done and how optimal it might be. A change of behavior runs a risk of breaking something. I'm not thinking about some theoretical optimal system. Whatever the original intent was, how people have actually used it is the compatibility legacy. |
|
That's why the PR I linked to flags the behaviour change behind a new config. Since it's already there, we might as well use it to mean 'HX-Request header is for fragments only, otherwise full pages'? |
|
Anyway, this is all hypothetical to me. I don't believe in the general case that it's possible to have a header or combination of headers that allows us to determine with 100% certainty whether a fragment or a full page should be rendered, because of |
|
It doesn't seem necessary to have a dependency on the value of that config flag, it just seems an unneeded complication. |
|
@MichaelWest22 Would it be useful to use a different joining character for select and selectOOB so that can know which ones are for which? They both can query for multiple elements with comma used to separate them. |
|
I think it's time to move this discussion to a new issue. I'd like the discussion in this PR to reflect the actual behaviour as it stands today. |
|
Yeah sorry for hijacking your PR! moved to a new issue #3319 |
|
Generally speaking, the htmx docs need to document htmx behavior. We have small examples that include references to server code—generally, in the examples section—but this sort of explainer is really out of scope, especially for the docs page. If you wanted to add a small note to the |
Description
Document the exact logic needed by backend servers to decide whether to render an HTML fragment or a full HTML document.
Checklist
masterfor website changes,devforsource changes)
approved via an issue
npm run test) and verified that it succeeded