Skip to content

Commit 5220b51

Browse files
SkyZeroZxcrisbeto
authored andcommitted
feat(service-worker): Adds for type in provideServiceWorker (#62831)
Enables specifying the script type ('classic' or 'module') when registering Service Workers, improving compatibility with ES module features. PR Close #62831
1 parent 700225a commit 5220b51

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

goldens/public-api/service-worker/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export abstract class SwRegistrationOptions {
7272
enabled?: boolean;
7373
registrationStrategy?: string | (() => Observable<unknown>);
7474
scope?: string;
75+
type?: WorkerType;
7576
updateViaCache?: ServiceWorkerUpdateViaCache;
7677
}
7778

packages/service-worker/src/provider.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ export function ngswAppInitializer(): void {
101101
}
102102

103103
navigator.serviceWorker
104-
.register(script, {scope: options.scope, updateViaCache: options.updateViaCache})
104+
.register(script, {
105+
scope: options.scope,
106+
updateViaCache: options.updateViaCache,
107+
type: options.type,
108+
})
105109
.catch((err) =>
106110
console.error(
107111
formatRuntimeError(
@@ -159,6 +163,16 @@ export abstract class SwRegistrationOptions {
159163
*/
160164
updateViaCache?: ServiceWorkerUpdateViaCache;
161165

166+
/**
167+
* The type of the ServiceWorker script to register.
168+
* [ServiceWorkerRegistration#type](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#type)
169+
* - `classic`: Registers the script as a classic worker. ES module features such as `import` and `export` are NOT allowed in the script.
170+
* - `module`: Registers the script as an ES module. Allows use of `import`/`export` syntax and module features.
171+
*
172+
* @default 'classic'
173+
*/
174+
type?: WorkerType;
175+
162176
/**
163177
* A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
164178
* control. It will be used when calling

packages/service-worker/test/provider_spec.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ async function waitForReadyToRegister() {
6767
};
6868

6969
it('sets the registration options', async () => {
70-
await configTestBed({enabled: true, scope: 'foo', updateViaCache: 'all'});
70+
await configTestBed({enabled: true, scope: 'foo', updateViaCache: 'all', type: 'classic'});
7171

7272
expect(TestBed.inject(SwRegistrationOptions)).toEqual({
7373
enabled: true,
7474
scope: 'foo',
7575
updateViaCache: 'all',
76+
type: 'classic',
77+
});
78+
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
79+
scope: 'foo',
80+
updateViaCache: 'all',
81+
type: 'classic',
7682
});
77-
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: 'foo', updateViaCache: 'all'});
7883
});
7984

8085
it('can disable the SW', async () => {
@@ -91,6 +96,7 @@ async function waitForReadyToRegister() {
9196
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
9297
scope: undefined,
9398
updateViaCache: undefined,
99+
type: undefined,
94100
});
95101
});
96102

@@ -101,6 +107,18 @@ async function waitForReadyToRegister() {
101107
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
102108
scope: undefined,
103109
updateViaCache: 'imports',
110+
type: undefined,
111+
});
112+
});
113+
114+
it('can set type', async () => {
115+
await configTestBed({enabled: true, type: 'module'});
116+
117+
expect(TestBed.inject(SwUpdate).isEnabled).toBe(true);
118+
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
119+
scope: undefined,
120+
updateViaCache: undefined,
121+
type: 'module',
104122
});
105123
});
106124

@@ -111,6 +129,7 @@ async function waitForReadyToRegister() {
111129
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
112130
scope: undefined,
113131
updateViaCache: undefined,
132+
type: undefined,
114133
});
115134
});
116135

@@ -155,17 +174,24 @@ async function waitForReadyToRegister() {
155174
};
156175

157176
it('sets the registration options (and overwrites those set via `provideServiceWorker()`', async () => {
158-
configTestBed({enabled: true, scope: 'provider', updateViaCache: 'imports'});
177+
configTestBed({
178+
enabled: true,
179+
scope: 'provider',
180+
updateViaCache: 'imports',
181+
type: 'module',
182+
});
159183
await untilStable();
160184
expect(TestBed.inject(SwRegistrationOptions)).toEqual({
161185
enabled: true,
162186
scope: 'provider',
163187
updateViaCache: 'imports',
188+
type: 'module',
164189
});
165190
await waitForReadyToRegister();
166191
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
167192
scope: 'provider',
168193
updateViaCache: 'imports',
194+
type: 'module',
169195
});
170196
});
171197

@@ -186,6 +212,7 @@ async function waitForReadyToRegister() {
186212
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
187213
scope: undefined,
188214
updateViaCache: undefined,
215+
type: undefined,
189216
});
190217
});
191218

@@ -198,6 +225,7 @@ async function waitForReadyToRegister() {
198225
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
199226
scope: undefined,
200227
updateViaCache: undefined,
228+
type: undefined,
201229
});
202230
});
203231

@@ -271,6 +299,7 @@ async function waitForReadyToRegister() {
271299
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
272300
scope: undefined,
273301
updateViaCache: undefined,
302+
type: undefined,
274303
});
275304
}));
276305

@@ -281,6 +310,7 @@ async function waitForReadyToRegister() {
281310
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
282311
scope: undefined,
283312
updateViaCache: undefined,
313+
type: undefined,
284314
});
285315
}));
286316

@@ -298,6 +328,7 @@ async function waitForReadyToRegister() {
298328
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
299329
scope: undefined,
300330
updateViaCache: undefined,
331+
type: undefined,
301332
});
302333
}));
303334

@@ -308,6 +339,7 @@ async function waitForReadyToRegister() {
308339
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
309340
scope: undefined,
310341
updateViaCache: undefined,
342+
type: undefined,
311343
});
312344
}));
313345

@@ -323,6 +355,7 @@ async function waitForReadyToRegister() {
323355
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
324356
scope: undefined,
325357
updateViaCache: undefined,
358+
type: undefined,
326359
});
327360
}));
328361

@@ -340,6 +373,7 @@ async function waitForReadyToRegister() {
340373
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
341374
scope: undefined,
342375
updateViaCache: undefined,
376+
type: undefined,
343377
});
344378
}));
345379

@@ -357,6 +391,7 @@ async function waitForReadyToRegister() {
357391
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
358392
scope: undefined,
359393
updateViaCache: undefined,
394+
type: undefined,
360395
});
361396
}));
362397

@@ -366,6 +401,7 @@ async function waitForReadyToRegister() {
366401
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
367402
scope: undefined,
368403
updateViaCache: undefined,
404+
type: undefined,
369405
});
370406
});
371407

@@ -381,6 +417,7 @@ async function waitForReadyToRegister() {
381417
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
382418
scope: undefined,
383419
updateViaCache: undefined,
420+
type: undefined,
384421
});
385422
}));
386423

@@ -397,6 +434,7 @@ async function waitForReadyToRegister() {
397434
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
398435
scope: undefined,
399436
updateViaCache: undefined,
437+
type: undefined,
400438
});
401439
}));
402440

@@ -413,6 +451,7 @@ async function waitForReadyToRegister() {
413451
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
414452
scope: undefined,
415453
updateViaCache: undefined,
454+
type: undefined,
416455
});
417456
}));
418457

@@ -429,6 +468,7 @@ async function waitForReadyToRegister() {
429468
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
430469
scope: undefined,
431470
updateViaCache: undefined,
471+
type: undefined,
432472
});
433473
}));
434474

0 commit comments

Comments
 (0)