0
votes

I am working on laravel progressive web application . I initiate the service worker in app.js . My service worker file is in ./public directoy . its name is service-worker.js.

but the problem is when i run the app in localhost its showing the folling error in console.

"uncaught (in promise) DOMException: Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/plain')."

So how can i solve this please tell me. in app.js i included below code:

if ('serviceWorker' in navigator)
{
    window.addEventListener('load', function() {
                 navigator.serviceWorker.register('/service-worker.js');
    });
}
2

2 Answers

0
votes

If you are using nginx to serve your project then add this to server block this may solve the issue :

location ~ \.js {
     add_header  Content-Type    application/x-javascript;
}

Else if you are using apache then put this in httpd.conf (also ensure you have mod_mime) :

AddType text/javascript .js

This may solve your issue, generally if you don't include mime setups in your server setup you may face these issues. i suggest check your server setup first.

0
votes

Check your netwrok requests, this error shows up when you are not loading the service-worker.js file correctly.

Make sure that you can really reach the service-worker.js file by the URL you are requesting