← Back to Angular, page 4

angular-0066 · Easy fresher

How do you set the page title and meta tags in Angular, and what does that mean for SEO and social sharing?

Set a title per route with the title property or a resolver, and use the Title and Meta services to update the title and meta tags from code. Crawlers and link previews mostly read the server HTML, so for public pages use server-side rendering or prerendering so these tags are present in the first response.

Full answer

Every page needs a title and a description. Browsers show the title in the tab and history, assistive technology announces it, and search engines use both in results.

Ways to set them:

  1. Route title: { path: 'courses', component: CoursesComponent, title: 'Courses' }. The router sets document.title on navigation. A function or resolver gives a dynamic title, such as the course name. A custom TitleStrategy can add a suffix for every page.
  2. Title service: inject(Title).setTitle('Angular basics').
  3. Meta service: meta.updateTag({ name: 'description', content: '...' }). Social sharing uses Open Graph tags such as og:title, og:description, and og:image, set with { property: 'og:title', content: '...' }.

The important point is when the tags exist. A purely client-rendered app sends an almost empty HTML file, and tags are set only after JavaScript runs. Search engines that run JavaScript will see them, but many link-preview bots (chat apps, social networks) read only the first HTML. So for public pages use server-side rendering or prerendering. Title and Meta work on the server too, because they use the platform's document.

Good practice:

  • A unique title and description for each page. Keep titles under about 60 characters and descriptions under about 160.
  • Use updateTag (replace) instead of addTag (can create duplicates) when navigating.
  • Set a canonical link for pages that appear at more than one URL.
  • Put structured data (JSON-LD) in the page for rich results if relevant.
  • Do not rely on the meta keywords tag. Search engines ignore it.
typescript
import { Component, Injectable, inject } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
import { RouterStateSnapshot, TitleStrategy, Routes } from '@angular/router';

@Injectable({ providedIn: 'root' })
export class SiteTitleStrategy extends TitleStrategy {
  private readonly title = inject(Title);

  override updateTitle(snapshot: RouterStateSnapshot): void {
    const pageTitle = this.buildTitle(snapshot);
    this.title.setTitle(pageTitle ? `${pageTitle} | Schoolabe` : 'Schoolabe');
  }
}

@Component({
  selector: 'app-course-page',
  template: `<h1>Angular basics</h1>`,
})
export class CoursePageComponent {
  private readonly meta = inject(Meta);

  constructor() {
    this.meta.updateTag({ name: 'description', content: 'Learn Angular step by step with short lessons.' });
    this.meta.updateTag({ property: 'og:title', content: 'Angular basics' });
    this.meta.updateTag({ property: 'og:description', content: 'Short lessons with practice questions.' });
  }
}

export const routes: Routes = [{ path: 'courses/angular', component: CoursePageComponent, title: 'Angular basics' }];

export const titleProvider = { provide: TitleStrategy, useClass: SiteTitleStrategy };

Angular 20.3, type-checked. The suffix strategy is registered with `{ provide: TitleStrategy, useClass: SiteTitleStrategy }`.

Key points

  • Route `title` is the simplest way. `Title` and `Meta` cover dynamic pages.
  • `meta.updateTag` avoids duplicates when navigating.
  • Link preview bots read the first HTML. Use SSR or prerendering for public pages.
  • Unique titles and descriptions per page.

Common mistakes

  • Using `addTag` on every navigation and filling the head with duplicates.
  • Relying on client-only rendering for pages that need link previews.
  • Leaving the default title for all routes.

Follow-ups

  • How would you add a suffix like "| Schoolabe" to every title?
  • How do you set a canonical URL?
  • How can you test that the tags appear in the server response?

Tips

  • Talk about when the tags exist (client versus server HTML). It is the senior part of this simple question.
  • Mention `TitleStrategy` for consistent titles.
  • Name length guidance for titles and descriptions, and say it is guidance, not a rule.

Evaluation rubric

  • Knows route titles and the Title and Meta services.
  • Explains the SSR relationship.
  • Avoids duplicate tag mistakes.