Skip to content

Commit b7bb0cf

Browse files
authored
fix: join service worker path with a single slash (#1471)
1 parent fd95b43 commit b7bb0cf

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@
7777
},
7878
{
7979
"path": "./build/releases/OneSignalSDK.page.es6.js",
80-
"limit": "42.7 kB",
80+
"limit": "42.57 kB",
8181
"gzip": true
8282
},
8383
{
8484
"path": "./build/releases/OneSignalSDK.sw.js",
85-
"limit": "12.65 kB",
85+
"limit": "12.51 kB",
8686
"gzip": true
8787
},
8888
{

src/shared/helpers/context.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, test } from 'vite-plus/test';
2+
3+
import type { AppConfig } from '../config/types';
4+
import type { ContextInterface } from '../context/types';
5+
import { getServiceWorkerManager } from './context';
6+
7+
const buildContext = (path: string, serviceWorkerPath: string): ContextInterface =>
8+
({
9+
_appConfig: {
10+
userConfig: { path, serviceWorkerPath },
11+
} as AppConfig,
12+
}) as ContextInterface;
13+
14+
const workerPathFor = (path: string, serviceWorkerPath: string): string => {
15+
const manager = getServiceWorkerManager(buildContext(path, serviceWorkerPath));
16+
return manager['_config'].workerPath._getFullPath();
17+
};
18+
19+
describe('getServiceWorkerManager worker path', () => {
20+
test('joins default root path with worker file name', () => {
21+
expect(workerPathFor('/', 'OneSignalSDKWorker.js')).toBe('/OneSignalSDKWorker.js');
22+
});
23+
24+
test('collapses a leading slash in serviceWorkerPath to avoid a protocol-relative URL', () => {
25+
expect(workerPathFor('/', '/OneSignalSDKWorker.js')).toBe('/OneSignalSDKWorker.js');
26+
});
27+
28+
test('joins a nested path with a trailing slash', () => {
29+
expect(workerPathFor('/push/onesignal/', 'OneSignalSDKWorker.js')).toBe(
30+
'/push/onesignal/OneSignalSDKWorker.js',
31+
);
32+
});
33+
34+
test('normalizes slashes on both sides of the join', () => {
35+
expect(workerPathFor('/push/', '/OneSignalSDKWorker.js')).toBe('/push/OneSignalSDKWorker.js');
36+
});
37+
});

src/shared/helpers/context.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ export function getServiceWorkerManager(context: ContextInterface): ServiceWorke
1717

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

0 commit comments

Comments
 (0)