From 29d729184ad0076f67aa78da5586589277b438b1 Mon Sep 17 00:00:00 2001 From: Nicolas Dorseuil Date: Thu, 25 Sep 2025 20:28:07 +0200 Subject: [PATCH] Enhance navigation tracking by skipping search parameters and external links with modifier keys --- packages/gitbook/src/components/hooks/useHash.tsx | 5 +++++ packages/gitbook/src/components/primitives/Link.tsx | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/gitbook/src/components/hooks/useHash.tsx b/packages/gitbook/src/components/hooks/useHash.tsx index 1c3957ab2e..0468461e99 100644 --- a/packages/gitbook/src/components/hooks/useHash.tsx +++ b/packages/gitbook/src/components/hooks/useHash.tsx @@ -59,6 +59,11 @@ export const NavigationStatusProvider: React.FC = ({ ch }, []); const onNavigationClick = React.useCallback((href: string) => { + // We need to skip it for search like params (i.e. ?ask= or ?q=) because they don't really trigger a navigation + // Search may trigger a navigation whenn clicking on the ask ai for example, this is not something we want to track here + if (href.startsWith('?') || href.startsWith('#')) { + return; + } const url = new URL( href, typeof window !== 'undefined' ? window.location.origin : 'http://localhost' diff --git a/packages/gitbook/src/components/primitives/Link.tsx b/packages/gitbook/src/components/primitives/Link.tsx index 722b7fdb80..c21dd0cb38 100644 --- a/packages/gitbook/src/components/primitives/Link.tsx +++ b/packages/gitbook/src/components/primitives/Link.tsx @@ -80,7 +80,8 @@ export const Link = React.forwardRef(function Link( const onClick = (event: React.MouseEvent) => { const isExternalWithOrigin = isExternalLink(href, window.location.origin); - if (!isExternal) { + // Only trigger navigation context for internal links without modifier keys (i.e. open in new tab). + if (!isExternal && !event.ctrlKey && !event.metaKey) { onNavigationClick(href); }