Skip to content

Use 'pagehide' for page termination by default. #4886

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 6 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { debugAssert } from '../util/assert';
import { AsyncQueue, DelayedOperation, TimerId } from '../util/async_queue';
import { Code, FirestoreError } from '../util/error';
import { logDebug, logError } from '../util/log';
import { DocumentLike, WindowLike } from '../util/types';
import { DocumentLike, WindowLike, terminationEvent } from '../util/types';

import { BundleCache } from './bundle_cache';
import { IndexManager } from './index_manager';
Expand Down Expand Up @@ -966,7 +966,10 @@ export class IndexedDbPersistence implements Persistence {
return this.shutdown();
});
};
this.window.addEventListener('unload', this.windowUnloadHandler);
this.window.addEventListener(
terminationEvent(this.window),
this.windowUnloadHandler
);
}
}

Expand All @@ -976,7 +979,10 @@ export class IndexedDbPersistence implements Persistence {
typeof this.window?.removeEventListener === 'function',
"Expected 'window.removeEventListener' to be a function"
);
this.window!.removeEventListener('unload', this.windowUnloadHandler);
this.window!.removeEventListener(
terminationEvent(this.window),
this.windowUnloadHandler
);
this.windowUnloadHandler = null;
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { logError, logDebug } from '../util/log';
import { primitiveComparator } from '../util/misc';
import { SortedMap } from '../util/sorted_map';
import { SortedSet } from '../util/sorted_set';
import { isSafeInteger, WindowLike } from '../util/types';
import { isSafeInteger, terminationEvent, WindowLike } from '../util/types';

import {
CLIENT_STATE_KEY_PREFIX,
Expand Down Expand Up @@ -613,7 +613,9 @@ export class WebStorageSharedClientState implements SharedClientState {

// Register a window unload hook to remove the client metadata entry from
// WebStorage even if `shutdown()` was not called.
this.window.addEventListener('unload', () => this.shutdown());
this.window.addEventListener(terminationEvent(this.window), () =>
this.shutdown()
);

this.started = true;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/firestore/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export interface WindowLike {
removeEventListener(type: string, listener: EventListener): void;
}

/**
* Returns the page termination event to listen to. 'pagehide' is recommended
* if available, it falls back to the less reliable 'unload'.
*/
export function terminationEvent(window: WindowLike | null): string {
return 'onpagehide' in window! ? 'pagehide' : 'unload';
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can just use "pagehide" and do not need to fall back: https://caniuse.com/page-transition-events

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, done.

}

/** The subset of the browser's Document interface used by the SDK. */
export interface DocumentLike {
readonly visibilityState: VisibilityState;
Expand Down