DOMException: Failed to register a ServiceWorker: The script does not have a MIME type

Viewed 17892

It is my first time to use service worker. I got an error "The script does not have a MIME type.". Can anyone help with this issue?

The script does not have a MIME type.

Failed to load resource: net::ERR_INSECURE_RESPONSE

Service worker registration failed: DOMException: Failed to register a ServiceWorker: The script does not have a MIME type. register.js:6

register.js

// Make sure sw are supported
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('sw_cached_page.js').then(function(registration) {
        console.log('Service Worker: Registered (Pages)');
    },function(error){
        console.log('Service worker registration failed:', error);
    });
}else{
    console.log('Service workers are not supported.');
}

2 Answers

This looks like your browser couldn't load the file sw_cached_page.js correctly. Check in chrome devtools what your browser actually downloaded.

Check the content-type:

Check the content-type

And check the content:

check the actual content

Please inlcude service worker js file in your html file

<script type="text/javascript" src="/myproject/scripts/common/pushNotifications/sw.js"></script>
navigator.serviceWorker.register('/mproject/scripts/common/pushNotifications/sw.js').then(function(registration) {
  console.log('ServiceWorker registration successful with scope: ', registration.active);
});
Related