Why not directly pass firebase config into the service worker file, instead of passing it via query strings? #8693
|
In the firebase nextjs codelab, a service worker is used, and the config is passed via query params. Why can't it be passed as code in the service worker file, since it will anyway be included in the build? Does this have something to do with SW lifecycle? Also is it possible to use firebase with next js without service worker? |
Replies: 1 comment
|
You can initialize Firebase directly inside the service-worker file. The current FCM documentation shows exactly that: Passing config through query parameters is therefore not a Firebase or service-worker lifecycle requirement. It is a deployment choice that lets an unbundled/static worker receive environment-specific values at registration time. A file placed in Next.js Firebase can also be used with Next.js without a service worker for Authentication, Firestore, Storage, and foreground FCM handling. A worker is needed when you want Firebase Cloud Messaging to handle messages while the page is in the background or closed. You can register your own worker and pass its registration to Official reference and examples: Receive messages in web apps and Add Firebase to a JavaScript project. |
You can initialize Firebase directly inside the service-worker file. The current FCM documentation shows exactly that:
initializeApp({...firebaseConfig})in the worker.Passing config through query parameters is therefore not a Firebase or service-worker lifecycle requirement. It is a deployment choice that lets an unbundled/static worker receive environment-specific values at registration time. A file placed in Next.js
public/is served as-is, so it does not automatically share imports or build-time variables with your application bundle. If you bundle the worker, importing/injecting the config at build time is another valid approach.Firebase can also be used with Next.js without a serv…