Skip to content

Commit 161831a

Browse files
committed
Send session heartbeats and ends with keepalive
A session end submitted on pagehide is a GET without a body, so it was cancelled with the page.
1 parent 76dde1a commit 161831a

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎packages/core/src/submission/DefaultSubmissionClient.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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> {

‎packages/core/test/submission/DefaultSubmissionClient.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ describe("DefaultSubmissionClient", () => {
131131
expect(lastRequest().init.keepalive).toBe(true);
132132
});
133133

134+
test("sends session heartbeats with keepalive so a session end on page hide is delivered", async () => {
135+
respond(200);
136+
const client = new DefaultSubmissionClient(config, fetchMock);
137+
138+
await client.submitHeartbeat("session-id", true);
139+
140+
expect(lastRequest().init.keepalive).toBe(true);
141+
});
142+
134143
test("sends large submissions without keepalive", async () => {
135144
const client = new DefaultSubmissionClient(config, fetchMock);
136145

0 commit comments

Comments
 (0)