Skip to content

Browser Back Button not working #451

Open
@davidmrhodes

Description

@davidmrhodes

I am have problems getting the browser back and forward button working in and MFE. I think there is a bug in the
following function in this file:

https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf-tools/src/lib/web-components/router-utils.ts

export function connectRouter(router: Router, useHash = false): void {
let url: string;
if (!useHash) {
url = ${location.pathname.substring(1)}${location.search};
router.navigateByUrl(url);
window.addEventListener('popstate', () => {
router.navigateByUrl(url);
});
} else {
url = ${location.hash.substring(1)}${location.search};
router.navigateByUrl(url);
window.addEventListener('hashchange', () => {
router.navigateByUrl(url);
});
}
}

When I change it to this:

export function connectRouter(router: Router, useHash = false): void {

let url: string;
if (!useHash) {
    url = `${location.pathname.substring(1)}${location.search}`;
    router.navigateByUrl(url);
    window.addEventListener('popstate', () => {
        url = `${location.hash.substring(1)}${location.search}`;
        router.navigateByUrl(url);
    });
}
else {
    url = `${location.hash.substring(1)}${location.search}`;
    router.navigateByUrl(url);
    window.addEventListener('hashchange', () => {
        url = `${location.hash.substring(1)}${location.search}`;
        router.navigateByUrl(url);
    });
}

}

Subtle change but url should be recalculated in the addEventListener.

I think this change would better match with the code in the article:

https://www.angulararchitects.io/en/blog/multi-framework-and-version-micro-frontends-with-module-federation-the-good-the-bad-the-ugly/ under the section "Several Routers must Work Together"

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