Skip to content

Commit 74db5f8

Browse files
committed
Add changeset and fix format
1 parent 3576ef8 commit 74db5f8

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

.changeset/nervous-needles-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/performance': patch
3+
---
4+
5+
Fix bug where events are not sent if they exceed sendBeacon payload limit

packages/performance/src/services/transport_service.test.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import sinonChai from 'sinon-chai';
2121
import {
2222
transportHandler,
2323
setupTransportService,
24-
resetTransportService, flushQueuedEvents
24+
resetTransportService,
25+
flushQueuedEvents
2526
} from './transport_service';
2627
import { SettingsService } from './settings_service';
2728

@@ -167,13 +168,10 @@ describe('Firebase Performance > transport_service', () => {
167168
'event_time_ms': '1'
168169
});
169170
}
170-
expect(fetchStub).calledWith(
171-
flTransportFullUrl,
172-
{
173-
method: 'POST',
174-
body: JSON.stringify(firstLogRequest),
175-
}
176-
);
171+
expect(fetchStub).calledWith(flTransportFullUrl, {
172+
method: 'POST',
173+
body: JSON.stringify(firstLogRequest)
174+
});
177175
// Expects the second logRequest which contains remaining 20 events;
178176
const secondLogRequest = generateLogRequest('15501');
179177
for (let i = 0; i < 20; i++) {
@@ -269,7 +267,6 @@ describe('Firebase Performance > transport_service', () => {
269267
sendBeaconStub.onCall(1).returns(false);
270268
flushQueuedEvents();
271269

272-
273270
// Assert
274271
const firstLogRequest = generateLogRequest('1');
275272
const secondLogRequest = generateLogRequest('1');
@@ -289,13 +286,10 @@ describe('Firebase Performance > transport_service', () => {
289286
flTransportFullUrl,
290287
JSON.stringify(firstLogRequest)
291288
);
292-
expect(fetchStub).calledWith(
293-
flTransportFullUrl,
294-
{
295-
method: 'POST',
296-
body: JSON.stringify(secondLogRequest),
297-
}
298-
);
289+
expect(fetchStub).calledWith(flTransportFullUrl, {
290+
method: 'POST',
291+
body: JSON.stringify(secondLogRequest)
292+
});
299293
});
300294

301295
function generateLogRequest(requestTimeMs: string): any {

packages/performance/src/services/transport_service.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,16 @@ function postToFlEndpoint(body: string): Promise<void | Response> {
142142
SettingsService.getInstance().getFlTransportFullUrl();
143143
const size = TEXT_ENCODER.encode(body).length;
144144

145-
if (size <= MAX_SEND_BEACON_PAYLOAD_SIZE && navigator.sendBeacon &&
146-
navigator.sendBeacon(flTransportFullUrl, body)) {
145+
if (
146+
size <= MAX_SEND_BEACON_PAYLOAD_SIZE &&
147+
navigator.sendBeacon &&
148+
navigator.sendBeacon(flTransportFullUrl, body)
149+
) {
147150
return Promise.resolve();
148151
} else {
149152
return fetch(flTransportFullUrl, {
150153
method: 'POST',
151-
body,
154+
body
152155
});
153156
}
154157
}
@@ -170,15 +173,15 @@ export function transportHandler(
170173
const message = serializer(...args);
171174
addToQueue({
172175
message,
173-
eventTime: Date.now(),
176+
eventTime: Date.now()
174177
});
175178
};
176179
}
177180

178181
/**
179182
* Force flush the queued events. Useful at page unload time to ensure all events are uploaded.
180183
* Flush will attempt to use sendBeacon to send events async and defaults back to fetch as soon as a
181-
* sendBeacon fails. Firefox
184+
* sendBeacon fails. Firefox
182185
*/
183186
export function flushQueuedEvents(): void {
184187
const flTransportFullUrl =
@@ -189,7 +192,10 @@ export function flushQueuedEvents(): void {
189192
const staged = queue.splice(-MAX_FLUSH_SIZE);
190193
const body = buildPayload(staged);
191194

192-
if (navigator.sendBeacon && navigator.sendBeacon(flTransportFullUrl, body)) {
195+
if (
196+
navigator.sendBeacon &&
197+
navigator.sendBeacon(flTransportFullUrl, body)
198+
) {
193199
continue;
194200
} else {
195201
queue = [...queue, ...staged];
@@ -200,7 +206,7 @@ export function flushQueuedEvents(): void {
200206
const body = buildPayload(queue);
201207
fetch(flTransportFullUrl, {
202208
method: 'POST',
203-
body,
209+
body
204210
}).catch(() => {
205211
consoleLogger.info(`Failed flushing queued events.`);
206212
});

0 commit comments

Comments
 (0)