@@ -12,6 +12,15 @@ declare const htmx: typeof htmxType;
1212( function ( ) {
1313 let api : HtmxApi ;
1414
15+ // Helper function to detect chunked transfer (HTTP/1.1) or streaming (HTTP/2)
16+ function isChunkedTransfer ( xhr : XMLHttpRequest ) : boolean {
17+ const te = xhr . getResponseHeader ( "Transfer-Encoding" ) ;
18+ const cl = xhr . getResponseHeader ( "Content-Length" ) ;
19+ const isHttp1Chunked = te === "chunked" ;
20+ const isStreamingWithoutLength = ! te && ! cl ; // typical HTTP/2 streaming
21+ return isHttp1Chunked || isStreamingWithoutLength ;
22+ }
23+
1524 htmx . defineExtension ( "chunked-transfer" , {
1625 init : function ( apiRef : HtmxApi ) {
1726 api = apiRef ;
@@ -22,14 +31,15 @@ declare const htmx: typeof htmxType;
2231
2332 if ( name === "htmx:beforeRequest" ) {
2433 const xhr = evt . detail . xhr as XMLHttpRequest ;
25- ( xhr as any ) . _chunkedMode = elt . getAttribute ( "hx-chunked-mode" ) || "append" ;
34+ ( xhr as any ) . _chunkedMode =
35+ elt . getAttribute ( "hx-chunked-mode" ) || "append" ;
2636 ( xhr as any ) . _chunkedLastLen = 0 ;
2737
2838 xhr . onprogress = function ( ) {
29- const is_chunked =
30- xhr . getResponseHeader ( "Transfer-Encoding" ) === "chunked" ;
39+ if ( ! isChunkedTransfer ( xhr ) ) return ;
3140
32- if ( ! is_chunked ) return ;
41+ const swapSpec = api . getSwapSpecification ( elt ) ;
42+ if ( swapSpec . swapStyle !== "innerHTML" ) return ;
3343
3444 const mode = ( xhr as any ) . _chunkedMode || "append" ;
3545 const full = ( xhr . response as string ) ?? "" ;
@@ -54,7 +64,6 @@ declare const htmx: typeof htmxType;
5464 response = extension . transformResponse ( response , xhr , elt ) ;
5565 } ) ;
5666
57- const swapSpec = api . getSwapSpecification ( elt ) ;
5867 const settleInfo = api . makeSettleInfo ( elt ) ;
5968
6069 if ( api . swap ) {
@@ -82,9 +91,7 @@ declare const htmx: typeof htmxType;
8291 const mode = ( xhr as any ) . _chunkedMode ;
8392 if ( mode !== "swap" ) return ;
8493
85- const is_chunked =
86- xhr . getResponseHeader ( "Transfer-Encoding" ) === "chunked" ;
87- if ( ! is_chunked ) return ;
94+ if ( ! isChunkedTransfer ( xhr ) ) return ;
8895
8996 detail . shouldSwap = false ;
9097 }
0 commit comments