Skip to content

Commit

Permalink
chore(app): navigation links do not work when using baseHref
Browse files Browse the repository at this point in the history
Closes #34
  • Loading branch information
skoropadas committed Mar 21, 2023
1 parent 7bcb73b commit 91c802d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libs/app/components/root/root.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BreakpointObserver} from '@angular/cdk/layout';
import {Location} from '@angular/common';
import {DOCUMENT, Location} from '@angular/common';
import {ChangeDetectionStrategy, Component, Directive, ElementRef, Inject, NgZone} from '@angular/core';
import {Router} from '@angular/router';
import {isExternalLink} from '@ng-doc/app/helpers/is-external-link';
Expand Down Expand Up @@ -58,6 +58,8 @@ export class NgDocCustomSidebarDirective {}
@UntilDestroy()
export class NgDocRootComponent {
constructor(
@Inject(DOCUMENT)
private readonly document: Document,
private readonly elementRef: ElementRef<HTMLElement>,
private readonly ngZone: NgZone,
private readonly router: Router,
Expand All @@ -82,6 +84,9 @@ export class NgDocRootComponent {
}

if (target instanceof HTMLAnchorElement) {
const base: HTMLBaseElement | null = this.document.querySelector('base');
const baseHref: string = base?.getAttribute('href') ?? '/';

if (isExternalLink(target.href)) {
event.preventDefault();

Expand All @@ -94,7 +99,7 @@ export class NgDocRootComponent {
const {pathname, search} = target;
const isInPageAnchor: boolean = target.getAttribute('href')?.startsWith('#') ?? false;
const correctPathname: string = isInPageAnchor ? this.location.path() : pathname;
const relativeUrl: string = correctPathname + search;
const relativeUrl: string = (correctPathname + search).replace(baseHref, '');
const hash: string = target.hash;

event.preventDefault();
Expand Down

0 comments on commit 91c802d

Please sign in to comment.