All files / app/page-not-found page-not-found.component.ts

100% Statements 8/8
100% Branches 1/1
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                    1x                             1x   2x   2x             2x 2x                 2x      
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import type { OnDestroy, OnInit } from '@angular/core';
import { Meta } from '@angular/platform-browser';
import type { MetaDefinition } from '@angular/platform-browser';
import { RouterLink } from '@angular/router';
 
/**
 * Block robots from indexing the 404 page.
 */
export const ROBOTS_META: MetaDefinition = {
  content: 'noindex, nofollow',
  name: 'robots',
} as const;
 
/**
 * Wildcard catchall route component for paths that do not match configured routes.
 */
@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ NgOptimizedImage, RouterLink ],
  selector: 'app-page-not-found',
  styleUrl: './page-not-found.component.scss',
  templateUrl: './page-not-found.component.html',
})
export class PageNotFoundComponent implements OnDestroy, OnInit {
  /** Angular service to manage Meta tags. https://angular.dev/api/platform-browser/Meta */
  private readonly _metaService: Meta = inject(Meta);
  /** Reference to the meta tag created OnInit to be removed OnDestroy. */
  private _robotsTag: HTMLMetaElement | null = null; // eslint-disable-line unicorn/no-null -- DOM uses null
 
  /**
   * Remove the robot blocking meta tag from the HTML Head when this Component is no longer the
   * routed Component.
   */
  public ngOnDestroy(): void {
    if (this._robotsTag) {
      this._metaService.removeTagElement(this._robotsTag);
    }
  }
 
  /**
   * Prevent robots from indexing this soft 404 page. Note that this probably is only necessary
   * with SSR or JavaScript executing web crawlers.
   */
  public ngOnInit(): void {
    this._robotsTag = this._metaService.addTag(ROBOTS_META);
  }
}