From 41b10396d2389ecdb38770909770357e49158e2a Mon Sep 17 00:00:00 2001 From: arturovt Date: Sat, 24 May 2025 15:07:26 +0300 Subject: [PATCH] refactor(service-worker): make `SwPush` and `SwUpdate` tree-shakable (#61670) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/service-worker/src/module.ts | 4 +--- packages/service-worker/src/provider.ts | 4 ---- packages/service-worker/src/push.ts | 2 +- packages/service-worker/src/update.ts | 2 +- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/service-worker/src/module.ts b/packages/service-worker/src/module.ts index 101fcc4cc13..0d59e2f09af 100644 --- a/packages/service-worker/src/module.ts +++ b/packages/service-worker/src/module.ts @@ -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. diff --git a/packages/service-worker/src/provider.ts b/packages/service-worker/src/provider.ts index ca20ad30635..591433df219 100644 --- a/packages/service-worker/src/provider.ts +++ b/packages/service-worker/src/provider.ts @@ -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(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}, { diff --git a/packages/service-worker/src/push.ts b/packages/service-worker/src/push.ts index a8491a711fb..0c367a3f8ae 100644 --- a/packages/service-worker/src/push.ts +++ b/packages/service-worker/src/push.ts @@ -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. diff --git a/packages/service-worker/src/update.ts b/packages/service-worker/src/update.ts index 5839ced701a..43f4a7d0b15 100644 --- a/packages/service-worker/src/update.ts +++ b/packages/service-worker/src/update.ts @@ -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.