'Service worker registration failed -- PWA Not working after Universal Prerendering implementation in Angular 9
Error Throwing in the console Log The script has an unsupported MIME type ('text/html').
Service worker registration failed with: DOMException: Failed to register a ServiceWorker for scope ('https://localhost:4000/') with script ('https://localhost:4000/ngsw-worker.js'): The script has an unsupported MIME type ('text/html').
Solution 1:[1]
Try
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production, scope: './', registrationStrategy: 'registerImmediately' })
inside app.module.ts
Solution 2:[2]
In my case service worker was twice registered.
Once from index.html
<script type="text/javascript">
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/serviceworker').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
console.log('ServiceWorker registration failed: ', err);
});
}
Second time by
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
inside app.module.ts
//ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
Solved my problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | canbax |
| Solution 2 | Carl S. |
