From a93e4d68a388d9e1e176172d3b756daa290986d0 Mon Sep 17 00:00:00 2001 From: JL Date: Wed, 20 Nov 2024 10:47:05 -0800 Subject: [PATCH] feature: unload on navigation event --- package.json | 2 +- src/web/components/configuration.ts | 17 +++++++++++++++++ src/web/index.ts | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e515925ca..a9df7843a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pubnub", - "version": "8.3.1", + "version": "8.3.2", "author": "PubNub ", "description": "Publish & Subscribe Real-time Messaging with PubNub", "scripts": { diff --git a/src/web/components/configuration.ts b/src/web/components/configuration.ts index 4f0709836..869473ae2 100644 --- a/src/web/components/configuration.ts +++ b/src/web/components/configuration.ts @@ -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. */ @@ -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. * @@ -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, diff --git a/src/web/index.ts b/src/web/index.ts index 4a0c33cd4..68e66db14 100644 --- a/src/web/index.ts +++ b/src/web/index.ts @@ -136,6 +136,12 @@ export default class PubNub extends PubNubCore { + this.destroy(true); + }); + } } private networkDownDetected() {