Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set canonical url to public page #2059

Merged
merged 8 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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