Skip to content
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

feature: cleanup SDK on navigation event #421

Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "8.3.1",
"version": "8.3.2",
"author": "PubNub <[email protected]>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
17 changes: 17 additions & 0 deletions src/web/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { ICryptoModule } from '../../core/interfaces/crypto-module';
*/
const LISTEN_TO_BROWSER_NETWORK_EVENTS = true;

/**
* If set to `true`, SDK will attempt to destroy itself when the browser begins navigation via the beforeunload event.
* Fixes issues with Safari memory leaks when refreshing, or navigating away and returning with the browser back button.
*/
const CLEANUP_ON_BROWSER_NAVIGATION = false;

/**
* Whether verbose logging should be enabled for `Subscription` worker to print debug messages or not.
*/
Expand All @@ -42,6 +48,16 @@ export type PubNubConfiguration = UserConfiguration & {
*/
listenToBrowserNetworkEvents?: boolean;

/**
* If set to `true`, SDK will attempt to destroy itself when the browser begins navigation via the
* beforeunload event.
*
* This fixes issues with Safari memory leaks when refreshing, or navigating away and returning with the back button.
*
* @default `false`
*/
cleanupOnNavigationEvent?: boolean;

/**
* Path to the hosted PubNub `Subscription` service worker.
*
Expand Down Expand Up @@ -116,6 +132,7 @@ export const setDefaults = (configuration: PubNubConfiguration): PubNubConfigura
...setBaseDefaults(configuration),
// Set platform-specific options.
listenToBrowserNetworkEvents: configuration.listenToBrowserNetworkEvents ?? LISTEN_TO_BROWSER_NETWORK_EVENTS,
cleanupOnNavigationEvent: configuration.cleanupOnNavigationEvent ?? CLEANUP_ON_BROWSER_NAVIGATION,
subscriptionWorkerUrl: configuration.subscriptionWorkerUrl,
subscriptionWorkerLogVerbosity: configuration.subscriptionWorkerLogVerbosity ?? SUBSCRIPTION_WORKER_LOG_VERBOSITY,
keepAlive: configuration.keepAlive ?? KEEP_ALIVE,
Expand Down
6 changes: 6 additions & 0 deletions src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export default class PubNub extends PubNubCore<ArrayBuffer | string, PubNubFileP
this.networkUpDetected();
});
}

if (configuration.cleanupOnNavigationEvent ?? false) {
window.addEventListener('beforeunload', () => {
this.destroy(true);
});
}
}

private networkDownDetected() {
Expand Down