
Since abb36e3cb, we no longer rely on the cli to set up ServiceWorker, but do it manually as part of `yarn build`. When using `ng serve`, registering the ServiceWorker fails, because we haven't created `ngsw-manifest.json` nor copied `worker-basic.min.js` into dist. This commit works around this, by only registering the service worker in production mode (which is what the cli does too). Caveat: It is not possible to enable ServiceWorker with `ng serve`/`yarn start` and using the `--prod` flag will try to register it, but fail because the necessary files (`ngsw-manifest.json` and `worker-basic.min.js`) will not be available. (As a work-around, you can use `yarn build` and serve the files in `dist/` with `yarn http-server -- dist -p 4200`.)
19 lines
642 B
TypeScript
19 lines
642 B
TypeScript
import { enableProdMode, ApplicationRef } from '@angular/core';
|
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
|
|
|
import { AppModule } from './app/app.module';
|
|
import { environment } from './environments/environment';
|
|
|
|
if (environment.production) {
|
|
enableProdMode();
|
|
}
|
|
|
|
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
|
|
if (environment.production && 'serviceWorker' in (navigator as any)) {
|
|
const appRef: ApplicationRef = ref.injector.get(ApplicationRef);
|
|
appRef.isStable.first().subscribe(() => {
|
|
(navigator as any).serviceWorker.register('/worker-basic.min.js');
|
|
});
|
|
}
|
|
});
|