0

Am trying to do Angular SSR with canonical URL. AM using the below code, Actually this is working fine with HTTP links. My issue is Canonical url is showing HTTP link and actual my Website is in HTTPS. How to fix this issue..???

let link: HTMLLinkElement = this.doc.createElement('link');
        link.setAttribute('rel', 'canonical');
        this.doc.head.appendChild(link);
        let url = this.doc.URL;
        link.setAttribute('href', url);
Abijith Ajayan
  • 238
  • 3
  • 17

1 Answers1

2

Works for me

constructor(@Inject(DOCUMENT) private document: any) {}

updateCanonical(url: string) {
    const canonicalUrl = this.siteUrl + '/' + url;
    const head = this.document.getElementsByTagName('head')[0];
    var element: HTMLLinkElement = this.document.querySelector(`link[rel='canonical']`) || null;
    if (element == null) {
      element = this.document.createElement('link') as HTMLLinkElement;
      head.appendChild(element);
    }
    element.setAttribute('rel', 'canonical');
    element.setAttribute('href', canonicalUrl);
  }