From 2035fe84e5a1bee9205daab0f34bd5e5fbc7ebb0 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Mon, 2 Dec 2024 15:28:07 -0800 Subject: [PATCH 1/4] Correctly wait for service worker registration to become active before allowing any operations on it. --- .changeset/neat-beans-rescue.md | 5 +++ .../src/helpers/registerDefaultSw.ts | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 .changeset/neat-beans-rescue.md diff --git a/.changeset/neat-beans-rescue.md b/.changeset/neat-beans-rescue.md new file mode 100644 index 00000000000..278e5511da0 --- /dev/null +++ b/.changeset/neat-beans-rescue.md @@ -0,0 +1,5 @@ +--- +'@firebase/messaging': patch +--- + +Fix an issue where PushManager.subscribe() is called too soon after registering the default service worker. diff --git a/packages/messaging/src/helpers/registerDefaultSw.ts b/packages/messaging/src/helpers/registerDefaultSw.ts index 239e6ed8244..c5f76faaf33 100644 --- a/packages/messaging/src/helpers/registerDefaultSw.ts +++ b/packages/messaging/src/helpers/registerDefaultSw.ts @@ -39,9 +39,40 @@ export async function registerDefaultSw( messaging.swRegistration.update().catch(() => { /* it is non blocking and we don't care if it failed */ }); + await waitForRegistrationActive(messaging.swRegistration); } catch (e) { throw ERROR_FACTORY.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, { browserErrorMessage: (e as Error)?.message }); } } + +/** + * Waits for registration to become active. MDN documentation claims that + * a service worker registration should be ready to use after awaiting + * navigator.serviceWorker.register() but that doesn't seem to be the case in + * practice, causing the SDK to throw errors when calling + * swRegistration.pushManager.subscribe() too soon after register(). The only + * solution seems to be waiting for the service worker registration `state` + * to become "active". + */ +async function waitForRegistrationActive( + registration: ServiceWorkerRegistration +): Promise { + return new Promise((resolve, reject) => { + if (registration.active) { + resolve(); + } + const incomingSw = registration.installing || registration.waiting; + if (incomingSw) { + incomingSw.onstatechange = ev => { + if ((ev.target as ServiceWorker)?.state === 'activated') { + incomingSw.onstatechange = null; + resolve(); + } + }; + } else { + reject(new Error('No incoming service worker found.')); + } + }); +} From e2ee51345c5b0f2a453266cb9282317d0661fd42 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Tue, 3 Dec 2024 11:32:54 -0800 Subject: [PATCH 2/4] Added a timeout --- .changeset/neat-beans-rescue.md | 2 +- common/api-review/messaging-sw.api.md | 1 + common/api-review/messaging.api.md | 1 + docs-devsite/messaging_.gettokenoptions.md | 11 ++++++ docs-devsite/messaging_sw.gettokenoptions.md | 11 ++++++ packages/messaging/src/api/deleteToken.ts | 5 +-- packages/messaging/src/api/getToken.ts | 6 +++- .../src/helpers/registerDefaultSw.ts | 34 +++++++++++++++---- packages/messaging/src/helpers/updateSwReg.ts | 5 +-- .../messaging/src/interfaces/public-types.ts | 5 +++ packages/messaging/src/util/constants.ts | 1 + 11 files changed, 69 insertions(+), 13 deletions(-) diff --git a/.changeset/neat-beans-rescue.md b/.changeset/neat-beans-rescue.md index 278e5511da0..b8d74a67a56 100644 --- a/.changeset/neat-beans-rescue.md +++ b/.changeset/neat-beans-rescue.md @@ -1,5 +1,5 @@ --- -'@firebase/messaging': patch +'@firebase/messaging': minor --- Fix an issue where PushManager.subscribe() is called too soon after registering the default service worker. diff --git a/common/api-review/messaging-sw.api.md b/common/api-review/messaging-sw.api.md index 74f823de196..cab3ddc527c 100644 --- a/common/api-review/messaging-sw.api.md +++ b/common/api-review/messaging-sw.api.md @@ -24,6 +24,7 @@ export function getMessaging(app?: FirebaseApp): Messaging; // @public export interface GetTokenOptions { serviceWorkerRegistration?: ServiceWorkerRegistration; + serviceWorkerRegistrationTimeout?: number; vapidKey?: string; } diff --git a/common/api-review/messaging.api.md b/common/api-review/messaging.api.md index b74fc3a69fa..be927e4f49a 100644 --- a/common/api-review/messaging.api.md +++ b/common/api-review/messaging.api.md @@ -27,6 +27,7 @@ export function getToken(messaging: Messaging, options?: GetTokenOptions): Promi // @public export interface GetTokenOptions { serviceWorkerRegistration?: ServiceWorkerRegistration; + serviceWorkerRegistrationTimeout?: number; vapidKey?: string; } diff --git a/docs-devsite/messaging_.gettokenoptions.md b/docs-devsite/messaging_.gettokenoptions.md index d2c9bc89896..1e63b248aa7 100644 --- a/docs-devsite/messaging_.gettokenoptions.md +++ b/docs-devsite/messaging_.gettokenoptions.md @@ -23,6 +23,7 @@ export interface GetTokenOptions | Property | Type | Description | | --- | --- | --- | | [serviceWorkerRegistration](./messaging_.gettokenoptions.md#gettokenoptionsserviceworkerregistration) | ServiceWorkerRegistration | The service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See [Access the registration token](https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token) for more details. | +| [serviceWorkerRegistrationTimeout](./messaging_.gettokenoptions.md#gettokenoptionsserviceworkerregistrationtimeout) | number | Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. | | [vapidKey](./messaging_.gettokenoptions.md#gettokenoptionsvapidkey) | string | The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. | ## GetTokenOptions.serviceWorkerRegistration @@ -35,6 +36,16 @@ The service worker registration for receiving push messaging. If the registratio serviceWorkerRegistration?: ServiceWorkerRegistration; ``` +## GetTokenOptions.serviceWorkerRegistrationTimeout + +Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. + +Signature: + +```typescript +serviceWorkerRegistrationTimeout?: number; +``` + ## GetTokenOptions.vapidKey The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. diff --git a/docs-devsite/messaging_sw.gettokenoptions.md b/docs-devsite/messaging_sw.gettokenoptions.md index 6fea1e41498..90ae266d22a 100644 --- a/docs-devsite/messaging_sw.gettokenoptions.md +++ b/docs-devsite/messaging_sw.gettokenoptions.md @@ -23,6 +23,7 @@ export interface GetTokenOptions | Property | Type | Description | | --- | --- | --- | | [serviceWorkerRegistration](./messaging_sw.gettokenoptions.md#gettokenoptionsserviceworkerregistration) | ServiceWorkerRegistration | The service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See [Access the registration token](https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token) for more details. | +| [serviceWorkerRegistrationTimeout](./messaging_sw.gettokenoptions.md#gettokenoptionsserviceworkerregistrationtimeout) | number | Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. | | [vapidKey](./messaging_sw.gettokenoptions.md#gettokenoptionsvapidkey) | string | The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. | ## GetTokenOptions.serviceWorkerRegistration @@ -35,6 +36,16 @@ The service worker registration for receiving push messaging. If the registratio serviceWorkerRegistration?: ServiceWorkerRegistration; ``` +## GetTokenOptions.serviceWorkerRegistrationTimeout + +Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. + +Signature: + +```typescript +serviceWorkerRegistrationTimeout?: number; +``` + ## GetTokenOptions.vapidKey The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. diff --git a/packages/messaging/src/api/deleteToken.ts b/packages/messaging/src/api/deleteToken.ts index 5a1725f2f0d..7c49e86ae4e 100644 --- a/packages/messaging/src/api/deleteToken.ts +++ b/packages/messaging/src/api/deleteToken.ts @@ -22,14 +22,15 @@ import { deleteTokenInternal } from '../internals/token-manager'; import { registerDefaultSw } from '../helpers/registerDefaultSw'; export async function deleteToken( - messaging: MessagingService + messaging: MessagingService, + swRegistrationTimeoutMillis?: number ): Promise { if (!navigator) { throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW); } if (!messaging.swRegistration) { - await registerDefaultSw(messaging); + await registerDefaultSw(messaging, swRegistrationTimeoutMillis); } return deleteTokenInternal(messaging); diff --git a/packages/messaging/src/api/getToken.ts b/packages/messaging/src/api/getToken.ts index 58c9fea90c4..ef015630869 100644 --- a/packages/messaging/src/api/getToken.ts +++ b/packages/messaging/src/api/getToken.ts @@ -40,7 +40,11 @@ export async function getToken( } await updateVapidKey(messaging, options?.vapidKey); - await updateSwReg(messaging, options?.serviceWorkerRegistration); + await updateSwReg( + messaging, + options?.serviceWorkerRegistration, + options?.serviceWorkerRegistrationTimeout + ); return getTokenInternal(messaging); } diff --git a/packages/messaging/src/helpers/registerDefaultSw.ts b/packages/messaging/src/helpers/registerDefaultSw.ts index c5f76faaf33..235f616386a 100644 --- a/packages/messaging/src/helpers/registerDefaultSw.ts +++ b/packages/messaging/src/helpers/registerDefaultSw.ts @@ -15,13 +15,18 @@ * limitations under the License. */ -import { DEFAULT_SW_PATH, DEFAULT_SW_SCOPE } from '../util/constants'; +import { + DEFAULT_REGISTRATION_TIMEOUT, + DEFAULT_SW_PATH, + DEFAULT_SW_SCOPE +} from '../util/constants'; import { ERROR_FACTORY, ErrorCode } from '../util/errors'; import { MessagingService } from '../messaging-service'; export async function registerDefaultSw( - messaging: MessagingService + messaging: MessagingService, + swRegistrationTimeoutMillis?: number ): Promise { try { messaging.swRegistration = await navigator.serviceWorker.register( @@ -39,7 +44,10 @@ export async function registerDefaultSw( messaging.swRegistration.update().catch(() => { /* it is non blocking and we don't care if it failed */ }); - await waitForRegistrationActive(messaging.swRegistration); + await waitForRegistrationActive( + messaging.swRegistration, + swRegistrationTimeoutMillis + ); } catch (e) { throw ERROR_FACTORY.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, { browserErrorMessage: (e as Error)?.message @@ -57,21 +65,33 @@ export async function registerDefaultSw( * to become "active". */ async function waitForRegistrationActive( - registration: ServiceWorkerRegistration + registration: ServiceWorkerRegistration, + swRegistrationTimeoutMillis: number = DEFAULT_REGISTRATION_TIMEOUT ): Promise { return new Promise((resolve, reject) => { + const rejectTimeout = setTimeout( + () => + reject( + new Error( + `Service worker not registered after ${swRegistrationTimeoutMillis} ms` + ) + ), + swRegistrationTimeoutMillis + ); + const incomingSw = registration.installing || registration.waiting; if (registration.active) { + clearTimeout(rejectTimeout); resolve(); - } - const incomingSw = registration.installing || registration.waiting; - if (incomingSw) { + } else if (incomingSw) { incomingSw.onstatechange = ev => { if ((ev.target as ServiceWorker)?.state === 'activated') { incomingSw.onstatechange = null; + clearTimeout(rejectTimeout); resolve(); } }; } else { + clearTimeout(rejectTimeout); reject(new Error('No incoming service worker found.')); } }); diff --git a/packages/messaging/src/helpers/updateSwReg.ts b/packages/messaging/src/helpers/updateSwReg.ts index 927599aaa73..ace783f6c59 100644 --- a/packages/messaging/src/helpers/updateSwReg.ts +++ b/packages/messaging/src/helpers/updateSwReg.ts @@ -22,10 +22,11 @@ import { registerDefaultSw } from './registerDefaultSw'; export async function updateSwReg( messaging: MessagingService, - swRegistration?: ServiceWorkerRegistration | undefined + swRegistration?: ServiceWorkerRegistration | undefined, + swRegistrationTimeoutMillis?: number ): Promise { if (!swRegistration && !messaging.swRegistration) { - await registerDefaultSw(messaging); + await registerDefaultSw(messaging, swRegistrationTimeoutMillis); } if (!swRegistration && !!messaging.swRegistration) { diff --git a/packages/messaging/src/interfaces/public-types.ts b/packages/messaging/src/interfaces/public-types.ts index b3e33965a9c..e6343504fe0 100644 --- a/packages/messaging/src/interfaces/public-types.ts +++ b/packages/messaging/src/interfaces/public-types.ts @@ -132,6 +132,11 @@ export interface GetTokenOptions { * for more details. */ serviceWorkerRegistration?: ServiceWorkerRegistration; + /** + * Milliseconds to wait for service worker registration to complete before + * rejecting and throwing an error. Defaults to 10000. + */ + serviceWorkerRegistrationTimeout?: number; } /** diff --git a/packages/messaging/src/util/constants.ts b/packages/messaging/src/util/constants.ts index 8491380a5a0..e06c8cfaa34 100644 --- a/packages/messaging/src/util/constants.ts +++ b/packages/messaging/src/util/constants.ts @@ -36,6 +36,7 @@ export const MAX_NUMBER_OF_EVENTS_PER_LOG_REQUEST = 1000; export const MAX_RETRIES = 3; export const LOG_INTERVAL_IN_MS = 86400000; //24 hour export const DEFAULT_BACKOFF_TIME_MS = 5000; +export const DEFAULT_REGISTRATION_TIMEOUT = 10000; // FCM log source name registered at Firelog: 'FCM_CLIENT_EVENT_LOGGING'. It uniquely identifies // FCM's logging configuration. From 9ba1f05b59fac5807e3bb8147d7b58297c06a258 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Tue, 3 Dec 2024 12:22:11 -0800 Subject: [PATCH 3/4] Removed customizable timeout --- .changeset/neat-beans-rescue.md | 2 +- common/api-review/messaging-sw.api.md | 1 - common/api-review/messaging.api.md | 1 - docs-devsite/messaging_.gettokenoptions.md | 11 ----------- docs-devsite/messaging_sw.gettokenoptions.md | 11 ----------- packages/messaging/src/api/deleteToken.ts | 5 ++--- packages/messaging/src/api/getToken.ts | 3 +-- packages/messaging/src/helpers/registerDefaultSw.ts | 13 +++++-------- packages/messaging/src/helpers/updateSwReg.ts | 5 ++--- packages/messaging/src/interfaces/public-types.ts | 5 ----- 10 files changed, 11 insertions(+), 46 deletions(-) diff --git a/.changeset/neat-beans-rescue.md b/.changeset/neat-beans-rescue.md index b8d74a67a56..278e5511da0 100644 --- a/.changeset/neat-beans-rescue.md +++ b/.changeset/neat-beans-rescue.md @@ -1,5 +1,5 @@ --- -'@firebase/messaging': minor +'@firebase/messaging': patch --- Fix an issue where PushManager.subscribe() is called too soon after registering the default service worker. diff --git a/common/api-review/messaging-sw.api.md b/common/api-review/messaging-sw.api.md index cab3ddc527c..74f823de196 100644 --- a/common/api-review/messaging-sw.api.md +++ b/common/api-review/messaging-sw.api.md @@ -24,7 +24,6 @@ export function getMessaging(app?: FirebaseApp): Messaging; // @public export interface GetTokenOptions { serviceWorkerRegistration?: ServiceWorkerRegistration; - serviceWorkerRegistrationTimeout?: number; vapidKey?: string; } diff --git a/common/api-review/messaging.api.md b/common/api-review/messaging.api.md index be927e4f49a..b74fc3a69fa 100644 --- a/common/api-review/messaging.api.md +++ b/common/api-review/messaging.api.md @@ -27,7 +27,6 @@ export function getToken(messaging: Messaging, options?: GetTokenOptions): Promi // @public export interface GetTokenOptions { serviceWorkerRegistration?: ServiceWorkerRegistration; - serviceWorkerRegistrationTimeout?: number; vapidKey?: string; } diff --git a/docs-devsite/messaging_.gettokenoptions.md b/docs-devsite/messaging_.gettokenoptions.md index 1e63b248aa7..d2c9bc89896 100644 --- a/docs-devsite/messaging_.gettokenoptions.md +++ b/docs-devsite/messaging_.gettokenoptions.md @@ -23,7 +23,6 @@ export interface GetTokenOptions | Property | Type | Description | | --- | --- | --- | | [serviceWorkerRegistration](./messaging_.gettokenoptions.md#gettokenoptionsserviceworkerregistration) | ServiceWorkerRegistration | The service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See [Access the registration token](https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token) for more details. | -| [serviceWorkerRegistrationTimeout](./messaging_.gettokenoptions.md#gettokenoptionsserviceworkerregistrationtimeout) | number | Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. | | [vapidKey](./messaging_.gettokenoptions.md#gettokenoptionsvapidkey) | string | The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. | ## GetTokenOptions.serviceWorkerRegistration @@ -36,16 +35,6 @@ The service worker registration for receiving push messaging. If the registratio serviceWorkerRegistration?: ServiceWorkerRegistration; ``` -## GetTokenOptions.serviceWorkerRegistrationTimeout - -Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. - -Signature: - -```typescript -serviceWorkerRegistrationTimeout?: number; -``` - ## GetTokenOptions.vapidKey The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. diff --git a/docs-devsite/messaging_sw.gettokenoptions.md b/docs-devsite/messaging_sw.gettokenoptions.md index 90ae266d22a..6fea1e41498 100644 --- a/docs-devsite/messaging_sw.gettokenoptions.md +++ b/docs-devsite/messaging_sw.gettokenoptions.md @@ -23,7 +23,6 @@ export interface GetTokenOptions | Property | Type | Description | | --- | --- | --- | | [serviceWorkerRegistration](./messaging_sw.gettokenoptions.md#gettokenoptionsserviceworkerregistration) | ServiceWorkerRegistration | The service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See [Access the registration token](https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token) for more details. | -| [serviceWorkerRegistrationTimeout](./messaging_sw.gettokenoptions.md#gettokenoptionsserviceworkerregistrationtimeout) | number | Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. | | [vapidKey](./messaging_sw.gettokenoptions.md#gettokenoptionsvapidkey) | string | The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. | ## GetTokenOptions.serviceWorkerRegistration @@ -36,16 +35,6 @@ The service worker registration for receiving push messaging. If the registratio serviceWorkerRegistration?: ServiceWorkerRegistration; ``` -## GetTokenOptions.serviceWorkerRegistrationTimeout - -Milliseconds to wait for service worker registration to complete before rejecting and throwing an error. Defaults to 10000. - -Signature: - -```typescript -serviceWorkerRegistrationTimeout?: number; -``` - ## GetTokenOptions.vapidKey The public server key provided to push services. The key is used to authenticate push subscribers to receive push messages only from sending servers that hold the corresponding private key. If it is not provided, a default VAPID key is used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to generate and import a VAPID key for your project with [Configure Web Credentials with FCM](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_in_your_app). See [The Web Push Protocol](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol) for details on web push services. diff --git a/packages/messaging/src/api/deleteToken.ts b/packages/messaging/src/api/deleteToken.ts index 7c49e86ae4e..5a1725f2f0d 100644 --- a/packages/messaging/src/api/deleteToken.ts +++ b/packages/messaging/src/api/deleteToken.ts @@ -22,15 +22,14 @@ import { deleteTokenInternal } from '../internals/token-manager'; import { registerDefaultSw } from '../helpers/registerDefaultSw'; export async function deleteToken( - messaging: MessagingService, - swRegistrationTimeoutMillis?: number + messaging: MessagingService ): Promise { if (!navigator) { throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW); } if (!messaging.swRegistration) { - await registerDefaultSw(messaging, swRegistrationTimeoutMillis); + await registerDefaultSw(messaging); } return deleteTokenInternal(messaging); diff --git a/packages/messaging/src/api/getToken.ts b/packages/messaging/src/api/getToken.ts index ef015630869..cb7cc37a51c 100644 --- a/packages/messaging/src/api/getToken.ts +++ b/packages/messaging/src/api/getToken.ts @@ -42,8 +42,7 @@ export async function getToken( await updateVapidKey(messaging, options?.vapidKey); await updateSwReg( messaging, - options?.serviceWorkerRegistration, - options?.serviceWorkerRegistrationTimeout + options?.serviceWorkerRegistration ); return getTokenInternal(messaging); diff --git a/packages/messaging/src/helpers/registerDefaultSw.ts b/packages/messaging/src/helpers/registerDefaultSw.ts index 235f616386a..a32173e27e8 100644 --- a/packages/messaging/src/helpers/registerDefaultSw.ts +++ b/packages/messaging/src/helpers/registerDefaultSw.ts @@ -25,8 +25,7 @@ import { ERROR_FACTORY, ErrorCode } from '../util/errors'; import { MessagingService } from '../messaging-service'; export async function registerDefaultSw( - messaging: MessagingService, - swRegistrationTimeoutMillis?: number + messaging: MessagingService ): Promise { try { messaging.swRegistration = await navigator.serviceWorker.register( @@ -45,8 +44,7 @@ export async function registerDefaultSw( /* it is non blocking and we don't care if it failed */ }); await waitForRegistrationActive( - messaging.swRegistration, - swRegistrationTimeoutMillis + messaging.swRegistration ); } catch (e) { throw ERROR_FACTORY.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, { @@ -65,18 +63,17 @@ export async function registerDefaultSw( * to become "active". */ async function waitForRegistrationActive( - registration: ServiceWorkerRegistration, - swRegistrationTimeoutMillis: number = DEFAULT_REGISTRATION_TIMEOUT + registration: ServiceWorkerRegistration ): Promise { return new Promise((resolve, reject) => { const rejectTimeout = setTimeout( () => reject( new Error( - `Service worker not registered after ${swRegistrationTimeoutMillis} ms` + `Service worker not registered after ${DEFAULT_REGISTRATION_TIMEOUT} ms` ) ), - swRegistrationTimeoutMillis + DEFAULT_REGISTRATION_TIMEOUT ); const incomingSw = registration.installing || registration.waiting; if (registration.active) { diff --git a/packages/messaging/src/helpers/updateSwReg.ts b/packages/messaging/src/helpers/updateSwReg.ts index ace783f6c59..927599aaa73 100644 --- a/packages/messaging/src/helpers/updateSwReg.ts +++ b/packages/messaging/src/helpers/updateSwReg.ts @@ -22,11 +22,10 @@ import { registerDefaultSw } from './registerDefaultSw'; export async function updateSwReg( messaging: MessagingService, - swRegistration?: ServiceWorkerRegistration | undefined, - swRegistrationTimeoutMillis?: number + swRegistration?: ServiceWorkerRegistration | undefined ): Promise { if (!swRegistration && !messaging.swRegistration) { - await registerDefaultSw(messaging, swRegistrationTimeoutMillis); + await registerDefaultSw(messaging); } if (!swRegistration && !!messaging.swRegistration) { diff --git a/packages/messaging/src/interfaces/public-types.ts b/packages/messaging/src/interfaces/public-types.ts index e6343504fe0..b3e33965a9c 100644 --- a/packages/messaging/src/interfaces/public-types.ts +++ b/packages/messaging/src/interfaces/public-types.ts @@ -132,11 +132,6 @@ export interface GetTokenOptions { * for more details. */ serviceWorkerRegistration?: ServiceWorkerRegistration; - /** - * Milliseconds to wait for service worker registration to complete before - * rejecting and throwing an error. Defaults to 10000. - */ - serviceWorkerRegistrationTimeout?: number; } /** From 2a81f96dd5dd756300a27ab06c55766b5d87f266 Mon Sep 17 00:00:00 2001 From: Christina Holland Date: Tue, 3 Dec 2024 13:30:03 -0800 Subject: [PATCH 4/4] format --- packages/messaging/src/api/getToken.ts | 5 +---- packages/messaging/src/helpers/registerDefaultSw.ts | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/messaging/src/api/getToken.ts b/packages/messaging/src/api/getToken.ts index cb7cc37a51c..58c9fea90c4 100644 --- a/packages/messaging/src/api/getToken.ts +++ b/packages/messaging/src/api/getToken.ts @@ -40,10 +40,7 @@ export async function getToken( } await updateVapidKey(messaging, options?.vapidKey); - await updateSwReg( - messaging, - options?.serviceWorkerRegistration - ); + await updateSwReg(messaging, options?.serviceWorkerRegistration); return getTokenInternal(messaging); } diff --git a/packages/messaging/src/helpers/registerDefaultSw.ts b/packages/messaging/src/helpers/registerDefaultSw.ts index a32173e27e8..28890c93bd2 100644 --- a/packages/messaging/src/helpers/registerDefaultSw.ts +++ b/packages/messaging/src/helpers/registerDefaultSw.ts @@ -43,9 +43,7 @@ export async function registerDefaultSw( messaging.swRegistration.update().catch(() => { /* it is non blocking and we don't care if it failed */ }); - await waitForRegistrationActive( - messaging.swRegistration - ); + await waitForRegistrationActive(messaging.swRegistration); } catch (e) { throw ERROR_FACTORY.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, { browserErrorMessage: (e as Error)?.message