From 5097f7c254ba9ec3db4d9803885e9afb08540d05 Mon Sep 17 00:00:00 2001 From: ItzMineyy Date: Tue, 24 Dec 2024 23:05:50 +0100 Subject: [PATCH] Revised Meta Data & improved Routing --- src/app/about/about.component.ts | 7 +-- src/app/app.component.ts | 51 ++++++++++++++++++-- src/app/app.routes.ts | 60 +++++++++++++++++++++--- src/app/blog/blog.component.ts | 7 +-- src/app/not-found/not-found.component.ts | 5 -- src/app/portfolio/portfolio.component.ts | 6 +-- 6 files changed, 103 insertions(+), 33 deletions(-) diff --git a/src/app/about/about.component.ts b/src/app/about/about.component.ts index 0abd801..52185d2 100644 --- a/src/app/about/about.component.ts +++ b/src/app/about/about.component.ts @@ -1,5 +1,4 @@ import {Component, OnInit, OnDestroy, AfterViewInit} from '@angular/core'; -import {Meta, Title} from '@angular/platform-browser'; import { DeviceDetectorService } from 'ngx-device-detector'; import {NgIf} from '@angular/common'; @@ -12,17 +11,13 @@ import {NgIf} from '@angular/common'; styleUrls: ['./about.component.css'] }) export class AboutComponent implements OnInit, OnDestroy, AfterViewInit { - constructor(private titleService: Title, private meta: Meta, private deviceService: DeviceDetectorService) { + constructor( private deviceService: DeviceDetectorService) { this.isMobile = this.deviceService.isMobile(); } isMobile: boolean; private vantaEffect: any; ngOnInit() { - this.titleService.setTitle('itzMiney\'s Home'); - this.meta.updateTag({ name: 'description', content: 'Welcome to itzMiney\'s Homepage!\bHere you can find my portfolio and other cool stuff.' }); - this.meta.updateTag({ property: 'og:image', content: 'https://itzminey.dev/assets/ogimg.png' }); - window.addEventListener('resize', this.onResize.bind(this)); // Add resize listener for responsiveness const bgElement = document.getElementById('vanta-about-bg'); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index acb8e2d..dc2bd86 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,10 +1,12 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import {NgIf} from '@angular/common'; import { DeviceDetectorService } from 'ngx-device-detector'; -import {RouterOutlet, provideRouter, Router} from '@angular/router'; +import {RouterOutlet, ActivatedRoute, Router, NavigationEnd} from '@angular/router'; import {NavbarComponent} from './navbar/navbar.component'; import {MobileNavbarComponent} from './mobile-navbar/mobile-navbar.component'; import {FooterComponent} from './footer/footer.component'; +import { Title, Meta } from '@angular/platform-browser'; +import { filter, map, mergeMap } from 'rxjs/operators'; @Component({ selector: 'app-root', @@ -12,11 +14,50 @@ import {FooterComponent} from './footer/footer.component'; templateUrl: './app.component.html', styleUrl: './app.component.css' }) -export class AppComponent { - title = 'itzmineydev'; +export class AppComponent implements OnInit { isMobile: boolean; - constructor(private deviceService: DeviceDetectorService) { + constructor( + private deviceService: DeviceDetectorService, + private router: Router, + private activatedRoute: ActivatedRoute, + private titleService: Title, + private metaService: Meta + ) { this.isMobile = this.deviceService.isMobile(); } + + ngOnInit() { + this.router.events + .pipe( + filter(event => event instanceof NavigationEnd), + map(() => this.activatedRoute), + map(route => { + while (route.firstChild) route = route.firstChild; + return route; + }), + mergeMap(route => route.data) + ) + .subscribe(data => { + const meta = data['meta'] || {}; + this.updateMetaTags(meta); + }); + } + + updateMetaTags(meta: { title?: string; description?: string; image?: string; url?: string }) { + if (meta.title) { + this.titleService.setTitle(meta.title); + this.metaService.updateTag({ name: 'og:title', content: meta.title }); + } + if (meta.description) { + this.metaService.updateTag({ name: 'description', content: meta.description }); + this.metaService.updateTag({ name: 'og:description', content: meta.description }); + } + if (meta.image) { + this.metaService.updateTag({ name: 'og:image', content: meta.image }); + } + if (meta.url) { + this.metaService.updateTag({ name: 'og:url', content: meta.url }); + } + } } diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index be6e994..38ad645 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -5,10 +5,58 @@ import { BlogComponent } from './blog/blog.component'; import {NotFoundComponent} from './not-found/not-found.component'; export const routes: Routes = [ - { path: '', redirectTo: '/about', pathMatch: 'full' }, // Redirect default route to 'about' - { path: 'about', component: AboutComponent }, - { path: 'portfolio', component: PortfolioComponent }, - { path: 'blog', component: BlogComponent }, - - { path: '**', pathMatch: 'full', component: NotFoundComponent }, + { + path: '', + redirectTo: '/about', + pathMatch: 'full' + }, // Redirect default route to 'about' + { + path: 'about', + component: AboutComponent, + data: { + meta: { + title: 'itzMiney\'s Home', + description: 'Welcome to itzMiney\'s Homepage!\bHere you can find my portfolio and other cool stuff.', + image: 'https://itzminey.dev/assets/ogimg.png', + url: 'https://itzminey.dev/about' + } + } + }, + { + path: 'portfolio', + component: PortfolioComponent, + data: { + meta: { + title: 'Portfolio | itzMiney', + description: 'This is my portfolio where you can find examples of my designs and other projects!', + image: 'https://itzminey.dev/assets/ogimg.png', + url: 'https://itzminey.dev/portfolio' + } + } + }, + { + path: 'blog', + component: BlogComponent, + data: { + meta: { + title: 'Blog | itzMiney', + description: 'Welcome to my blog!\bI\'ll post occasional updates here on the stuff I\'m currently up to.', + image: 'https://itzminey.dev/assets/ogimg.png', + url: 'https://itzminey.dev/blog' + } + } + }, + { + path: '**', + pathMatch: 'full', + component: NotFoundComponent, + data: { + meta: { + title: '404 | Page Not Found', + description: 'How the hell did you end up here?', + image: 'https://itzminey.dev/assets/ogimg.png', + url: 'https://itzminey.dev/404' + } + } + } ]; diff --git a/src/app/blog/blog.component.ts b/src/app/blog/blog.component.ts index 660a676..b66d4ba 100644 --- a/src/app/blog/blog.component.ts +++ b/src/app/blog/blog.component.ts @@ -1,5 +1,4 @@ import {Component, OnInit, OnDestroy, AfterViewInit } from '@angular/core'; -import {Meta, Title} from '@angular/platform-browser'; import { DeviceDetectorService } from 'ngx-device-detector'; import {NgIf} from '@angular/common'; @@ -12,17 +11,13 @@ import {NgIf} from '@angular/common'; styleUrl: './blog.component.css' }) export class BlogComponent implements OnInit, OnDestroy, AfterViewInit { - constructor(private titleService: Title, private meta: Meta, private deviceService: DeviceDetectorService) { + constructor(private deviceService: DeviceDetectorService) { this.isMobile = this.deviceService.isMobile(); } isMobile: boolean; private vantaEffect: any; ngOnInit() { - this.titleService.setTitle('Blog | itzMiney'); - this.meta.updateTag({ name: 'description', content: 'Welcome to my blog!\bI\'ll post occasional updates here on the stuff I\'m currently up to.'}); - this.meta.updateTag({ property: 'og:image', content: 'https://itzminey.dev/assets/ogimg.png' }); - window.addEventListener('resize', this.onResize.bind(this)); // Add resize listener for responsiveness const bgElement = document.getElementById('vanta-blog-bg'); diff --git a/src/app/not-found/not-found.component.ts b/src/app/not-found/not-found.component.ts index ef93281..f55a298 100644 --- a/src/app/not-found/not-found.component.ts +++ b/src/app/not-found/not-found.component.ts @@ -1,5 +1,4 @@ import {Component, OnInit, OnDestroy, AfterViewInit } from '@angular/core'; -import {Meta, Title} from '@angular/platform-browser'; @Component({ selector: 'app-not-found', @@ -9,13 +8,9 @@ import {Meta, Title} from '@angular/platform-browser'; }) export class NotFoundComponent implements OnInit, OnDestroy, AfterViewInit { - constructor(private titleService: Title, private meta: Meta) {} private vantaEffect: any; ngOnInit() { - this.titleService.setTitle('404 Not Found'); - this.meta.updateTag({ name: 'description', content: 'How the hell did you end up here?' }); - this.meta.updateTag({ property: 'og:image', content: 'https://itzminey.dev/assets/ogimg.png' }); window.addEventListener('resize', this.onResize.bind(this)); // Add resize listener for responsiveness diff --git a/src/app/portfolio/portfolio.component.ts b/src/app/portfolio/portfolio.component.ts index e9e8cea..b774b6e 100644 --- a/src/app/portfolio/portfolio.component.ts +++ b/src/app/portfolio/portfolio.component.ts @@ -1,5 +1,4 @@ import {AfterViewInit, Component, OnDestroy, OnInit} from '@angular/core'; -import {Meta, Title } from '@angular/platform-browser'; import { DeviceDetectorService } from 'ngx-device-detector'; import {NgIf} from '@angular/common'; @@ -12,16 +11,13 @@ import {NgIf} from '@angular/common'; styleUrl: './portfolio.component.css' }) export class PortfolioComponent implements OnInit, OnDestroy, AfterViewInit { - constructor(private titleService: Title, private meta: Meta, private deviceService: DeviceDetectorService) { + constructor(private deviceService: DeviceDetectorService) { this.isMobile = this.deviceService.isMobile(); } isMobile: boolean; private vantaEffect: any; ngOnInit() { - this.titleService.setTitle('Portfolio | itzMiney'); - this.meta.updateTag({ name: 'description', content: 'This is my portfolio where you can find examples of my designs and other projects!' }); - this.meta.updateTag({ property: 'og:image', content: 'https://itzminey.dev/assets/ogimg.png' }); window.addEventListener('resize', this.onResize.bind(this)); // Add resize listener for responsiveness const bgElement = document.getElementById('vanta-portfolio-bg');