-
I am having a heck of a time finding documentation anywhere for how to go from this: // Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
databaseURL: 'https://project-id.firebaseio.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
measurementId: 'G-measurement-id',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging(); to this: import { initializeApp } from "firebase/app";
import { getMessaging } from "firebase/messaging/sw";
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
const firebaseApp = initializeApp({
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
databaseURL: 'https://project-id.firebaseio.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
measurementId: 'G-measurement-id',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = getMessaging(firebaseApp); I am guessing I'll need to register the service worker with type module, but how do I support that with Webpack? Currently, I am using the 'copy-webpack-plugin' to copy the service worker file to the public path. That works, but now I need to support the import syntax, and scouring the web has not produced many results. The documentation basically just says "we assume you know how to do this". Unfortunately, even Webpack's own documentation is lacking, so it would be nice to have an example to go by. Any help? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 15 replies
-
Here's what I did to build the firebase-messaging-sw.js file with webpack (I'm not sure if this works because I'm having other issues with Firebase, I would be interested in knowing):
|
Beta Was this translation helpful? Give feedback.
Here's what I did to build the firebase-messaging-sw.js file with webpack (I'm not sure if this works because I'm having other issues with Firebase, I would be interested in knowing):
entry
collection for the firebase-messaging-sw.js file. We do this because we want to bundle and output it separately from the rest of the app's files.output.filename
config if needed so that the service worker entry's output file is named correctly. Mine is doing something like this: