mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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
This commit is contained in:
parent
a4b86b23cf
commit
66abff6db8
3 changed files with 12 additions and 12 deletions
|
|
@ -24,7 +24,6 @@ import {WINDOW} from '../../providers/index';
|
|||
export class ExternalLink implements OnInit {
|
||||
private readonly anchor: ElementRef<HTMLAnchorElement> = 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';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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[],
|
||||
|
|
|
|||
|
|
@ -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<string, NavigationItem[]> = {
|
||||
[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),
|
||||
),
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue