填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

SwRegistrationOptions

Token that can be used to provide options for ServiceWorkerModule outside of ServiceWorkerModule.register().

查看"说明"...

      
      abstract class SwRegistrationOptions {
  enabled?: boolean
  scope?: string
  registrationStrategy?: string | (() => Observable<unknown>)
}
    

说明

You can use this token to define a provider that generates the registration options at runtime, for example via a function call:

import {NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {ServiceWorkerModule, SwRegistrationOptions} from '@angular/service-worker'; /* . . . */ @NgModule({ /* . . . */ imports: [ BrowserModule, ServiceWorkerModule.register('ngsw-worker.js'), ], providers: [ { provide: SwRegistrationOptions, useFactory: () => ({enabled: location.search.includes('sw=true')}), }, ], }) export class AppModule { }
app.module.ts
      
      import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {ServiceWorkerModule, SwRegistrationOptions} from '@angular/service-worker';
/* . . . */

@NgModule({
/* . . . */
  imports: [
    BrowserModule,
    ServiceWorkerModule.register('ngsw-worker.js'),
  ],
  providers: [
    {
      provide: SwRegistrationOptions,
      useFactory: () => ({enabled: location.search.includes('sw=true')}),
    },
  ],
})
export class AppModule {
}
    

属性

属性说明
enabled?: boolean

Whether the ServiceWorker will be registered and the related services (such as SwPush and SwUpdate) will attempt to communicate and interact with it.

Default: true

scope?: string

A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can control. It will be used when calling ServiceWorkerContainer#register().

registrationStrategy?: string | (() => Observable<unknown>)

Defines the ServiceWorker registration strategy, which determines when it will be registered with the browser.

The default behavior of registering once the application stabilizes (i.e. as soon as there are no pending micro- and macro-tasks), is designed register the ServiceWorker as soon as possible but without affecting the application's first time load.

Still, there might be cases where you want more control over when the ServiceWorker is registered (e.g. there might be a long-running timeout or polling interval, preventing the app to stabilize). The available option are:

  • registerWhenStable: Register as soon as the application stabilizes (no pending micro-/macro-tasks).
  • registerImmediately: Register immediately.
  • registerWithDelay:<timeout>: Register with a delay of <timeout> milliseconds. For example, use registerWithDelay:5000 to register the ServiceWorker after 5 seconds. If <timeout> is omitted, is defaults to 0, which will register the ServiceWorker as soon as possible but still asynchronously, once all pending micro-tasks are completed.
  • An Observable factory function: A function that returns an Observable. The function will be used at runtime to obtain and subscribe to the Observable and the ServiceWorker will be registered as soon as the first value is emitted.

Default: 'registerWhenStable'