Skip to content

Commit

Permalink
Initialize service worker from Url (#369)
Browse files Browse the repository at this point in the history
refactor(service-worker): initialize service worker from Url

Provider configuration option to set service worker from the URL (because of browser restrictions
for worker files to be registered from the same domain).
  • Loading branch information
parfeon authored Apr 23, 2024
1 parent 410930f commit 3fff075
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 46 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2024-04-23
version: v8.0.1
changes:
- type: improvement
text: "Provider configuration option to set service worker from the URL (because of browser restrictions for worker files to be registered from the same domain)."
- date: 2024-04-22
version: v8.0.0
changes:
Expand Down Expand Up @@ -967,7 +972,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '8.0.0'
version: '8.0.1'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -983,7 +988,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.0.0.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.0.1.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1654,7 +1659,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v8.0.0/pubnub.8.0.0.js
location: https://github.com/pubnub/javascript/releases/download/v8.0.1/pubnub.8.0.1.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v8.0.1
April 23 2024

#### Modified
- Provider configuration option to set service worker from the URL (because of browser restrictions for worker files to be registered from the same domain).

## v8.0.0
April 22 2024

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.0.0.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.0.0.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.0.1.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.0.1.min.js
2. Configure your keys:
Expand Down
20 changes: 8 additions & 12 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@
return;
const serviceWorkerContainer = navigator.serviceWorker;
serviceWorkerContainer
.register(`https://cdn.pubnub.com/sdk/javascript/dist/web/pubnub.worker.js`, {
.register(this.configuration.serviceWorkerUrl, {
scope: `/pubnub-${this.configuration.sdkVersion}`,
})
.then((registration) => {
Expand Down Expand Up @@ -3599,11 +3599,6 @@
* reconnection issues, set the flag to `false`. This allows the SDK reconnection logic to take over.
*/
const LISTEN_TO_BROWSER_NETWORK_EVENTS = true;
/**
* Whether PubNub client should spawn `Subscription` service worker for better user presence
* experience or not.
*/
const ENABLE_SERVICE_WORKER = false;
/**
* Whether PubNub client should try to utilize existing TCP connection for new requests or not.
*/
Expand All @@ -3614,13 +3609,13 @@
* @param configuration - User-provided configuration.
*/
const setDefaults = (configuration) => {
var _a, _b, _c, _d;
var _a, _b;
// Force disable service workers if environment doesn't support them.
if (((_a = configuration.enableServiceWorker) !== null && _a !== void 0 ? _a : ENABLE_SERVICE_WORKER) && !('serviceWorker' in navigator))
configuration.enableServiceWorker = false;
if (configuration.serviceWorkerUrl && !('serviceWorker' in navigator))
configuration.serviceWorkerUrl = null;
return Object.assign(Object.assign({}, setDefaults$1(configuration)), {
// Set platform-specific options.
listenToBrowserNetworkEvents: (_b = configuration.listenToBrowserNetworkEvents) !== null && _b !== void 0 ? _b : LISTEN_TO_BROWSER_NETWORK_EVENTS, enableServiceWorker: (_c = configuration.enableServiceWorker) !== null && _c !== void 0 ? _c : ENABLE_SERVICE_WORKER, keepAlive: (_d = configuration.keepAlive) !== null && _d !== void 0 ? _d : KEEP_ALIVE });
listenToBrowserNetworkEvents: (_a = configuration.listenToBrowserNetworkEvents) !== null && _a !== void 0 ? _a : LISTEN_TO_BROWSER_NETWORK_EVENTS, serviceWorkerUrl: configuration.serviceWorkerUrl, keepAlive: (_b = configuration.keepAlive) !== null && _b !== void 0 ? _b : KEEP_ALIVE });
};

var uuid = {exports: {}};
Expand Down Expand Up @@ -3782,7 +3777,7 @@
return base.PubNubFile;
},
get version() {
return '7.6.3';
return '8.0.1';
},
getVersion() {
return this.version;
Expand Down Expand Up @@ -13030,11 +13025,12 @@
}
// Setup transport provider.
let transport = new WebReactNativeTransport(clientConfiguration.keepAlive, clientConfiguration.logVerbosity);
if (configurationCopy.enableServiceWorker) {
if (configurationCopy.serviceWorkerUrl) {
// Inject subscription service worker into transport provider stack.
transport = new SubscriptionServiceWorkerMiddleware({
clientIdentifier: clientConfiguration._instanceId,
subscriptionKey: clientConfiguration.subscribeKey,
serviceWorkerUrl: configurationCopy.serviceWorkerUrl,
sdkVersion: clientConfiguration.getVersion(),
logVerbosity: clientConfiguration.logVerbosity,
transport,
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const makeConfiguration = (base, setupCryptoModule) => {
return base.PubNubFile;
},
get version() {
return '7.6.3';
return '8.0.1';
},
getVersion() {
return this.version;
Expand Down
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.0.0",
"version": "8.0.1",
"author": "PubNub <support@pubnub.com>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const makeConfiguration = (
return base.PubNubFile;
},
get version(): string {
return '8.0.0';
return '8.0.1';
},
getVersion(): string {
return this.version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type PubNubMiddlewareConfiguration = {
*/
subscriptionKey: string;

/**
* Url of the hoster `Subscription` service worker file.
*/
serviceWorkerUrl: string;

/**
* Current PubNub client version.
*/
Expand Down Expand Up @@ -190,7 +195,7 @@ export class SubscriptionServiceWorkerMiddleware implements Transport {
if (!('serviceWorker' in navigator)) return;
const serviceWorkerContainer = navigator.serviceWorker as ServiceWorkerContainer;
serviceWorkerContainer
.register(`SERVICE_WORKER_CDN/SERVICE_WORKER_FILE_PLACEHOLDER`, {
.register(this.configuration.serviceWorkerUrl, {
scope: `/pubnub-${this.configuration.sdkVersion}`,
})
.then((registration) => {
Expand Down
22 changes: 9 additions & 13 deletions src/web/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import {
*/
const LISTEN_TO_BROWSER_NETWORK_EVENTS = true;

/**
* Whether PubNub client should spawn `Subscription` service worker for better user presence
* experience or not.
*/
const ENABLE_SERVICE_WORKER = false;

/**
* Whether PubNub client should try to utilize existing TCP connection for new requests or not.
*/
Expand All @@ -43,12 +37,15 @@ export type PubNubConfiguration = UserConfiguration & {
listenToBrowserNetworkEvents?: boolean;

/**
* Whether PubNub client should spawn `Subscription` service worker for better user presence
* experience or not.
* Path to the hosted PubNub `Subscription` service worker.
*
* @default `true` (if supported)
* **Important:** Serving server should add `Service-Worker-Allowed: /` to response on service worker file request to
* make it possible for PubNub SDK use own `scope`.
* **Important:** Service worker file should be server from the same domain where PubNub client will be used. If
* statics provided from `subdomain.main.com` and page loaded from `account.main.com` - then server should be
* configured to serve worker file from `account.main.com`.
*/
enableServiceWorker?: boolean;
serviceWorkerUrl?: string | null;

/**
* If set to `true`, SDK will use the same TCP connection for each HTTP request, instead of
Expand All @@ -66,15 +63,14 @@ export type PubNubConfiguration = UserConfiguration & {
*/
export const setDefaults = (configuration: PubNubConfiguration): PubNubConfiguration & ExtendedConfiguration => {
// Force disable service workers if environment doesn't support them.
if ((configuration.enableServiceWorker ?? ENABLE_SERVICE_WORKER) && !('serviceWorker' in navigator))
configuration.enableServiceWorker = false;
if (configuration.serviceWorkerUrl && !('serviceWorker' in navigator)) configuration.serviceWorkerUrl = null;

return {
// Set base configuration defaults.
...setBaseDefaults(configuration),
// Set platform-specific options.
listenToBrowserNetworkEvents: configuration.listenToBrowserNetworkEvents ?? LISTEN_TO_BROWSER_NETWORK_EVENTS,
enableServiceWorker: configuration.enableServiceWorker ?? ENABLE_SERVICE_WORKER,
serviceWorkerUrl: configuration.serviceWorkerUrl,
keepAlive: configuration.keepAlive ?? KEEP_ALIVE,
};
};
3 changes: 2 additions & 1 deletion src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ export default class PubNub extends PubNubCore<ArrayBuffer | string, PubNubFileP
clientConfiguration.keepAlive,
clientConfiguration.logVerbosity!,
);
if (configurationCopy.enableServiceWorker) {
if (configurationCopy.serviceWorkerUrl) {
// Inject subscription service worker into transport provider stack.
transport = new SubscriptionServiceWorkerMiddleware({
clientIdentifier: clientConfiguration._instanceId,
subscriptionKey: clientConfiguration.subscribeKey,
serviceWorkerUrl: configurationCopy.serviceWorkerUrl,
sdkVersion: clientConfiguration.getVersion(),
logVerbosity: clientConfiguration.logVerbosity!,
transport,
Expand Down

0 comments on commit 3fff075

Please sign in to comment.