I have a react-create-app application, in which I used firebase for pushnotification. Recently I upgraded firebase from 8.* to 9.*. While getting push token, it throws the following error:
FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('https://9fd5-49-204-137-92.ngrok.io/firebase-cloud-messaging-push-scope') with script ('https://9fd5-49-204-137-92.ngrok.io/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html'). (messaging/failed-service-worker-registration).
In firebase.js, I have the following code set, which throws the above mentioned error
import { initializeApp } from "firebase/app"import { getToken, getMessaging, onMessage } from "firebase/messaging";
const message_key = "key"var firebaseConfig = {
apiKey: "apikey",
authDomain: "authdomain",
projectId: "project-app",
storageBucket: "******.com",
messagingSenderId: "id",
appId: "appid",
measurementId: "mid"
};
const firebaseApp = initializeApp(firebaseConfig);
let messaging = getMessaging(firebaseApp);
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register(process.env.PUBLIC_URL + "/firebase-messaging-sw.js")
.then(function(registration) {
console.log("Registration successful, scope is:", registration);
getToken({messaging, vapidKey: message_key})
.then((currentToken) => {
if (currentToken) {
console.log('current token for client: ', currentToken);
} else {
console.log('No registration token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
})
.catch(function(err) {
console.log("Service worker registration failed, error:", err);
});
}}
How can I solve this issue?
This error occurs because the service worker script file has an unsupported MIME type.
Here are some things you can try to fix the problem:
Make sure that the file firebase-messaging-sw.js is located in the public directory of your React project. This is where the create-react-app development server expects to find the service worker script file.
Check the file firebase-messaging-sw.js to make sure that it is a valid JavaScript file. There should not be any HTML or other non-JavaScript content in the file.
If you are using a hosting service or a CDN to serve your application, make sure that the service worker script file is being served with the correct MIME type. The MIME type for JavaScript files is application/javascript.
If you are using a tool like webpack or Babel to build your application, make sure that it is configured to correctly process and output the service worker script file.
If none of these suggestions help, you can try to register the service worker manually by calling navigator.serviceWorker.register() and passing in the URL of the service worker script file as the first argument. You may also want to check the browser's developer console for additional error messages or log messages that may give more information about the cause of the problem.