Skip to content

Commit b359eb2

Browse files
authored
Replace window.location.href with window.location.pathname (#983)
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`
1 parent c781d37 commit b359eb2

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/utils/navigatorData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function extractRootNode(data) {
183183
// the URL in situations where the renderer is being hosted at some path
184184
// prefix
185185
const rootPathPattern = /(\/documentation\/[^/]+)/;
186-
const rootPath = window.location.href.match(rootPathPattern)?.[1] ?? '';
186+
const rootPath = window.location.pathname.match(rootPathPattern)?.[1] ?? '';
187187
// most of the time, it is expected that `data` always has a single item
188188
// that represents the top-level root node of the navigation tree
189189
//

tests/unit/utils/navigatorData.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe('when multiple top-level children are provided', () => {
373373
describe('flattenNavigationIndex', () => {
374374
it('prefers the root child with the same url path prefix', () => {
375375
Object.defineProperty(window, 'location', {
376-
value: { href: 'http://localhost/documentation/b/b42' },
376+
value: new URL('http://localhost/documentation/b/b42?language=objc'),
377377
});
378378

379379
// use first root node if only one is provided
@@ -409,7 +409,7 @@ describe('when multiple top-level children are provided', () => {
409409
describe('extractTechnologyProps', () => {
410410
it('prefers the root child with the same url path prefix', () => {
411411
Object.defineProperty(window, 'location', {
412-
value: { href: 'http://localhost/documentation/b/b42' },
412+
value: new URL('http://localhost/documentation/b/b42?language=objc'),
413413
});
414414

415415
// use first root node if only one is provided

0 commit comments

Comments
 (0)