Skip to content

Commit e6e49f7

Browse files
[DSC-1529] [CST-13917] add fallbacks, add og:type and twitter:card (summary) meta tags, add generic page tags
1 parent 645d655 commit e6e49f7

3 files changed

Lines changed: 122 additions & 25 deletions

File tree

src/app/core/metadata/metadata.service.ts

Lines changed: 114 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { SchemaJsonLDService } from './schema-json-ld/schema-json-ld.service';
4444
import { ITEM } from '../shared/item.resource-type';
4545
import { isPlatformServer } from '@angular/common';
4646
import { Root } from '../data/root.model';
47+
import { environment } from '../../../environments/environment';
4748

4849
/**
4950
* The base selector function to select the metaTag section in the store
@@ -82,6 +83,9 @@ export class MetadataService {
8283
'application/epub+zip', // .epub
8384
];
8485

86+
private fallbackImagePath = environment.metaTagFallbacksConfig.logo;
87+
private defaultPageDescription = environment.metaTagFallbacksConfig.description;
88+
8589
constructor(
8690
private router: Router,
8791
private translate: TranslateService,
@@ -152,6 +156,10 @@ export class MetadataService {
152156
}
153157
}
154158

159+
if (!hasValue(routeInfo.data.value.dso)) {
160+
this.setGenericPageMetaTags();
161+
}
162+
155163

156164
}
157165

@@ -163,6 +171,7 @@ export class MetadataService {
163171
}
164172

165173
private setDSOMetaTags(): void {
174+
const openGraphType = this.getOpenGraphType();
166175

167176
this.setTitleTag();
168177
this.setDescriptionTag();
@@ -172,9 +181,14 @@ export class MetadataService {
172181
this.setOpenGraphImageTag();
173182
this.setOpenGraphUrlTag();
174183

184+
if (openGraphType) {
185+
this.setOpenGraphTypeTag(openGraphType);
186+
}
187+
175188
this.setTwitterTitleTag();
176189
this.setTwitterDescriptionTag();
177190
this.setTwitterImageTag();
191+
this.setTwitterSummaryCardTag();
178192

179193
if (!this.isResearchOutput()) {
180194
return;
@@ -214,18 +228,18 @@ export class MetadataService {
214228
/**
215229
* Add <meta name="title" ... > to the <head>
216230
*/
217-
private setTitleTag(): void {
218-
const value = this.dsoNameService.getName(this.currentObject.getValue());
231+
private setTitleTag(title?: string): void {
232+
const value = title ?? this.dsoNameService.getName(this.currentObject.getValue());
219233
this.addMetaTag('title', value);
220234
this.title.setTitle(value);
221235
}
222236

223237
/**
224238
* Add <meta name="description" ... > to the <head>
225239
*/
226-
private setDescriptionTag(): void {
240+
private setDescriptionTag(description?: string): void {
227241
// TODO: truncate abstract
228-
const value = this.getMetaTagValue('dc.description.abstract');
242+
const value = description ?? this.getMetaTagValue('dc.description.abstract');
229243
this.addMetaTag('description', value);
230244
}
231245

@@ -409,20 +423,27 @@ export class MetadataService {
409423
this.setPrimaryBitstreamInBundleTag('citation_pdf_url');
410424
}
411425

426+
/**
427+
* Add <meta name="og:type" ... > to the <head>
428+
*/
429+
private setOpenGraphTypeTag(type: string): void {
430+
this.addMetaTag('og:type', type);
431+
}
432+
412433
/**
413434
* Add <meta name="og:title" ... > to the <head>
414435
*/
415-
private setOpenGraphTitleTag(): void {
416-
const value = this.getMetaTagValue('dc.title');
436+
private setOpenGraphTitleTag(title?: string): void {
437+
const value = title ?? this.getMetaTagValue('dc.title');
417438
this.addMetaTag('og:title', value);
418439
}
419440

420441
/**
421442
* Add <meta name="og:description" ... > to the <head>
422443
*/
423-
private setOpenGraphDescriptionTag(): void {
444+
private setOpenGraphDescriptionTag(description?: string): void {
424445
// TODO: truncate abstract
425-
const value = this.getMetaTagValue('dc.description.abstract') ?? this.translate.instant('meta.tag.missing.description');
446+
const value = description ?? this.getMetaTagValue('dc.description.abstract') ?? this.translate.instant('meta.tag.missing.description');
426447
this.addMetaTag('og:description', value);
427448
}
428449

@@ -436,26 +457,26 @@ export class MetadataService {
436457
/**
437458
* Add <meta name="og:url" ... > to the <head>
438459
*/
439-
private setOpenGraphUrlTag(): void {
440-
const value = this.getMetaTagValue('dc.identifier.uri');
460+
private setOpenGraphUrlTag(url?: string): void {
461+
const value = url ?? this.getMetaTagValue('dc.identifier.uri');
441462
this.addMetaTag('og:url', value);
442463
}
443464

444465

445466
/**
446467
* Add <meta name="twitter:title" ... > to the <head>
447468
*/
448-
private setTwitterTitleTag(): void {
449-
const value = this.getMetaTagValue('dc.title');
469+
private setTwitterTitleTag(title?: string): void {
470+
const value = title ?? this.getMetaTagValue('dc.title');
450471
this.addMetaTag('twitter:title', value);
451472
}
452473

453474
/**
454475
* Add <meta name="twitter:description" ... > to the <head>
455476
*/
456-
private setTwitterDescriptionTag(): void {
477+
private setTwitterDescriptionTag(description?: string): void {
457478
// TODO: truncate abstract
458-
const value = this.getMetaTagValue('dc.description.abstract');
479+
const value = description ?? this.getMetaTagValue('dc.description.abstract') ?? this.translate.instant('meta.tag.missing.description');
459480
this.addMetaTag('twitter:description', value);
460481
}
461482

@@ -466,6 +487,13 @@ export class MetadataService {
466487
this.setPrimaryBitstreamInBundleTag('twitter:image');
467488
}
468489

490+
/**
491+
* Add <meta name="twitter:card" ... > to the <head>
492+
*/
493+
private setTwitterSummaryCardTag(): void {
494+
this.addMetaTag('twitter:card', 'summary');
495+
}
496+
469497
/**
470498
* Get bitstream from item thumbnail link
471499
*
@@ -482,31 +510,35 @@ export class MetadataService {
482510
return null;
483511
}
484512
}),
485-
filter(data => !!data),
486-
getDownloadableBitstream(this.authorizationService),
513+
getDownloadableBitstream(this.authorizationService)
487514
);
488515
}
489516

490517
private setPrimaryBitstreamInBundleTag(tag: string): void {
491518
if (this.currentObject.value instanceof Item) {
492519
const item = this.currentObject.value as Item;
493520
this.getBitstreamFromThumbnail(item).pipe(
494-
switchMap((bitstream) => {
521+
map((bitstream) => {
495522
if (hasValue(bitstream)) {
496-
return [getBitstreamDownloadRoute(bitstream)];
523+
return getBitstreamDownloadRoute(bitstream);
497524
} else {
498525
return null;
499526
}
500527
}),
501-
filter(data => !!data),
502528
take(1)
503-
).subscribe((link: string) => {
504-
// Use the found link to set the <meta> tag
505-
this.addMetaTag(
506-
tag,
507-
new URLCombiner(this.hardRedirectService.getCurrentOrigin(), link).toString()
508-
);
529+
).subscribe((link) => {
530+
if (hasValue(link)) {
531+
// Use the found link to set the <meta> tag
532+
this.addMetaTag(
533+
tag,
534+
new URLCombiner(this.hardRedirectService.getCurrentOrigin(), link).toString()
535+
);
536+
} else {
537+
this.addFallbackImageToTag(tag);
538+
}
509539
});
540+
} else {
541+
this.addFallbackImageToTag(tag);
510542
}
511543
}
512544

@@ -670,5 +702,62 @@ export class MetadataService {
670702
});
671703
}
672704

705+
private addFallbackImageToTag(tag: string) {
706+
this.addMetaTag(
707+
tag,
708+
new URLCombiner(this.hardRedirectService.getCurrentOrigin(), this.fallbackImagePath).toString()
709+
);
710+
}
711+
712+
private getOpenGraphType(): string {
713+
let type = '';
714+
if (this.currentObject.value instanceof Item) {
715+
const item = this.currentObject.value as Item;
716+
switch (item.entityType) {
717+
case 'News':
718+
type = 'article';
719+
break;
720+
case 'Publication':
721+
type = 'article';
722+
break;
723+
case 'Book':
724+
type = 'book';
725+
break;
726+
case 'Person':
727+
type = 'profile';
728+
break;
729+
case 'Audio':
730+
type = 'music';
731+
break;
732+
case 'Video':
733+
type = 'video';
734+
break;
735+
default:
736+
break;
737+
}
738+
}
739+
return type;
740+
}
673741

742+
743+
private setGenericPageMetaTags() {
744+
const pageDocumentTitle = document.getElementsByTagName('title')[0].innerText;
745+
const pageUrl = new URLCombiner(this.hardRedirectService.getCurrentOrigin(), this.router.url).toString();
746+
const genericPageOpenGraphType = 'website';
747+
748+
this.setTitleTag(pageDocumentTitle);
749+
this.setDescriptionTag(this.defaultPageDescription);
750+
751+
this.setOpenGraphTitleTag(pageDocumentTitle);
752+
this.setOpenGraphDescriptionTag(this.defaultPageDescription);
753+
this.setOpenGraphUrlTag(pageUrl);
754+
this.setOpenGraphImageTag();
755+
this.setOpenGraphTypeTag(genericPageOpenGraphType);
756+
757+
758+
this.setTwitterTitleTag(pageDocumentTitle);
759+
this.setTwitterDescriptionTag(this.defaultPageDescription);
760+
this.setTwitterImageTag();
761+
this.setTwitterSummaryCardTag();
762+
}
674763
}
62.4 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Interface configuration for meta tag fallbacks information
3+
*/
4+
5+
export interface MetaTagFallbacksConfig {
6+
logo: string,
7+
description: string
8+
}

0 commit comments

Comments
 (0)