From bcfe18936a1840ea45e4f465de3cc7e62d684b6b Mon Sep 17 00:00:00 2001 From: Bastian Jakobi Date: Thu, 4 Jul 2024 16:15:22 +0200 Subject: [PATCH] fix: add location observer to fix webcomponent routing --- .../lib/utils/webcomponent-bootstrap.utils.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts index 87d87b74..fa632e0c 100644 --- a/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts +++ b/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts @@ -115,9 +115,15 @@ function connectMicroFrontendRouter(injector: Injector) { } function connectRouter(router: Router): void { - const url = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}` - router.navigateByUrl(url) - window.addEventListener('popstate', () => { - router.navigateByUrl(url) - }) + const initialUrl = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}` + router.navigateByUrl(initialUrl) + let lastUrl = location.href + new MutationObserver(() => { + const url = location.href + if (url !== lastUrl) { + lastUrl = url + const routerUrl = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}` + router.navigateByUrl(routerUrl) + } + }).observe(document, { subtree: true, childList: true }) }