Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion spec/mocha.helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable import/no-unresolved, import/extensions */
const qs = require('qs');
const sinon = require('sinon');
const store = require('../test/utils/store');

// Trigger browser resize event
Expand All @@ -21,7 +22,8 @@ const triggerResize = () => {
const triggerUnload = () => {
const unloadEvent = document.createEvent('Event');

unloadEvent.initEvent('beforeunload', true, true);
unloadEvent.initEvent('visibilitychange', true, true);
sinon.stub(document, 'visibilityState').value('hidden');

global.window.unload = () => {
global.window.dispatchEvent(unloadEvent);
Expand Down
17 changes: 13 additions & 4 deletions src/utils/request-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ class RequestQueue {
? true
: false; // Defaults to 'false'

// Mark if page environment is unloading
helpers.addEventListener('beforeunload', () => {
this.pageUnloading = true;
helpers.addEventListener('visibilitychange', () => {
// Mark if page environment is unloading
if (document.visibilityState === 'hidden') {
Copy link
Contributor

@jjl014 jjl014 Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visibilityState=hidden based on what MDN says can mean the "document is either a background tab or part of a minimized window, or the OS screen lock is active".

Would it make sense to add in some additional logic here to call send() if the page becomes visible again? 🤔

if (document.visibilityState === 'visibile') {
    this.send();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I somehow completely missed the notification for this one. I don't see any reason not to. Good call

this.pageUnloading = true;
} else if (document.visibilityState === 'visible' && this.pageUnloading === true) {
// Send events once page is visible again
this.pageUnloading = false;

if (this.sendTrackingEvents) {
this.send();
}
}
});

if (this.sendTrackingEvents) {
Expand Down Expand Up @@ -182,7 +191,7 @@ class RequestQueue {
if (this.options && this.options.trackingSendDelay === 0) {
this.sendEvents();
} else {
// Defer sending of events to give beforeunload time to register (avoids race condition)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened/happens when beforeunload/visibilitychange registers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeunload/visibilitychange changes the pageUnloading value and if the page is unloading than we wouldn't send events and wait for the next page to send those events from the

// Defer sending of events to give visibilitychange time to register (avoids race condition)
setTimeout(this.sendEvents.bind(this), (this.options && this.options.trackingSendDelay) || 250);
}
}
Expand Down
Loading