How would I write an HTMX extension for "ReadableStream" or "Transfer-Encoding: Chunked"? #2437
Unanswered
ancientstraits
asked this question in
Q&A
Replies: 2 comments
-
For the response, you need to set the transfer encoding to chunked. This will cause xhr.progress to be triggered when the data is flushed from the server. res.setHeader('transfer-encoding', 'chunked'); Note that HTMX doesn't currently support the chunked encoding. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Look at SSE extension, it could help. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For my website, the user has to perform a process that takes a long time, and thus requires some status updates. I just wanted to have the HTTP server to send status data to the client over a period of time, by using a single HTTP request, and then let the request finish once the process was done. I was able to do this using the js
fetch
API, but not using theXMLHttpRequest
API, which is bad, because HTMX requires theXMLHttpRequest
API to make HTTP requests.For instance, this could be what the server does (if it runs on Express):
So when the client does
POST /thing
, the server should instantly writehi\n
, then writewhat\n
after 1 second, then writebye\n
and end the request after 2 seconds. When I docurl http://localhost:8080
, it works just as expected.When I use this JS code that uses
fetch
on the browser, it also works:However, no matter how much I try, the
XMLHttpRequest
API cannot do the same:When I do this, none of the event listeners of
xhr
are triggered 1 second after the request has been send (which is when the server sendswhat\n
. And no matter how hard I try, it stays that way. Some of the handlers are triggered when the request is started, and some are triggered when the request is finished, but none are triggered when the server sends intermediate data, likewhat\n
.This is not good, because I want to use HTMX in my website to make everything run simpler, and to write an extension, I have to use the
XMLHttpRequest
API instead of thefetch
API, meaning I cannot handle incremental HTTP responses.I wonder if there is a way to change the code on the server to make this work, or if there is something about the
XMLHttpRequest
API that I am missing. Is there any way to make this work?Beta Was this translation helpful? Give feedback.
All reactions