Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirebaseError: Messaging: We are unable to register the default service worker #324

Open
AlanGreyjoy opened this issue Jun 11, 2024 · 0 comments

Comments

@AlanGreyjoy
Copy link

When using the useToken hook, the following error occurs and I can't figure out how to fix it.

FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('https://localhost:5173/firebase-cloud-messaging-push-scope') with script ('https://localhost:5173/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html'). (messaging/failed-service-worker-registration).
    at registerDefaultSw (chunk-TJJQGBVO.js?v=57a89067:578:25)
    at async updateSwReg (chunk-TJJQGBVO.js?v=57a89067:585:5)
    at async getToken$1 (chunk-TJJQGBVO.js?v=57a89067:622:3)

FirebaseContext

import React, { createContext, useContext, useEffect } from 'react'
import firebase from 'firebase/compat/app'
import 'firebase/compat/firestore'
import 'firebase/compat/auth'
import 'firebase/compat/analytics'
import 'firebase/compat/storage'
import 'firebase/compat/messaging'

import { useToken } from 'react-firebase-hooks/messaging'

import firebaseConfig from 'src/configs/firebaseConfig'

firebase.initializeApp(firebaseConfig)

const FirebaseContext = createContext(null)

/**
 * Firebase provider
 * @returns {firebase, auth, firestore, analytics, messaging, storage}
 */
export const useFirebase = () => useContext(FirebaseContext)

const FirebaseProvider = ({ children }) => {
  const auth = firebase.auth()
  const firestore = firebase.firestore()
  const analytics = firebase.analytics()
  const storage = firebase.storage()
  const messaging = firebase.messaging()

  const [token, loading, error] = useToken(firebase.messaging())

  console.log('token', token)
  console.log('loading', loading)
  console.log('error', error)

  const value = {
    firebase,
    auth,
    firestore,
    analytics,
    messaging,
    storage
  }

  return <FirebaseContext.Provider value={value}>{children}</FirebaseContext.Provider>
}

export default FirebaseProvider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant