Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@
},
{
"path": "./build/releases/OneSignalSDK.page.es6.js",
"limit": "42.7 kB",
"limit": "42.57 kB",
"gzip": true
},
{
"path": "./build/releases/OneSignalSDK.sw.js",
"limit": "12.65 kB",
"limit": "12.51 kB",
"gzip": true
},
{
Expand Down
37 changes: 37 additions & 0 deletions src/shared/helpers/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from 'vite-plus/test';

import type { AppConfig } from '../config/types';
import type { ContextInterface } from '../context/types';
import { getServiceWorkerManager } from './context';

const buildContext = (path: string, serviceWorkerPath: string): ContextInterface =>
({
_appConfig: {
userConfig: { path, serviceWorkerPath },
} as AppConfig,
}) as ContextInterface;

const workerPathFor = (path: string, serviceWorkerPath: string): string => {
const manager = getServiceWorkerManager(buildContext(path, serviceWorkerPath));
return manager['_config'].workerPath._getFullPath();
};

describe('getServiceWorkerManager worker path', () => {
test('joins default root path with worker file name', () => {
expect(workerPathFor('/', 'OneSignalSDKWorker.js')).toBe('/OneSignalSDKWorker.js');
});

test('collapses a leading slash in serviceWorkerPath to avoid a protocol-relative URL', () => {
expect(workerPathFor('/', '/OneSignalSDKWorker.js')).toBe('/OneSignalSDKWorker.js');
});

test('joins a nested path with a trailing slash', () => {
expect(workerPathFor('/push/onesignal/', 'OneSignalSDKWorker.js')).toBe(
'/push/onesignal/OneSignalSDKWorker.js',
);
});

test('normalizes slashes on both sides of the join', () => {
expect(workerPathFor('/push/', '/OneSignalSDKWorker.js')).toBe('/push/OneSignalSDKWorker.js');
});
});
9 changes: 6 additions & 3 deletions src/shared/helpers/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export function getServiceWorkerManager(context: ContextInterface): ServiceWorke

if (config.userConfig) {
if (config.userConfig.path) {
serviceWorkerManagerConfig.workerPath = new Path(
`${config.userConfig.path}${config.userConfig.serviceWorkerPath}`,
);
// Join with exactly one slash. A leading "//" forms a protocol-relative
// URL, which the browser resolves to a different origin and rejects with a
// SecurityError when registering the worker.
const basePath = config.userConfig.path.replace(/\/+$/, '');
const workerPath = (config.userConfig.serviceWorkerPath ?? '').replace(/^\/+/, '');
serviceWorkerManagerConfig.workerPath = new Path(`${basePath}/${workerPath}`);
}
if (config.userConfig.serviceWorkerParam) {
serviceWorkerManagerConfig.registrationOptions = config.userConfig.serviceWorkerParam;
Expand Down
Loading