refactor(service-worker): make SwPush and SwUpdate tree-shakable (#61670)

In this commit, we mark the `SwPush` and `SwUpdate` classes as root providers. As a result, they are no longer statically referenced in the `provideServiceWorker` providers list, which previously forced them to be explicitly bundled into the main file. These classes might never be used—some consumers may use the service worker only for prefetching and caching assets.

Practically speaking, even if a user injects the `SwPush` class without calling `provideServiceWorker()`, it would result in a DI error because the communication channel dependency is not available. There is no practical reason to keep these classes as non-root providers or to reference them explicitly.

Currently, some users work around this by using `patch-package` to modify the service worker code and remove these classes from the providers list.

PR Close #61670
This commit is contained in:
arturovt
2025-05-24 15:07:26 +03:00
committed by kirjs
parent 3f7b77dcd6
commit 41b10396d2
4 changed files with 3 additions and 9 deletions
+1 -3
View File
@@ -9,13 +9,11 @@
import {ModuleWithProviders, NgModule} from '@angular/core';
import {provideServiceWorker, SwRegistrationOptions} from './provider';
import {SwPush} from './push';
import {SwUpdate} from './update';
/**
* @publicApi
*/
@NgModule({providers: [SwPush, SwUpdate]})
@NgModule()
export class ServiceWorkerModule {
/**
* Register the given Angular Service Worker script.
-4
View File
@@ -21,8 +21,6 @@ import {
import type {Observable} from 'rxjs';
import {NgswCommChannel} from './low_level';
import {SwPush} from './push';
import {SwUpdate} from './update';
import {RuntimeErrorCode} from './errors';
export const SCRIPT = new InjectionToken<string>(ngDevMode ? 'NGSW_REGISTER_SCRIPT' : '');
@@ -213,8 +211,6 @@ export function provideServiceWorker(
options: SwRegistrationOptions = {},
): EnvironmentProviders {
return makeEnvironmentProviders([
SwPush,
SwUpdate,
{provide: SCRIPT, useValue: script},
{provide: SwRegistrationOptions, useValue: options},
{
+1 -1
View File
@@ -92,7 +92,7 @@ import {ERR_SW_NOT_SUPPORTED, NgswCommChannel, PushEvent} from './low_level';
*
* @publicApi
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class SwPush {
/**
* Emits the payloads of the received push notification messages.
+1 -1
View File
@@ -25,7 +25,7 @@ import {
*
* @publicApi
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class SwUpdate {
/**
* Emits a `VersionDetectedEvent` event whenever a new version is detected on the server.