Repository navigation
fix(hx-preload): build preloads with hx-headers and htmx:config:request - #4111
Open
mhalikosen wants to merge 1 commit into
Open
mhalikosen wants to merge 1 commit into
mhalikosen wants to merge 1 commit into
Conversation
A preload was built with createRequestContext plus a bare fetch, so it missed the header work a triggered request does: hx-headers never ran, htmx:config:request never fired, and the documented HX-Preloaded header was never sent. The click then reused the preload response even though its own request would have carried different headers. Apply hx-headers and publish ctx.vals, assign form, submitter and body to ctx.request, and fire htmx:config:request (cancellable), as __handleTriggerEvent does. Set HX-Preloaded before the event, so a listener can tell the preload from the click that fires the event again, and HX-Request-Type after it, as core does. Fold the body into the query string after the event, so a listener's changes reach the URL, and clear it before fetching, since browsers reject a body on GET. hx-prompt now cancels a preload instead of answering it. Before, the click prompted and then reused a preload sent without HX-Prompt, so the answer never reached the server; with htmx:config:request firing for preloads, the element would also prompt twice. Refs bigskysoftware#4110
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A preload is built with
createRequestContextand a barefetch, so it skips the header work a triggered request does:hx-headersnever runs,htmx:config:requestnever fires, and theHX-Preloaded: trueheader the docs promise is never sent. The click then reuses that response, although its own request would have carried different headers.The preload listener now follows
__handleTriggerEvent:hx-valsreceives{ctx}and publishesctx.vals,hx-headersapplies, andform,submitterandbodyare assigned toctx.request.HX-Preloadedis set, thenhtmx:config:requestfires; cancelling it drops the preload. The click fires the event again, and the header lets a listener tell the two apart.HX-Request-Typeis computed after the event, as core does.fetchrejects a body on GET.hx-promptnow cancels a preload. Onfour-devthe click prompts, then reuses a preload sent withoutHX-Prompt, so the answer never reaches the server; once preloads firehtmx:config:request, the element would also prompt twice.The hx-preload and hx-prompt pages document both.
Corresponding issue: #4110
Testing
Five new tests, each failing on
four-dev(372c6e3) and passing here:hx-headersandHX-Preloadedreach the preload; header and body changes made inhtmx:config:requestreach the preload fetch, which carries no body; a listener cancels only the preload and the click is sent withoutHX-Preloaded; the click reuses a preload that carrieshx-headers.HX-Prompt.npm test: 1785 passed, 0 failed. In Chrome 154, a boosted link underhx-headers:inheritedandhx-config:inherited, with anhtmx:config:requestlistener, sent one request, the preload, carrying all three headers andHX-Preloaded: true, and the click swapped it in. A preloaded button withhx-promptprompted once and sent one request with the answer.Checklist
masterfor website changes,devforsource changes)
approved via an issue
npm run test) and verified that it succeeded