@@ -83,7 +83,7 @@ export class DefaultSubmissionClient implements ISubmissionClient {
8383
8484 // keepalive lets a submission started while the page is being hidden or unloaded finish after the page is gone.
8585 const keepAliveBytes = this . getKeepAliveBytes ( options . body ) ;
86- if ( keepAliveBytes > 0 ) {
86+ if ( keepAliveBytes !== undefined ) {
8787 requestOptions . keepalive = true ;
8888 this . _keepAliveBytesInFlight += keepAliveBytes ;
8989 }
@@ -112,7 +112,7 @@ export class DefaultSubmissionClient implements ISubmissionClient {
112112 throw ex ;
113113 } finally {
114114 clearTimeout ( timeoutId ) ;
115- this . _keepAliveBytesInFlight -= keepAliveBytes ;
115+ this . _keepAliveBytesInFlight -= keepAliveBytes ?? 0 ;
116116 }
117117 }
118118
@@ -130,8 +130,10 @@ export class DefaultSubmissionClient implements ISubmissionClient {
130130 }
131131 }
132132
133- private getKeepAliveBytes ( body : string | undefined ) : number {
133+ /** Returns the body size to reserve from the keepalive budget, or undefined when the request can't use keepalive. */
134+ private getKeepAliveBytes ( body : string | undefined ) : number | undefined {
134135 if ( ! body ) {
136+ // Requests without a body (session heartbeats and ends) don't count against the budget.
135137 return 0 ;
136138 }
137139
@@ -140,13 +142,13 @@ export class DefaultSubmissionClient implements ISubmissionClient {
140142 let bytes = maxBytes ;
141143 if ( maxBytes > this . KeepAliveBodyLimit ) {
142144 if ( typeof TextEncoder !== "function" || body . length > this . KeepAliveBodyLimit ) {
143- return 0 ;
145+ return undefined ;
144146 }
145147
146148 bytes = new TextEncoder ( ) . encode ( body ) . length ;
147149 }
148150
149- return this . _keepAliveBytesInFlight + bytes <= this . KeepAliveBodyLimit ? bytes : 0 ;
151+ return this . _keepAliveBytesInFlight + bytes <= this . KeepAliveBodyLimit ? bytes : undefined ;
150152 }
151153
152154 private isHeaders ( headers : HeadersInit | undefined ) : headers is Record < string , string > {
0 commit comments