38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { Injectable, inject } from '@angular/core';
|
|
import { Meta, Title } from '@angular/platform-browser';
|
|
|
|
interface SeoData {
|
|
title: string;
|
|
description: string;
|
|
url?: string;
|
|
image?: string;
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class SeoService {
|
|
private readonly meta = inject(Meta);
|
|
private readonly title = inject(Title);
|
|
|
|
update(data: SeoData): void {
|
|
const fullTitle = `${data.title} - Toju`;
|
|
|
|
this.title.setTitle(fullTitle);
|
|
|
|
this.meta.updateTag({ name: 'description', content: data.description });
|
|
this.meta.updateTag({ property: 'og:title', content: fullTitle });
|
|
this.meta.updateTag({ property: 'og:description', content: data.description });
|
|
this.meta.updateTag({ name: 'twitter:title', content: fullTitle });
|
|
this.meta.updateTag({ name: 'twitter:description', content: data.description });
|
|
|
|
if (data.url) {
|
|
this.meta.updateTag({ property: 'og:url', content: data.url });
|
|
this.meta.updateTag({ rel: 'canonical', href: data.url });
|
|
}
|
|
|
|
if (data.image) {
|
|
this.meta.updateTag({ property: 'og:image', content: data.image });
|
|
this.meta.updateTag({ name: 'twitter:image', content: data.image });
|
|
}
|
|
}
|
|
}
|