fix: Issue with trailing / in the left menu script#944
Conversation
Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
There was a problem hiding this comment.
Code Review
This pull request updates the active menu item detection script to ignore trailing slashes when comparing URLs. The review feedback suggests optimizing this check by utilizing native HTMLAnchorElement properties (origin and pathname) instead of instantiating a new URL object inside a try-catch block, which also prevents potential false positives with external links.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| var hasActive = Array.from(wrapperUl.querySelectorAll('a')).some(function (a) { | ||
| try { return new URL(a.href).pathname === window.location.pathname; } catch (e) { return false; } | ||
| try { return new URL(a.href).pathname.replace(/\/$/, '') === currentPath; } catch (e) { return false; } | ||
| }); |
There was a problem hiding this comment.
Using new URL(a.href) inside a try-catch block for every link is inefficient and unnecessary because HTMLAnchorElement already provides native pathname and origin properties. Additionally, comparing only the pathname can lead to false positives if there are external links in the menu that share the same path (e.g., an external link to https://github.com/some-path when the current site path is /some-path).
We can simplify the code, improve performance, and prevent potential false positives by directly comparing a.origin and a.pathname.
| var hasActive = Array.from(wrapperUl.querySelectorAll('a')).some(function (a) { | |
| try { return new URL(a.href).pathname === window.location.pathname; } catch (e) { return false; } | |
| try { return new URL(a.href).pathname.replace(/\/$/, '') === currentPath; } catch (e) { return false; } | |
| }); | |
| var hasActive = Array.from(wrapperUl.querySelectorAll('a')).some(function (a) { | |
| return a.origin === window.location.origin && a.pathname.replace(/\/$/, '') === currentPath; | |
| }); |
No description provided.