Skip to content

Commit 951f9ca

Browse files
committed
Use 'pagehide' for page termination by default.
1 parent 66deb25 commit 951f9ca

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,14 @@ export class IndexedDbPersistence implements Persistence {
942942
}
943943
}
944944

945+
/**
946+
* Returns the page termination event to listen to. 'pagehide' is recommended
947+
* if available, it falls back to the less reliable 'unload'.
948+
*/
949+
private terminationEvent(): string {
950+
return 'onpagehide' in this.window! ? 'pagehide' : 'unload';
951+
}
952+
945953
/**
946954
* Attaches a window.unload handler that will synchronously write our
947955
* clientId to a "zombie client id" location in LocalStorage. This can be used
@@ -966,7 +974,10 @@ export class IndexedDbPersistence implements Persistence {
966974
return this.shutdown();
967975
});
968976
};
969-
this.window.addEventListener('unload', this.windowUnloadHandler);
977+
this.window.addEventListener(
978+
this.terminationEvent(),
979+
this.windowUnloadHandler
980+
);
970981
}
971982
}
972983

@@ -976,7 +987,10 @@ export class IndexedDbPersistence implements Persistence {
976987
typeof this.window?.removeEventListener === 'function',
977988
"Expected 'window.removeEventListener' to be a function"
978989
);
979-
this.window!.removeEventListener('unload', this.windowUnloadHandler);
990+
this.window!.removeEventListener(
991+
this.terminationEvent(),
992+
this.windowUnloadHandler
993+
);
980994
this.windowUnloadHandler = null;
981995
}
982996
}

0 commit comments

Comments
 (0)