Skip to content

Commit

Permalink
Added SW param check to shouldInstallWorker
Browse files Browse the repository at this point in the history
* Added a new changedServiceWorkerParams method which check if;
   - OneSignal ServiceWorker path, filename, or query params changed.
   - OneSignal ServierWorker scope changed
* This is a new check that was added to shouldInstallWorker
* A developer might change the OneSignal service worker params to take
advantage of the PR #745 "Remove ServiceWorker Page Control Requirement"
  - This way the new OneSignal SW scope can be setup while their new
  root scoped SW can be installed at the same time.
  • Loading branch information
jkasten2 authored and rgomezp committed Mar 3, 2021
1 parent 0efb79e commit f48f766
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/helpers/ServiceWorkerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OutcomesConfig } from "../models/Outcomes";
import OutcomesHelper from './shared/OutcomesHelper';
import { cancelableTimeout, CancelableTimeoutPromise } from './sw/CancelableTimeout';
import { OSServiceWorkerFields } from "../service-worker/types";
import Utils from "../context/shared/utils/Utils";

declare var self: ServiceWorkerGlobalScope & OSServiceWorkerFields;

Expand All @@ -36,6 +37,14 @@ export default class ServiceWorkerHelper {
return ServiceWorkerHelper.appendServiceWorkerParams(workerFullPath, appId);
}

public static getPossibleServiceWorkerHrefs(
config: ServiceWorkerManagerConfig,
appId: string
): string[] {
const workerFullPaths = [config.workerAPath.getFullPath(), config.workerBPath.getFullPath()];
return workerFullPaths.map((href) => ServiceWorkerHelper.appendServiceWorkerParams(href, appId));
}

private static appendServiceWorkerParams(workerFullPath: string, appId: string): string {
const fullPath = new URL(workerFullPath, OneSignalUtils.getBaseUrl()).href;
const appIdHasQueryParam = Utils.encodeHashAsUriComponent({appId});
Expand Down
57 changes: 53 additions & 4 deletions src/managers/ServiceWorkerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ export class ServiceWorkerManager {
}
}

// 4. Is a OneSignal ServiceWorker not installed now?, if not and
// notification permissions are enabled we should install.
// This prevents an unnecessary install which saves bandwidth
// 4. Is a OneSignal ServiceWorker not installed now?
// If not and notification permissions are enabled we should install.
// This prevents an unnecessary install of the OneSignal worker which saves bandwidth
const workerState = await this.getActiveState();
if (workerState === ServiceWorkerActiveState.None || workerState === ServiceWorkerActiveState.ThirdParty) {
const permission = await OneSignal.context.permissionManager.getNotificationPermission(
Expand All @@ -167,10 +167,59 @@ export class ServiceWorkerManager {
return permission === "granted";
}

// 5. We have a OneSignal ServiceWorker installed, is there an update?
// 5. We have a OneSignal ServiceWorker installed, but did the path or scope of the ServiceWorker change?
if (await this.haveParamsChanged()) {
return true;
}

// 6. We have a OneSignal ServiceWorker installed, is there an update?
return this.workerNeedsUpdate();
}

private async haveParamsChanged(): Promise<boolean> {
// 1. No workerRegistration
const workerRegistration = await this.context.serviceWorkerManager.getRegistration();
if (!workerRegistration) {
Log.info(
"[changedServiceWorkerParams] workerRegistration not found at scope",
this.config.registrationOptions.scope
);
return true;
}

// 2. Different scope
const existingSwScope = new URL(workerRegistration.scope).pathname;
const configuredSwScope = this.config.registrationOptions.scope;
if (existingSwScope != configuredSwScope) {
Log.info(
"[changedServiceWorkerParams] ServiceWorker scope changing",
{ a_old: existingSwScope, b_new: configuredSwScope }
);
return true;
}

// 3. Different href?, asking if (path + filename [A or B] + queryParams) is different
const availableWorker = ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration);
const serviceWorkerHrefs = ServiceWorkerHelper.getPossibleServiceWorkerHrefs(
this.config,
this.context.appConfig.appId
);
// 3.1 If we can't get a scriptURL assume it is different
if (!availableWorker?.scriptURL) {
return true;
}
// 3.2 We don't care if the only differences is between OneSignal's A(Worker) vs B(WorkerUpdater) filename.
if (serviceWorkerHrefs.indexOf(availableWorker.scriptURL) === -1) {
Log.info(
"[changedServiceWorkerParams] ServiceWorker href changing:",
{ a_old: availableWorker?.scriptURL, b_new: serviceWorkerHrefs }
);
return true;
}

return false;
}

/**
* Performs a service worker update by swapping out the current service worker
* with a content-identical but differently named alternate service worker
Expand Down

0 comments on commit f48f766

Please sign in to comment.