Skip to content

Commit

Permalink
Remove Firefox detection
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Mar 13, 2021
1 parent a90eda3 commit 08f68ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
25 changes: 8 additions & 17 deletions src/lib/onHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,19 @@ export interface OnHiddenCallback {
(event: Event): void;
}

let beforeUnloadFixAdded = false;

export const onHidden = (cb: OnHiddenCallback, once?: boolean) => {
// Adding a `beforeunload` listener is needed to fix this bug:
// https://bugs.chromium.org/p/chromium/issues/detail?id=987409
if (!beforeUnloadFixAdded &&
// Avoid adding this in Firefox as it'll break bfcache:
// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
// @ts-ignore
typeof InstallTrigger === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-empty-function
addEventListener('beforeunload', () => {});
beforeUnloadFixAdded = true;
}

const onVisibilityChange = (event: Event) => {
if (document.visibilityState === 'hidden') {
const onHiddenOrPageHide = (event: Event) => {
if (event.type === 'pagehide' || document.visibilityState === 'hidden') {
cb(event);
if (once) {
removeEventListener('visibilitychange', onVisibilityChange, true);
removeEventListener('visibilitychange', onHiddenOrPageHide, true);
removeEventListener('pagehide', onHiddenOrPageHide, true);
}
}
}
addEventListener('visibilitychange', onVisibilityChange, true);
addEventListener('visibilitychange', onHiddenOrPageHide, true);
// Some browsers have buggy implementations of visibilitychange,
// so we use pagehide in addition, just to be safe.
addEventListener('pagehide', onHiddenOrPageHide, true);
};
9 changes: 6 additions & 3 deletions test/e2e/getTTFB-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ function assertValidEntry(entry) {

assert.strictEqual(entry.entryType, 'navigation');
for (const timingProp of timingProps) {
if (!(entry[timingProp] >= 0)) {
console.log(timingProp, entry[timingProp]);
if (browser.capabilities.browserName === 'firefox' &&
timingProp === 'fetchStart' &&
entry[timingProp] === -1) {
// Firefox sometimes reports the fetchStart value as -1
// https://bugzilla.mozilla.org/show_bug.cgi?id=1429422
continue;
}

assert(entry[timingProp] >= 0);
}
}
Expand Down

0 comments on commit 08f68ff

Please sign in to comment.