Skip to content

Changes back to async flushes after the unload handler has completed #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions src/__tests__/LDClient-test.js
Original file line number Diff line number Diff line change
@@ -57,6 +57,38 @@ describe('LDClient', () => {
});
});

describe('identify', () => {
describe('Disables synchronous XHR if page did not unload', () => {
async function setupClient() {
const config = { bootstrap: {}, flushInterval: 100000, fetchGoals: false, sendEvents: false };
const client = LDClient.initialize(envName, user, config);
await client.waitForInitialization();
return client;
}
function testWithUserAgent(desc, ua) {
it('in ' + desc, async () => {
window.navigator.__defineGetter__('userAgent', () => ua);

const client = await setupClient();
expect(server.requests.length).toEqual(0);
window.dispatchEvent(new window.Event('beforeunload'));
await client.identify({ key: 'new-user' });
expect(server.requests.length).toEqual(1);
expect(server.requests[0].async).toBe(true);
});
}

testWithUserAgent(
'Chrome 72',
'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'
);

testWithUserAgent('unknown browser', 'Special Kitty Cat Browser');

testWithUserAgent('empty user-agent', null);
});
});

describe('goals', () => {
it('fetches goals if fetchGoals is unspecified', async () => {
const client = LDClient.initialize(envName, user, { sendEvents: false });
@@ -174,6 +206,32 @@ describe('LDClient', () => {
testWithUserAgent('empty user-agent', null);
});

describe('Disables synchronous XHR if page did not unload', () => {
function testWithUserAgent(desc, ua) {
it('in ' + desc, async () => {
window.navigator.__defineGetter__('userAgent', () => ua);

const client = await setupClientAndTriggerUnload();
expect(server.requests.length).toEqual(2);
// ignore first request because it's just a side effect of calling browserPlatform.httpAllowsPost()
expect(server.requests[1].async).toBe(false); // events
client.track('Test'); // lets track a event that happen after a beforeunload event.
client.flush().catch(() => {}); // flush that event
expect(server.requests.length).toEqual(3); // assert the server got the request.
expect(server.requests[2].async).toBe(true);
});
}

testWithUserAgent(
'Chrome 72',
'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'
);

testWithUserAgent('unknown browser', 'Special Kitty Cat Browser');

testWithUserAgent('empty user-agent', null);
});

describe('discards events during page unload', () => {
function testWithUserAgent(desc, ua) {
it('in ' + desc, async () => {
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ export function initialize(env, user, options = {}) {
const syncFlushHandler = () => {
platform.synchronousFlush = true;
client.flush().catch(() => {});
platform.synchronousFlush = false;
};
window.addEventListener('beforeunload', syncFlushHandler);
window.addEventListener('unload', syncFlushHandler);