fix(techdocs): avoid rerender when clicking on anchor links in same page (#26938)

Signed-off-by: Thomas Cardonne <thomas.cardonne@adevinta.com>
This commit is contained in:
Thomas Cardonne
2024-10-15 21:34:13 +02:00
committed by GitHub
parent e0613e9957
commit 605bdc01cf
2 changed files with 17 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-techdocs': patch
---
Avoid page re-rendering when clicking on anchor links in the same documentation page.
@@ -208,7 +208,18 @@ export const useTechDocsReaderDom = (
if (modifierActive) {
window.open(url, '_blank');
} else {
navigate(url);
// If it's in a different page, we navigate to it
if (window.location.pathname !== parsedUrl.pathname) {
navigate(url);
} else {
// If it's in the same page we avoid using navigate that causes
// the page to rerender.
window.history.pushState(
null,
document.title,
parsedUrl.hash,
);
}
// Scroll to hash if it's on the current page
transformedElement
?.querySelector(`[id="${parsedUrl.hash.slice(1)}"]`)