From 49dcf84f957690c040d5a0d0869fae234aed883e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marina=20A=C3=ADsa?= Date: Tue, 25 Nov 2025 17:54:18 +0000 Subject: [PATCH] Replace window.location.href with window.location.pathname In the use cases of navigators with multiple top-level root, we want to make sure that queries don't interfere with the election of the top-level root node when extracting the root node. Before we were reading the window.location.href, which includes queries. Now, by using the window.location.pathname, we ignore the queries, which makes possible to compare with the node.path value to find which it's the correct top-level root. Example: URL: 'http://localhost:8080/documentation/foo?language=objc' Navigator path: '/documentation/foo' By using `window.location.pathname` we ignore `?language=objc` and we can compare `/documentation/foo` with `/documentation/foo` --- src/utils/navigatorData.js | 2 +- tests/unit/utils/navigatorData.spec.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/navigatorData.js b/src/utils/navigatorData.js index 3ef902063..9ddf6baf6 100644 --- a/src/utils/navigatorData.js +++ b/src/utils/navigatorData.js @@ -183,7 +183,7 @@ function extractRootNode(data) { // the URL in situations where the renderer is being hosted at some path // prefix const rootPathPattern = /(\/documentation\/[^/]+)/; - const rootPath = window.location.href.match(rootPathPattern)?.[1] ?? ''; + const rootPath = window.location.pathname.match(rootPathPattern)?.[1] ?? ''; // most of the time, it is expected that `data` always has a single item // that represents the top-level root node of the navigation tree // diff --git a/tests/unit/utils/navigatorData.spec.js b/tests/unit/utils/navigatorData.spec.js index 014b1c83d..050e6d87d 100644 --- a/tests/unit/utils/navigatorData.spec.js +++ b/tests/unit/utils/navigatorData.spec.js @@ -373,7 +373,7 @@ describe('when multiple top-level children are provided', () => { describe('flattenNavigationIndex', () => { it('prefers the root child with the same url path prefix', () => { Object.defineProperty(window, 'location', { - value: { href: 'http://localhost/documentation/b/b42' }, + value: new URL('http://localhost/documentation/b/b42?language=objc'), }); // use first root node if only one is provided @@ -409,7 +409,7 @@ describe('when multiple top-level children are provided', () => { describe('extractTechnologyProps', () => { it('prefers the root child with the same url path prefix', () => { Object.defineProperty(window, 'location', { - value: { href: 'http://localhost/documentation/b/b42' }, + value: new URL('http://localhost/documentation/b/b42?language=objc'), }); // use first root node if only one is provided