I'm trying to add service workers to our existing angular (5.2, CLI 1.7.1) application. I did everything I was suppose to:
- I created ngsw-config.json
- I run the the
ng set apps.0.serviceWorker=truecommand so I have"serviceWorker": true,in angular-cli.json. - I have installed
"@angular/service-worker": "5.2.9" I added to app.module.ts:
import { ServiceWorkerModule } from '@angular/service-worker'; imports: [ ... ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }), ]
I even tried to add this piece of code (which I found as a solution somewhere) to main.ts
platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('/ngsw-worker.js');
}
}).catch(err => console.log(err));
But I got this error:
Uncaught (in promise) DOMException: Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html').
ERROR Error: Uncaught (in promise): SecurityError: Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html'). at resolvePromise (polyfills.3e9ea997ddae46e16c09.bundle.js:1)
In my own personal small app, I did the same and it worked without any errors. The application where we have this error is pretty huge. With a lot of third party libraries. I've read that there can be a problem with them.
Can you help me please?
Also, I was checking my own application to find possible differences, which I didn't but I looked at network tab in devtools and I've noticed that the files which should be cached by service worker are always loaded. Is it a correct behaviour please?