Skip to content

window.location.assign and <a href''> stop triggering fetch after SW registration and Firebase Auth login #4

@maximyudayev

Description

@maximyudayev

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:

// 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

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 <a href"/en"> 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions