-
Notifications
You must be signed in to change notification settings - Fork 0
GAUD-10126: switch from unload to pagehide #41
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,8 @@ export class ServerLogger { | |
| this._batchSize = batchSize; | ||
| this._batchTime = batchTime; | ||
| this._logs = []; | ||
| window.addEventListener('unload', this._onUnload.bind(this)); | ||
| this._onPageHide = this._onPageHide.bind(this); | ||
| window.addEventListener('pagehide', this._onPageHide); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should mention that I spent a lot of time looking into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation says:
I'm guessing
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does yes. Following a link (both on the same domain or another), reloading the current page, as well as normal back/forward all trigger it. |
||
| } | ||
|
|
||
| logBatch(logs) { | ||
|
|
@@ -36,6 +37,11 @@ export class ServerLogger { | |
| } | ||
| } | ||
|
|
||
| _disconnectForTesting() { | ||
| window.removeEventListener('pagehide', this._onPageHide); | ||
| clearTimeout(this._batchTimeout); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new tests I added were failing sporadically because the old loggers from the other tests were still doing things after their tests finished! |
||
| } | ||
|
|
||
| async _log(logs) { | ||
| let options = { | ||
| method: 'POST', | ||
|
|
@@ -58,19 +64,17 @@ export class ServerLogger { | |
| } | ||
| } | ||
|
|
||
| async _onUnload() { | ||
| async _onPageHide() { | ||
| clearTimeout(this._batchTimeout); | ||
| if (!this._loggerPromise && !navigator || !navigator.sendBeacon) { | ||
| if (!this._loggerPromise || !navigator?.sendBeacon || this._logs.length === 0) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I think this was a bug before -- it should have been |
||
| return; | ||
| } | ||
| if (this._logs.length > 0) { | ||
| try { | ||
| const logger = await this._loggerPromise; | ||
| const data = JSON.stringify(this._logs); | ||
| navigator.sendBeacon(logger.Endpoint, data); | ||
| } catch (err) { | ||
| console.error(err, this._logs); | ||
| } | ||
| try { | ||
| const logger = await this._loggerPromise; | ||
| const data = JSON.stringify(this._logs); | ||
| navigator.sendBeacon(logger.Endpoint, data); | ||
| } catch (err) { | ||
| console.error(err, this._logs); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| "access": "public" | ||
| }, | ||
| "main": "index.js", | ||
| "type": "module", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This project was already ESM, so I don't think this is going to be a problem. |
||
| "scripts": { | ||
| "lint": "npm run lint:eslint", | ||
| "lint:eslint": "eslint .", | ||
|
|
@@ -16,7 +17,6 @@ | |
| "author": "D2L Corporation", | ||
| "license": "Apache-2.0", | ||
| "devDependencies": { | ||
| "@babel/core": "^7", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was doing weird things in the lint output. |
||
| "@brightspace-ui/testing": "^1", | ||
| "eslint": "^9", | ||
| "eslint-config-brightspace": "^2", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -697,7 +697,10 @@ describe('logging', () => { | |
| consoleErrorStub = stub(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(() => restore()); | ||
| afterEach(() => { | ||
| restore(); | ||
| logger._disconnectForTesting(); | ||
| }); | ||
|
|
||
| describe('batching', () => { | ||
|
|
||
|
|
@@ -862,30 +865,45 @@ describe('logging', () => { | |
|
|
||
| }); | ||
|
|
||
| describe('unload', () => { | ||
| describe('pagehide', () => { | ||
| let beaconStub; | ||
|
|
||
| beforeEach(() => { | ||
| beaconStub = stub(navigator, 'sendBeacon'); | ||
| }); | ||
|
|
||
| afterEach(() => restore()); | ||
|
|
||
| it('should send remaining logs as beacon on unload', async() => { | ||
|
|
||
| it('should send remaining logs as beacon when "pagehide" event is triggered', async() => { | ||
| logger.logBatch([{ message: '1' }, { message: '2' }]); | ||
|
|
||
| window.dispatchEvent(new Event('unload')); | ||
| window.dispatchEvent(new Event('pagehide')); | ||
|
|
||
| await aTimeout(0); | ||
|
|
||
| expect(beaconStub).to.have.been.calledOnce; | ||
| expect(beaconStub.calledOnce).to.be.true; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other format of this |
||
| const [endpoint, data] = beaconStub.getCall(0).args; | ||
|
|
||
| expect(endpoint).to.equal('/test/endpoint/0'); | ||
| expect(data).to.equal(JSON.stringify([{ message: '1' }, { message: '2' }])); | ||
| }); | ||
|
|
||
| it('should not send anything as beacon if no logs have ever been sent', async() => { | ||
| window.dispatchEvent(new Event('pagehide')); | ||
| await aTimeout(0); | ||
|
|
||
| expect(beaconStub.called).to.be.false; | ||
| }); | ||
|
|
||
| it('should not send anything as beacon if there are no logs', async() => { | ||
| logger.logBatch([{ message: '1' }]); | ||
| await aTimeout(batchTime); | ||
|
|
||
| window.dispatchEvent(new Event('pagehide')); | ||
| await aTimeout(0); | ||
|
|
||
| expect(beaconStub.called).to.be.false; | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I specifically didn't feature flag this since our logger both runs in so many places that likely don't have access to our feature flags but also runs so early in the page cycle.