UNPKG

16.1 kBTypeScriptView Raw
1/**
2 * @license Angular v18.2.2
3 * (c) 2010-2024 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7
8import { EnvironmentProviders } from '@angular/core';
9import * as i0 from '@angular/core';
10import { ModuleWithProviders } from '@angular/core';
11import { Observable } from 'rxjs';
12
13/**
14 * @publicApi
15 */
16declare class NgswCommChannel {
17 private serviceWorker;
18 readonly worker: Observable<ServiceWorker>;
19 readonly registration: Observable<ServiceWorkerRegistration>;
20 readonly events: Observable<TypedEvent>;
21 constructor(serviceWorker: ServiceWorkerContainer | undefined);
22 postMessage(action: string, payload: Object): Promise<void>;
23 postMessageWithOperation(type: string, payload: Object, operationNonce: number): Promise<boolean>;
24 generateNonce(): number;
25 eventsOfType<T extends TypedEvent>(type: T['type'] | T['type'][]): Observable<T>;
26 nextEventOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
27 waitForOperationCompleted(nonce: number): Promise<boolean>;
28 get isEnabled(): boolean;
29}
30
31/**
32 * An event emitted when the service worker has checked the version of the app on the server and it
33 * didn't find a new version that it doesn't have already downloaded.
34 *
35 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
36 *
37 * @publicApi
38 */
39export declare interface NoNewVersionDetectedEvent {
40 type: 'NO_NEW_VERSION_DETECTED';
41 version: {
42 hash: string;
43 appData?: Object;
44 };
45}
46
47/**
48 * @publicApi
49 *
50 * Sets up providers to register the given Angular Service Worker script.
51 *
52 * If `enabled` is set to `false` in the given options, the module will behave as if service
53 * workers are not supported by the browser, and the service worker will not be registered.
54 *
55 * Example usage:
56 * ```ts
57 * bootstrapApplication(AppComponent, {
58 * providers: [
59 * provideServiceWorker('ngsw-worker.js')
60 * ],
61 * });
62 * ```
63 */
64export declare function provideServiceWorker(script: string, options?: SwRegistrationOptions): EnvironmentProviders;
65
66/**
67 * @publicApi
68 */
69export declare class ServiceWorkerModule {
70 /**
71 * Register the given Angular Service Worker script.
72 *
73 * If `enabled` is set to `false` in the given options, the module will behave as if service
74 * workers are not supported by the browser, and the service worker will not be registered.
75 */
76 static register(script: string, options?: SwRegistrationOptions): ModuleWithProviders<ServiceWorkerModule>;
77 static ɵfac: i0.ɵɵFactoryDeclaration<ServiceWorkerModule, never>;
78 static ɵmod: i0.ɵɵNgModuleDeclaration<ServiceWorkerModule, never, never, never>;
79 static ɵinj: i0.ɵɵInjectorDeclaration<ServiceWorkerModule>;
80}
81
82/**
83 * Subscribe and listen to
84 * [Web Push
85 * Notifications](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices) through
86 * Angular Service Worker.
87 *
88 * @usageNotes
89 *
90 * You can inject a `SwPush` instance into any component or service
91 * as a dependency.
92 *
93 * <code-example path="service-worker/push/module.ts" region="inject-sw-push"
94 * header="app.component.ts"></code-example>
95 *
96 * To subscribe, call `SwPush.requestSubscription()`, which asks the user for permission.
97 * The call returns a `Promise` with a new
98 * [`PushSubscription`](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
99 * instance.
100 *
101 * <code-example path="service-worker/push/module.ts" region="subscribe-to-push"
102 * header="app.component.ts"></code-example>
103 *
104 * A request is rejected if the user denies permission, or if the browser
105 * blocks or does not support the Push API or ServiceWorkers.
106 * Check `SwPush.isEnabled` to confirm status.
107 *
108 * Invoke Push Notifications by pushing a message with the following payload.
109 *
110 * ```ts
111 * {
112 * "notification": {
113 * "actions": NotificationAction[],
114 * "badge": USVString,
115 * "body": DOMString,
116 * "data": any,
117 * "dir": "auto"|"ltr"|"rtl",
118 * "icon": USVString,
119 * "image": USVString,
120 * "lang": DOMString,
121 * "renotify": boolean,
122 * "requireInteraction": boolean,
123 * "silent": boolean,
124 * "tag": DOMString,
125 * "timestamp": DOMTimeStamp,
126 * "title": DOMString,
127 * "vibrate": number[]
128 * }
129 * }
130 * ```
131 *
132 * Only `title` is required. See `Notification`
133 * [instance
134 * properties](https://developer.mozilla.org/en-US/docs/Web/API/Notification#Instance_properties).
135 *
136 * While the subscription is active, Service Worker listens for
137 * [PushEvent](https://developer.mozilla.org/en-US/docs/Web/API/PushEvent)
138 * occurrences and creates
139 * [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification)
140 * instances in response.
141 *
142 * Unsubscribe using `SwPush.unsubscribe()`.
143 *
144 * An application can subscribe to `SwPush.notificationClicks` observable to be notified when a user
145 * clicks on a notification. For example:
146 *
147 * <code-example path="service-worker/push/module.ts" region="subscribe-to-notification-clicks"
148 * header="app.component.ts"></code-example>
149 *
150 * You can read more on handling notification clicks in the [Service worker notifications
151 * guide](ecosystem/service-workers/push-notifications).
152 *
153 * @see [Push Notifications](https://developers.google.com/web/fundamentals/codelabs/push-notifications/)
154 * @see [Angular Push Notifications](https://blog.angular-university.io/angular-push-notifications/)
155 * @see [MDN: Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
156 * @see [MDN: Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API)
157 * @see [MDN: Web Push API Notifications best practices](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices)
158 *
159 * @publicApi
160 */
161export declare class SwPush {
162 private sw;
163 /**
164 * Emits the payloads of the received push notification messages.
165 */
166 readonly messages: Observable<object>;
167 /**
168 * Emits the payloads of the received push notification messages as well as the action the user
169 * interacted with. If no action was used the `action` property contains an empty string `''`.
170 *
171 * Note that the `notification` property does **not** contain a
172 * [Notification][Mozilla Notification] object but rather a
173 * [NotificationOptions](https://notifications.spec.whatwg.org/#dictdef-notificationoptions)
174 * object that also includes the `title` of the [Notification][Mozilla Notification] object.
175 *
176 * [Mozilla Notification]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
177 */
178 readonly notificationClicks: Observable<{
179 action: string;
180 notification: NotificationOptions & {
181 title: string;
182 };
183 }>;
184 /**
185 * Emits the currently active
186 * [PushSubscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
187 * associated to the Service Worker registration or `null` if there is no subscription.
188 */
189 readonly subscription: Observable<PushSubscription | null>;
190 /**
191 * True if the Service Worker is enabled (supported by the browser and enabled via
192 * `ServiceWorkerModule`).
193 */
194 get isEnabled(): boolean;
195 private pushManager;
196 private subscriptionChanges;
197 constructor(sw: NgswCommChannel);
198 /**
199 * Subscribes to Web Push Notifications,
200 * after requesting and receiving user permission.
201 *
202 * @param options An object containing the `serverPublicKey` string.
203 * @returns A Promise that resolves to the new subscription object.
204 */
205 requestSubscription(options: {
206 serverPublicKey: string;
207 }): Promise<PushSubscription>;
208 /**
209 * Unsubscribes from Service Worker push notifications.
210 *
211 * @returns A Promise that is resolved when the operation succeeds, or is rejected if there is no
212 * active subscription or the unsubscribe operation fails.
213 */
214 unsubscribe(): Promise<void>;
215 private decodeBase64;
216 static ɵfac: i0.ɵɵFactoryDeclaration<SwPush, never>;
217 static ɵprov: i0.ɵɵInjectableDeclaration<SwPush>;
218}
219
220/**
221 * Token that can be used to provide options for `ServiceWorkerModule` outside of
222 * `ServiceWorkerModule.register()`.
223 *
224 * You can use this token to define a provider that generates the registration options at runtime,
225 * for example via a function call:
226 *
227 * {@example service-worker/registration-options/module.ts region="registration-options"
228 * header="app.module.ts"}
229 *
230 * @publicApi
231 */
232export declare abstract class SwRegistrationOptions {
233 /**
234 * Whether the ServiceWorker will be registered and the related services (such as `SwPush` and
235 * `SwUpdate`) will attempt to communicate and interact with it.
236 *
237 * Default: true
238 */
239 enabled?: boolean;
240 /**
241 * A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
242 * control. It will be used when calling
243 * [ServiceWorkerContainer#register()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
244 */
245 scope?: string;
246 /**
247 * Defines the ServiceWorker registration strategy, which determines when it will be registered
248 * with the browser.
249 *
250 * The default behavior of registering once the application stabilizes (i.e. as soon as there are
251 * no pending micro- and macro-tasks) is designed to register the ServiceWorker as soon as
252 * possible but without affecting the application's first time load.
253 *
254 * Still, there might be cases where you want more control over when the ServiceWorker is
255 * registered (for example, there might be a long-running timeout or polling interval, preventing
256 * the app from stabilizing). The available option are:
257 *
258 * - `registerWhenStable:<timeout>`: Register as soon as the application stabilizes (no pending
259 * micro-/macro-tasks) but no later than `<timeout>` milliseconds. If the app hasn't
260 * stabilized after `<timeout>` milliseconds (for example, due to a recurrent asynchronous
261 * task), the ServiceWorker will be registered anyway.
262 * If `<timeout>` is omitted, the ServiceWorker will only be registered once the app
263 * stabilizes.
264 * - `registerImmediately`: Register immediately.
265 * - `registerWithDelay:<timeout>`: Register with a delay of `<timeout>` milliseconds. For
266 * example, use `registerWithDelay:5000` to register the ServiceWorker after 5 seconds. If
267 * `<timeout>` is omitted, is defaults to `0`, which will register the ServiceWorker as soon
268 * as possible but still asynchronously, once all pending micro-tasks are completed.
269 * - An Observable factory function: A function that returns an `Observable`.
270 * The function will be used at runtime to obtain and subscribe to the `Observable` and the
271 * ServiceWorker will be registered as soon as the first value is emitted.
272 *
273 * Default: 'registerWhenStable:30000'
274 */
275 registrationStrategy?: string | (() => Observable<unknown>);
276}
277
278/**
279 * Subscribe to update notifications from the Service Worker, trigger update
280 * checks, and forcibly activate updates.
281 *
282 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
283 *
284 * @publicApi
285 */
286export declare class SwUpdate {
287 private sw;
288 /**
289 * Emits a `VersionDetectedEvent` event whenever a new version is detected on the server.
290 *
291 * Emits a `VersionInstallationFailedEvent` event whenever checking for or downloading a new
292 * version fails.
293 *
294 * Emits a `VersionReadyEvent` event whenever a new version has been downloaded and is ready for
295 * activation.
296 */
297 readonly versionUpdates: Observable<VersionEvent>;
298 /**
299 * Emits an `UnrecoverableStateEvent` event whenever the version of the app used by the service
300 * worker to serve this client is in a broken state that cannot be recovered from without a full
301 * page reload.
302 */
303 readonly unrecoverable: Observable<UnrecoverableStateEvent>;
304 /**
305 * True if the Service Worker is enabled (supported by the browser and enabled via
306 * `ServiceWorkerModule`).
307 */
308 get isEnabled(): boolean;
309 constructor(sw: NgswCommChannel);
310 /**
311 * Checks for an update and waits until the new version is downloaded from the server and ready
312 * for activation.
313 *
314 * @returns a promise that
315 * - resolves to `true` if a new version was found and is ready to be activated.
316 * - resolves to `false` if no new version was found
317 * - rejects if any error occurs
318 */
319 checkForUpdate(): Promise<boolean>;
320 /**
321 * Updates the current client (i.e. browser tab) to the latest version that is ready for
322 * activation.
323 *
324 * In most cases, you should not use this method and instead should update a client by reloading
325 * the page.
326 *
327 * <div class="alert is-important">
328 *
329 * Updating a client without reloading can easily result in a broken application due to a version
330 * mismatch between the application shell and other page resources,
331 * such as lazy-loaded chunks, whose filenames may change between
332 * versions.
333 *
334 * Only use this method, if you are certain it is safe for your specific use case.
335 *
336 * </div>
337 *
338 * @returns a promise that
339 * - resolves to `true` if an update was activated successfully
340 * - resolves to `false` if no update was available (for example, the client was already on the
341 * latest version).
342 * - rejects if any error occurs
343 */
344 activateUpdate(): Promise<boolean>;
345 static ɵfac: i0.ɵɵFactoryDeclaration<SwUpdate, never>;
346 static ɵprov: i0.ɵɵInjectableDeclaration<SwUpdate>;
347}
348
349declare interface TypedEvent {
350 type: string;
351}
352
353/**
354 * An event emitted when the version of the app used by the service worker to serve this client is
355 * in a broken state that cannot be recovered from and a full page reload is required.
356 *
357 * For example, the service worker may not be able to retrieve a required resource, neither from the
358 * cache nor from the server. This could happen if a new version is deployed to the server and the
359 * service worker cache has been partially cleaned by the browser, removing some files of a previous
360 * app version but not all.
361 *
362 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
363 *
364 * @publicApi
365 */
366export declare interface UnrecoverableStateEvent {
367 type: 'UNRECOVERABLE_STATE';
368 reason: string;
369}
370
371/**
372 * An event emitted when the service worker has detected a new version of the app on the server and
373 * is about to start downloading it.
374 *
375 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
376 *
377 * @publicApi
378 */
379export declare interface VersionDetectedEvent {
380 type: 'VERSION_DETECTED';
381 version: {
382 hash: string;
383 appData?: object;
384 };
385}
386
387/**
388 * A union of all event types that can be emitted by
389 * {@link SwUpdate#versionUpdates}.
390 *
391 * @publicApi
392 */
393export declare type VersionEvent = VersionDetectedEvent | VersionInstallationFailedEvent | VersionReadyEvent | NoNewVersionDetectedEvent;
394
395/**
396 * An event emitted when the installation of a new version failed.
397 * It may be used for logging/monitoring purposes.
398 *
399 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
400 *
401 * @publicApi
402 */
403export declare interface VersionInstallationFailedEvent {
404 type: 'VERSION_INSTALLATION_FAILED';
405 version: {
406 hash: string;
407 appData?: object;
408 };
409 error: string;
410}
411
412/**
413 * An event emitted when a new version of the app is available.
414 *
415 * @see {@link ecosystem/service-workers/communications Service worker communication guide}
416 *
417 * @publicApi
418 */
419export declare interface VersionReadyEvent {
420 type: 'VERSION_READY';
421 currentVersion: {
422 hash: string;
423 appData?: object;
424 };
425 latestVersion: {
426 hash: string;
427 appData?: object;
428 };
429}
430
431export { }
432
\No newline at end of file