FirebaseExtended / FirebaseExtended/firebase-auth-service-worker-sessions
window.location.assign and <a href''> stop triggering fetch after SW registration and Firebase Auth login
- Dominant language
- JavaScript
- Stars
- 90
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
### Introduced Code Changes:
I replaced the current client-side `getIdToken()` method with the following block that registers an onIdTokenChanged listener to independently refresh the token inside SW when it's changed or user is logged out without the need to retrieve the token every time a request is sent:
```javascript
// CurrentUser module, provides up-to-date idToken
const CurrentUser = (() => {
let IDToken = '';
firebase.auth().onIdTokenChanged((user) => {
if(user) {
user.getIdToken()
.then(idToken => IDToken = idToken)
.catch(error => console.log(error));
} else {
IDToken = '';
}
});
// Public method
return {
getIdToken: () => { return Promise.resolve(IDToken); }
};
})();
```
I also then changed the `event.respondWith()` to this since the `.getIdToken()` always resolves and `.then()` no longer needs second argument for onRejected event
```javascript
event.respondWith(CurrentUser.getIdToken().then(requestProcessor));
```
### Observed Issue:
When registering the ServiceWorker, after a user logs in with Firebase Auth, `window.location.assign()` and anchor tag navigation `` fail to cause FetchEvent event. However, manual change of URL results in FetchEvent and proper navigation (token injection also takes place correctly).
### Background:
When ServiceWorker is registered, but before login with Firebase Auth, navigation with anchor tags and `window.location.assign()` works correctly and as expected.
### Assumptions:
Fetch spec for ServiceWorker mentions that `mode: 'navigate'` changes to `mode: 'same-origin'` automatically, if the `sw.js` 'fetch' event code is changed to inherit the mode of the original request, results in an error:
`Cannot construct a Request with a Request whose mode is 'navigate' and a non-empty RequestInit`
As such, I assume it may have something to do with the mode of the request as the behavior occurs only when creating `new Request(input, init)`
## Any thoughts what may be causing this or how to fix it?
Contributor guide
Assessment
This issue has not been assessed yet.