Description
[REQUIRED] Describe your environment
- Operating System version: Ubuntu
- Browser version: Google Chrome 91.0.4472.114
- Firebase SDK version: 8.8.0
- Firebase Product: functions, messaging (auth, database, storage, etc)
[REQUIRED] Describe the problem
Steps to reproduce:
- Initialize messaging
- Grant browser notifications permission
- Call
firebase.functions().httpsCallable('my-function')()
- Observe attempted service worker registration (e.g. in devtools)
The first two steps ensure that the following check passes:
firebase-js-sdk/packages/functions/src/context.ts
Lines 88 to 92 in 3d10d33
The third step results in a call to messaging.getToken()
which in turn tries to register the default service worker.
firebase-js-sdk/packages/functions/src/context.ts
Lines 96 to 104 in 3d10d33
Notice that getToken
is invoked without arguments, giving no chance to specify the serviceWorkerRegistration
parameter.
This is undesirable because:
- the app may be using a custom service worker for notifications, registered somewhere else,
- notifications and a service worker may not be needed when the code is run (despite permission granted and library loaded), e.g. during development,
- the call may log an error on the console, due to the default service worker being unavailable, completely unrelated to the triggering code (e.g.
firebase.functions().httpsCallable
), - in general, the call results in unexpected side effects (e.g., an extra HTTP call), depending on heuristic condititions (the
if
check above).
All the above apply in my use case.
Invoking messaging.useServiceWorker
could mitigate the issue, but it does not cover the case where the service worker is not yet available, or no service worker at all is used (and the method is deprecated).
Possible solutions
- add an extra option to
httpsCallable
to specify that the messaging token is not needed in the function context (easy fix?) - avoid calling
getToken
altogether, and only provide the token if a call togetToken
was performed explicitly (may break existing code, if it relies on the token being present in the function call context) - other options?