-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
With multiple htmx.ajax requests only the first call is executed #2147
Comments
Im having a similar issue, except that only the second request is executed. |
My solution was to use fetch to retrieve data and then insert the result into the DOM using JavaScript. I stopped using htmx for this problem. |
Hey, you might want to take a look at hx-sync
By default, if a second request is fired on an element that already has one in flight, that new request will be dropped thus cancelled What may not be intuitive in your case here, is that calling htmx.ajax without the third parameter will default using the You could:
|
Having a similar issue but for me the second request is not running.
|
|
I'm having trouble understanding when is the queuedRequest supposed to be cleared in the <!DOCTYPE html>
<html>
<head>
<script src="htmx.js"></script>
</head>
<body hx-sync="this:queue all">
<p id="questionid123">first</p>
<p id="questionid124">second</p>
<p id="questionid125">third</p>
<p id="questionid0">fourth</p>
<script>
setTimeout(() => htmx.ajax('GET', '/question/partial?fieldId=0', { source: "body", target: '#questionid0'}), 2000);
var array= ['123','124','125'];
for(i=0;i<array.length;i++) {
htmx.ajax('GET', '/question/partial?fieldId='+array[i], { source: "body",
target: '#questionid'+array[i] });
}
</script>
</body>
</html> |
This issue arises when I fixed the issue for myself with the following change: /**
* Issues an htmx-style AJAX request
*
* @see https://htmx.org/api/#ajax
*
* @param {HttpVerb} verb
* @param {string} path the URL path to make the AJAX
* @param {Element|string|HtmxAjaxHelperContext} context the element to target (defaults to the **body**) | a selector for the target | a context object that contains any of the following
* @return {Promise<void>} Promise that resolves immediately if no request is sent, or when the request is complete
*/
function ajaxHelper(verb, path, context) {
verb = (/** @type HttpVerb */(verb.toLowerCase()))
if (context) {
if (context instanceof Element || typeof context === 'string') {
- return issueAjaxRequest(verb, path, null, null, {
+ return issueAjaxRequest(verb, path, resolveTarget(context), null, {
targetOverride: resolveTarget(context),
returnPromise: true
})
} else {
return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event,
{
handler: context.handler,
headers: context.headers,
values: context.values,
targetOverride: resolveTarget(context.target),
swapOverride: context.swap,
select: context.select,
returnPromise: true
})
}
} else {
return issueAjaxRequest(verb, path, null, null, {
returnPromise: true
})
}
} I don't understand why it was passing HTH and happy to collab more on this. |
I'll roll with this change for now and if it doesn't cause me any trouble I'll issue a PR (if I don't forget ;)) |
I believe this fixes bigskysoftware#2147
fwiw, the above solution is still working fine for me, but I see that the maintainers don't want PRs without explicitly asking for them, so there's nothing for me to do for now. Their automated tests are also cool with this change. |
Sorry I meant to test this as well but I've not gotten to it yet. I'll be sure to test this on my end as well. But thank you for your work! |
Only for new features @patreeceeo ! Cf the contribution guidelines
You're ofc welcome to submit a bugfix PR. We'll then investigate on whether this is the best fix or not, make sure it doesn't break anything etc., but you can open that FR if you feel like it |
Oh, I totally misread... I'll open that bugfix PR. |
I believe this fixes bigskysoftware#2147
I solved this by adding the htmx
.ajax("GET", "./stuff.html", {
source: "#stuff",
target: "#stuff",
swap: "innerHTML",
}) From what I read, htmx remember what requests are already in flight for each source to avoid requesting the same data multiple times and to cancel in flight requests when the data changed and a new request should be made. If the |
This worked for me Thank you !! |
When calling
htmx.ajax
multiple times only the first call is actually executed.For example when executing the code below. Only my-page-1 is requested.
It seems like the my-page-2 call is cancelled.
Reproduction link is here
The text was updated successfully, but these errors were encountered: