Skip to content

Commit

Permalink
Merge pull request #2059 from ORCID/SetCanonicalUrlToPublicPage
Browse files Browse the repository at this point in the history
Set canonical url to public page
  • Loading branch information
leomendoza123 authored Sep 4, 2023
2 parents db8ceb1 + f98c151 commit 8b4853b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/app/core/canonocal-url/canonocal-url.service.spec.ts
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()
})
})
54 changes: 54 additions & 0 deletions src/app/core/canonocal-url/canonocal-url.service.ts
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)
}
})
}
}
13 changes: 11 additions & 2 deletions src/app/record/pages/my-orcid/my-orcid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
OnDestroy,
OnInit,
} from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router'
import { PlatformInfo, PlatformInfoService } from 'src/app/cdk/platform-info'
import { ORCID_REGEXP } from 'src/app/constants'
import { first, switchMap, take, takeUntil, tap } from 'rxjs/operators'
Expand All @@ -21,6 +21,10 @@ import { WINDOW } from 'src/app/cdk/window'
import { TogglzService } from 'src/app/core/togglz/togglz.service'
import { HelpHeroService } from 'src/app/core/help-hero/help-hero.service'
import { ScriptService } from '../../../core/crazy-egg/script.service'
import { DOCUMENT } from '@angular/common'
import { environment } from 'src/environments/environment'
import { filter, map } from 'rxjs/operators'
import { CanonocalUrlService } from 'src/app/core/canonocal-url/canonocal-url.service'

@Component({
selector: 'app-my-orcid',
Expand Down Expand Up @@ -80,7 +84,7 @@ export class MyOrcidComponent implements OnInit, OnDestroy {
@Inject(WINDOW) private window: Window,
private _togglz: TogglzService,
private _scriptService: ScriptService,
private _changeDetectorRef: ChangeDetectorRef
private _canonocalUrlService: CanonocalUrlService
) {}

private checkIfThisIsAPublicOrcid() {
Expand All @@ -106,6 +110,11 @@ export class MyOrcidComponent implements OnInit, OnDestroy {
ngOnInit(): void {
this.checkIfThisIsAPublicOrcid()
this.affiliations = 0

if (this.publicOrcid) {
this._canonocalUrlService.setCanonicalUrl(this.publicOrcid)
}

// Remove fragment temporally, to adding back when items have loaded
this.route.fragment.pipe(take(1)).subscribe((fragment) => {
if (fragment) {
Expand Down

0 comments on commit 8b4853b

Please sign in to comment.