Skip to content

Commit 471bad9

Browse files
edvineriksoneli-darkly
authored andcommitted
Changes back to async flushes after the unload handler has flushed its data (#199)
1 parent 39622a3 commit 471bad9

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/LDClient-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ describe('LDClient', () => {
5757
});
5858
});
5959

60+
describe('identify', () => {
61+
describe('Disables synchronous XHR if page did not unload', () => {
62+
async function setupClient() {
63+
const config = { bootstrap: {}, flushInterval: 100000, fetchGoals: false, sendEvents: false };
64+
const client = LDClient.initialize(envName, user, config);
65+
await client.waitForInitialization();
66+
return client;
67+
}
68+
function testWithUserAgent(desc, ua) {
69+
it('in ' + desc, async () => {
70+
window.navigator.__defineGetter__('userAgent', () => ua);
71+
72+
const client = await setupClient();
73+
expect(server.requests.length).toEqual(0);
74+
window.dispatchEvent(new window.Event('beforeunload'));
75+
await client.identify({ key: 'new-user' });
76+
expect(server.requests.length).toEqual(1);
77+
expect(server.requests[0].async).toBe(true);
78+
});
79+
}
80+
81+
testWithUserAgent(
82+
'Chrome 72',
83+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
84+
);
85+
86+
testWithUserAgent('unknown browser', 'Special Kitty Cat Browser');
87+
88+
testWithUserAgent('empty user-agent', null);
89+
});
90+
});
91+
6092
describe('goals', () => {
6193
it('fetches goals if fetchGoals is unspecified', async () => {
6294
const client = LDClient.initialize(envName, user, { sendEvents: false });
@@ -174,6 +206,32 @@ describe('LDClient', () => {
174206
testWithUserAgent('empty user-agent', null);
175207
});
176208

209+
describe('Disables synchronous XHR if page did not unload', () => {
210+
function testWithUserAgent(desc, ua) {
211+
it('in ' + desc, async () => {
212+
window.navigator.__defineGetter__('userAgent', () => ua);
213+
214+
const client = await setupClientAndTriggerUnload();
215+
expect(server.requests.length).toEqual(2);
216+
// ignore first request because it's just a side effect of calling browserPlatform.httpAllowsPost()
217+
expect(server.requests[1].async).toBe(false); // events
218+
client.track('Test'); // lets track a event that happen after a beforeunload event.
219+
client.flush().catch(() => {}); // flush that event
220+
expect(server.requests.length).toEqual(3); // assert the server got the request.
221+
expect(server.requests[2].async).toBe(true);
222+
});
223+
}
224+
225+
testWithUserAgent(
226+
'Chrome 72',
227+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
228+
);
229+
230+
testWithUserAgent('unknown browser', 'Special Kitty Cat Browser');
231+
232+
testWithUserAgent('empty user-agent', null);
233+
});
234+
177235
describe('discards events during page unload', () => {
178236
function testWithUserAgent(desc, ua) {
179237
it('in ' + desc, async () => {

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function initialize(env, user, options = {}) {
4949
const syncFlushHandler = () => {
5050
platform.synchronousFlush = true;
5151
client.flush().catch(() => {});
52+
platform.synchronousFlush = false;
5253
};
5354
window.addEventListener('beforeunload', syncFlushHandler);
5455
window.addEventListener('unload', syncFlushHandler);

0 commit comments

Comments
 (0)