From 66abff6db8061ba4cb229d7073677f8ed652d533 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Thu, 28 Nov 2024 15:58:02 +0100 Subject: [PATCH] docs(docs-infra): don't use `URL` to check for external links (#58955) For perf reasons, it is more efficient to not rely on creating an `URL` object. Instead we check for the explicit protocol to detect external links. This will also force us to use relative links, so archived versions & next versions navigate on the current version of the site. fixes #58954 PR Close #58955 --- .../external-link/external-link.directive.ts | 3 +-- adev/shared-docs/utils/navigation.utils.ts | 16 +++++++++------- .../secondary-navigation.component.ts | 5 ++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/adev/shared-docs/directives/external-link/external-link.directive.ts b/adev/shared-docs/directives/external-link/external-link.directive.ts index 1694147883c..87aac9620dc 100644 --- a/adev/shared-docs/directives/external-link/external-link.directive.ts +++ b/adev/shared-docs/directives/external-link/external-link.directive.ts @@ -24,7 +24,6 @@ import {WINDOW} from '../../providers/index'; export class ExternalLink implements OnInit { private readonly anchor: ElementRef = inject(ElementRef); private readonly platformId = inject(PLATFORM_ID); - private readonly window = inject(WINDOW); target?: '_blank' | '_self' | '_parent' | '_top' | ''; @@ -37,7 +36,7 @@ export class ExternalLink implements OnInit { return; } - if (isExternalLink(this.anchor.nativeElement.href, this.window.location.origin)) { + if (isExternalLink(this.anchor.nativeElement.href)) { this.target = '_blank'; } } diff --git a/adev/shared-docs/utils/navigation.utils.ts b/adev/shared-docs/utils/navigation.utils.ts index 853ca5fad3c..17eb62f5c5a 100644 --- a/adev/shared-docs/utils/navigation.utils.ts +++ b/adev/shared-docs/utils/navigation.utils.ts @@ -78,16 +78,18 @@ export const findNavigationItem = ( return result; }; -export const isExternalLink = (link: string, windowOrigin: string) => - new URL(link).origin !== windowOrigin; +/** + * For perf reasons, we only don't rely on creating a new Url object and comparing the origins + */ +export function isExternalLink(link: string): boolean { + return link.startsWith('http://') || link.startsWith('https://'); +} -export const markExternalLinks = (item: NavigationItem, origin: string): void => { +export function markExternalLinks(item: NavigationItem): void { if (item.path) { - try { - item.isExternal = isExternalLink(item.path, origin); - } catch (err) {} + item.isExternal = isExternalLink(item.path); } -}; +} export const mapNavigationItemsToRoutes = ( navigationItems: NavigationItem[], diff --git a/adev/src/app/core/layout/secondary-navigation/secondary-navigation.component.ts b/adev/src/app/core/layout/secondary-navigation/secondary-navigation.component.ts index fec8fb3fb87..7f7517cbe53 100644 --- a/adev/src/app/core/layout/secondary-navigation/secondary-navigation.component.ts +++ b/adev/src/app/core/layout/secondary-navigation/secondary-navigation.component.ts @@ -62,7 +62,6 @@ export class SecondaryNavigation implements OnInit { private readonly navigationState = inject(NavigationState); private readonly platformId = inject(PLATFORM_ID); private readonly router = inject(Router); - private readonly window = inject(WINDOW); readonly isSecondaryNavVisible = this.navigationState.isMobileNavVisible; readonly primaryActiveRouteItem = this.navigationState.primaryActiveRouteItem; @@ -83,10 +82,10 @@ export class SecondaryNavigation implements OnInit { private readonly routeMap: Record = { [PagePrefix.REFERENCE]: getNavigationItemsTree(SUB_NAVIGATION_DATA.reference, (tree) => - markExternalLinks(tree, this.window.origin), + markExternalLinks(tree), ), [PagePrefix.DOCS]: getNavigationItemsTree(SUB_NAVIGATION_DATA.docs, (tree) => - markExternalLinks(tree, this.window.origin), + markExternalLinks(tree), ), };