-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2059 from ORCID/SetCanonicalUrlToPublicPage
Set canonical url to public page
- Loading branch information
Showing
3 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing' | ||
|
||
import { CanonocalUrlService } from './canonocal-url.service' | ||
|
||
describe('CanonocalUrlService', () => { | ||
let service: CanonocalUrlService | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}) | ||
service = TestBed.inject(CanonocalUrlService) | ||
}) | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { DOCUMENT } from '@angular/common' | ||
import { Inject, Injectable } from '@angular/core' | ||
import { NavigationEnd, Router } from '@angular/router' | ||
import { filter, map } from 'rxjs/operators' | ||
import { ORCID_REGEXP } from 'src/app/constants' | ||
import { environment } from 'src/environments/environment' | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CanonocalUrlService { | ||
constructor(@Inject(DOCUMENT) private doc: any, private _router: Router) { | ||
this.init() | ||
} | ||
|
||
private init() { | ||
this._router.events | ||
.pipe( | ||
filter((event) => event instanceof NavigationEnd), | ||
map((event: NavigationEnd) => event as NavigationEnd) | ||
) | ||
.subscribe((event) => { | ||
if ( | ||
event?.url.startsWith('/my-orcid') || | ||
!ORCID_REGEXP.test(event?.url) | ||
) { | ||
// Remove canonical url if the route is not public page | ||
this.removeCanonicalUrl() | ||
} | ||
}) | ||
} | ||
|
||
setCanonicalUrl(publicOrcid) { | ||
// Just in case there is another canonical link already | ||
this.removeCanonicalUrl() | ||
let canonicalUrl = | ||
'https:' + | ||
environment.BASE_URL + | ||
(environment.BASE_URL.endsWith('/') ? publicOrcid : '/' + publicOrcid) | ||
let link: HTMLLinkElement = this.doc.createElement('link') | ||
|
||
link.setAttribute('rel', 'canonical') | ||
link.setAttribute('href', canonicalUrl) | ||
this.doc.head.appendChild(link) | ||
} | ||
removeCanonicalUrl() { | ||
this.doc.head.querySelectorAll('link').forEach((link) => { | ||
let attributeRel = link.getAttribute('rel') | ||
if (attributeRel && attributeRel == 'canonical') { | ||
link.parentNode.removeChild(link) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters