Skip to content

feat(service-worker): Adds for type in provideServiceWorker #62831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions goldens/public-api/service-worker/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export abstract class SwRegistrationOptions {
enabled?: boolean;
registrationStrategy?: string | (() => Observable<unknown>);
scope?: string;
type?: WorkerType;
updateViaCache?: ServiceWorkerUpdateViaCache;
}

Expand Down
16 changes: 15 additions & 1 deletion packages/service-worker/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export function ngswAppInitializer(): void {
}

navigator.serviceWorker
.register(script, {scope: options.scope, updateViaCache: options.updateViaCache})
.register(script, {
scope: options.scope,
updateViaCache: options.updateViaCache,
type: options.type,
})
.catch((err) =>
console.error(
formatRuntimeError(
Expand Down Expand Up @@ -159,6 +163,16 @@ export abstract class SwRegistrationOptions {
*/
updateViaCache?: ServiceWorkerUpdateViaCache;

/**
* The type of the ServiceWorker script to register.
* [ServiceWorkerRegistration#type](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#type)
* - `classic`: Registers the script as a classic worker. ES module features such as `import` and `export` are NOT allowed in the script.
* - `module`: Registers the script as an ES module. Allows use of `import`/`export` syntax and module features.
*
* @default 'classic'
*/
type?: WorkerType;

/**
* A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
* control. It will be used when calling
Expand Down
46 changes: 43 additions & 3 deletions packages/service-worker/test/provider_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ async function waitForReadyToRegister() {
};

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

expect(TestBed.inject(SwRegistrationOptions)).toEqual({
enabled: true,
scope: 'foo',
updateViaCache: 'all',
type: 'classic',
});
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: 'foo',
updateViaCache: 'all',
type: 'classic',
});
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: 'foo', updateViaCache: 'all'});
});

it('can disable the SW', async () => {
Expand All @@ -91,6 +96,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
});

Expand All @@ -101,6 +107,18 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: 'imports',
type: undefined,
});
});

it('can set type', async () => {
await configTestBed({enabled: true, type: 'module'});

expect(TestBed.inject(SwUpdate).isEnabled).toBe(true);
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: 'module',
});
});

Expand All @@ -111,6 +129,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
});

Expand Down Expand Up @@ -155,17 +174,24 @@ async function waitForReadyToRegister() {
};

it('sets the registration options (and overwrites those set via `provideServiceWorker()`', async () => {
configTestBed({enabled: true, scope: 'provider', updateViaCache: 'imports'});
configTestBed({
enabled: true,
scope: 'provider',
updateViaCache: 'imports',
type: 'module',
});
await untilStable();
expect(TestBed.inject(SwRegistrationOptions)).toEqual({
enabled: true,
scope: 'provider',
updateViaCache: 'imports',
type: 'module',
});
await waitForReadyToRegister();
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: 'provider',
updateViaCache: 'imports',
type: 'module',
});
});

Expand All @@ -186,6 +212,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
});

Expand All @@ -198,6 +225,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
});

Expand Down Expand Up @@ -271,6 +299,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -281,6 +310,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -298,6 +328,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -308,6 +339,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -323,6 +355,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -340,6 +373,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -357,6 +391,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -366,6 +401,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
});

Expand All @@ -381,6 +417,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -397,6 +434,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -413,6 +451,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand All @@ -429,6 +468,7 @@ async function waitForReadyToRegister() {
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {
scope: undefined,
updateViaCache: undefined,
type: undefined,
});
}));

Expand Down