@tencentcloud/web-push 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.0.1] - 2025-12-19
4
+
5
+ - Optimize subscription logic
6
+ - Optimized state persistence logic, only persisting VAPID public key while resetting temporary states each session
7
+
3
8
  ## [1.0.0] - 2025-12-17
4
9
 
5
10
  ### Added
package/README.md CHANGED
@@ -81,7 +81,7 @@ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and reg
81
81
 
82
82
  <td rowspan="1" colSpan="1" >Number</td>
83
83
 
84
- <td rowspan="1" colSpan="1" >The SDKAppID for the push service Push. For reference: [Prerequisites > Enabling a Service](https://write.woa.com/#ef1e073e-23da-422d-b06b-f13fcda46734) to obtain the SDKAppID.</td>
84
+ <td rowspan="1" colSpan="1" >The SDKAppID for the push service Push. </td>
85
85
  </tr>
86
86
 
87
87
  <tr>
@@ -89,7 +89,7 @@ In your homepage (for example: `index.js`), add `@tencentcloud/web-push` and reg
89
89
 
90
90
  <td rowspan="1" colSpan="1" >String</td>
91
91
 
92
- <td rowspan="1" colSpan="1" >The client key for the push service Push. For reference: [Prerequisites > Enabling a Service](https://write.woa.com/#ef1e073e-23da-422d-b06b-f13fcda46734) to obtain the AppKey.</td>
92
+ <td rowspan="1" colSpan="1" >The client key for the push service Push.</td>
93
93
  </tr>
94
94
 
95
95
  <tr>
package/index.d.ts CHANGED
@@ -104,8 +104,6 @@ declare class WebPushSDK_2 implements WebPushSDK {
104
104
  private initializeBrowserCompatibility;
105
105
  private setupInternalListeners;
106
106
  private pushStatistics;
107
- private saveState;
108
- private restoreState;
109
107
  private clearState;
110
108
  }
111
109
 
package/index.esm.js CHANGED
@@ -15544,7 +15544,7 @@ var professional = { exports: {} };
15544
15544
  })(professional);
15545
15545
  var professionalExports = professional.exports;
15546
15546
  const ChatSDK = /* @__PURE__ */ getDefaultExportFromCjs(professionalExports);
15547
- const version = "1.0.0";
15547
+ const version = "1.0.1";
15548
15548
  var EVENT = /* @__PURE__ */ ((EVENT2) => {
15549
15549
  EVENT2["MESSAGE_RECEIVED"] = "message_received";
15550
15550
  EVENT2["MESSAGE_REVOKED"] = "message_revoked";
@@ -16158,6 +16158,49 @@ const browserSupport = BrowserSupport.getInstance();
16158
16158
  function getBrowserInfo() {
16159
16159
  return browserSupport.getBrowserInfo();
16160
16160
  }
16161
+ const _Storage = class _Storage2 {
16162
+ static set(key, value) {
16163
+ try {
16164
+ const serializedValue = JSON.stringify(value);
16165
+ localStorage.setItem(_Storage2.PREFIX + key, serializedValue);
16166
+ } catch (error) {
16167
+ logger.error("Storage set error:", error);
16168
+ }
16169
+ }
16170
+ static get(key, defaultValue) {
16171
+ try {
16172
+ const item = localStorage.getItem(_Storage2.PREFIX + key);
16173
+ if (item === null) {
16174
+ return defaultValue || null;
16175
+ }
16176
+ return JSON.parse(item);
16177
+ } catch (error) {
16178
+ logger.error("Storage get error:", error);
16179
+ return defaultValue || null;
16180
+ }
16181
+ }
16182
+ static remove(key) {
16183
+ try {
16184
+ localStorage.removeItem(_Storage2.PREFIX + key);
16185
+ } catch (error) {
16186
+ logger.error("Storage remove error:", error);
16187
+ }
16188
+ }
16189
+ static clear() {
16190
+ try {
16191
+ const keys = Object.keys(localStorage);
16192
+ keys.forEach((key) => {
16193
+ if (key.startsWith(_Storage2.PREFIX)) {
16194
+ localStorage.removeItem(key);
16195
+ }
16196
+ });
16197
+ } catch (error) {
16198
+ logger.error("Storage clear error:", error);
16199
+ }
16200
+ }
16201
+ };
16202
+ _Storage.PREFIX = "webpush_";
16203
+ let Storage = _Storage;
16161
16204
  class ServiceWorkerManager {
16162
16205
  constructor(eventEmitter) {
16163
16206
  this.registration = null;
@@ -16287,7 +16330,15 @@ class ServiceWorkerManager {
16287
16330
  if (!this.registration) {
16288
16331
  throw new Error("Service Worker not registered");
16289
16332
  }
16333
+ const storedVapidKey = this.getStoredVapidPublicKey();
16290
16334
  let subscription = await this.registration.pushManager.getSubscription();
16335
+ if (subscription && storedVapidKey !== vapidPublicKey) {
16336
+ logger.log(
16337
+ "VAPID public key changed, unsubscribing existing subscription"
16338
+ );
16339
+ await subscription.unsubscribe();
16340
+ subscription = null;
16341
+ }
16291
16342
  if (!subscription) {
16292
16343
  subscription = await this.registration.pushManager.subscribe({
16293
16344
  userVisibleOnly: true,
@@ -16295,6 +16346,7 @@ class ServiceWorkerManager {
16295
16346
  vapidPublicKey
16296
16347
  )
16297
16348
  });
16349
+ this.setStoreVapidPublicKey(vapidPublicKey);
16298
16350
  }
16299
16351
  logger.log("Get push subscription successful", subscription);
16300
16352
  const subscriptionInfo = this.getSubscriptionInfo(subscription);
@@ -16562,6 +16614,7 @@ class ServiceWorkerManager {
16562
16614
  if (subscription) {
16563
16615
  const result = await subscription.unsubscribe();
16564
16616
  logger.log("Unsubscribe from push notifications", result);
16617
+ this.clearStoredVapidPublicKey();
16565
16618
  return result;
16566
16619
  }
16567
16620
  return true;
@@ -16572,6 +16625,7 @@ class ServiceWorkerManager {
16572
16625
  }
16573
16626
  const result = await this.registration.unregister();
16574
16627
  this.registration = null;
16628
+ this.clearStoredVapidPublicKey();
16575
16629
  logger.log("Unregister Service Worker", result);
16576
16630
  return result;
16577
16631
  }
@@ -16638,50 +16692,31 @@ class ServiceWorkerManager {
16638
16692
  getRegistration() {
16639
16693
  return this.registration;
16640
16694
  }
16641
- }
16642
- const _Storage = class _Storage2 {
16643
- static set(key, value) {
16644
- try {
16645
- const serializedValue = JSON.stringify(value);
16646
- localStorage.setItem(_Storage2.PREFIX + key, serializedValue);
16647
- } catch (error) {
16648
- logger.error("Storage set error:", error);
16649
- }
16650
- }
16651
- static get(key, defaultValue) {
16695
+ getStoredVapidPublicKey() {
16652
16696
  try {
16653
- const item = localStorage.getItem(_Storage2.PREFIX + key);
16654
- if (item === null) {
16655
- return defaultValue || null;
16656
- }
16657
- return JSON.parse(item);
16697
+ return Storage.get("sdk_vapid_key");
16658
16698
  } catch (error) {
16659
- logger.error("Storage get error:", error);
16660
- return defaultValue || null;
16699
+ logger.error("Failed to get stored VAPID public key", error);
16700
+ return null;
16661
16701
  }
16662
16702
  }
16663
- static remove(key) {
16703
+ setStoreVapidPublicKey(vapidPublicKey) {
16664
16704
  try {
16665
- localStorage.removeItem(_Storage2.PREFIX + key);
16705
+ Storage.set("sdk_vapid_key", vapidPublicKey);
16706
+ logger.log("VAPID public key stored successfully");
16666
16707
  } catch (error) {
16667
- logger.error("Storage remove error:", error);
16708
+ logger.error("Failed to store VAPID public key", error);
16668
16709
  }
16669
16710
  }
16670
- static clear() {
16711
+ clearStoredVapidPublicKey() {
16671
16712
  try {
16672
- const keys = Object.keys(localStorage);
16673
- keys.forEach((key) => {
16674
- if (key.startsWith(_Storage2.PREFIX)) {
16675
- localStorage.removeItem(key);
16676
- }
16677
- });
16713
+ Storage.remove("sdk_vapid_key");
16714
+ logger.log("VAPID public key cleared successfully");
16678
16715
  } catch (error) {
16679
- logger.error("Storage clear error:", error);
16716
+ logger.error("Failed to clear VAPID public key", error);
16680
16717
  }
16681
16718
  }
16682
- };
16683
- _Storage.PREFIX = "webpush_";
16684
- let Storage = _Storage;
16719
+ }
16685
16720
  var e = "undefined" != typeof global ? global : "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, t = [], r = [], n = "undefined" != typeof Uint8Array ? Uint8Array : Array, i = false;
16686
16721
  function o() {
16687
16722
  i = true;
@@ -20996,7 +21031,6 @@ class WebPushSDK {
20996
21031
  this.initializeBrowserCompatibility().catch((error) => {
20997
21032
  logger.error("Browser compatibility initialization failed", error);
20998
21033
  });
20999
- this.restoreState();
21000
21034
  this.setupInternalListeners();
21001
21035
  }
21002
21036
  static getInstance() {
@@ -21038,7 +21072,6 @@ class WebPushSDK {
21038
21072
  );
21039
21073
  await this.setToken(subscriptionInfo);
21040
21074
  this.isRegistered = true;
21041
- this.saveState();
21042
21075
  logger.log("Push service registration successful", this.registrationID);
21043
21076
  return this.registrationID;
21044
21077
  } catch (error) {
@@ -21316,27 +21349,12 @@ Please refresh the page after enabling notifications.`;
21316
21349
  logger.error(`Failed to record message ${type} statistics`, error);
21317
21350
  }
21318
21351
  }
21319
- saveState() {
21320
- Storage.set("sdk_state", {
21321
- isRegistered: this.isRegistered,
21322
- registrationID: this.registrationID,
21323
- SDKAppID: this.SDKAppID,
21324
- appKey: this.appKey,
21325
- vapidPublicKey: this.vapidPublicKey
21326
- });
21327
- }
21328
- restoreState() {
21329
- const state = Storage.get("sdk_state");
21330
- if (state) {
21331
- this.isRegistered = state.isRegistered || false;
21332
- this.registrationID = state.registrationID || "";
21333
- this.SDKAppID = state.SDKAppID || 0;
21334
- this.appKey = state.appKey || "";
21335
- this.vapidPublicKey = state.vapidPublicKey || "";
21336
- }
21337
- }
21338
21352
  clearState() {
21339
- Storage.remove("sdk_state");
21353
+ this.isRegistered = false;
21354
+ this.registrationID = "";
21355
+ this.SDKAppID = 0;
21356
+ this.appKey = "";
21357
+ this.vapidPublicKey = "";
21340
21358
  }
21341
21359
  /**
21342
21360
  * 向 service-worker 发送通知消息