How to create a sitemap dynamically in Angular 12?

Member

by emelie , in category: SEO , a year ago

How to create a sitemap dynamically in Angular 12?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

2 answers

by jose_gulgowski , a year ago

@emelie 

To create a sitemap dynamically in Angular 12, you can use a library called ngx-sitemap-generator.


Here are the steps to use this library:

  1. Install the ngx-sitemap-generator library using the following command:npm install ngx-sitemap-generator
  2. Import the NgxSitemapGeneratorModule module in your app module:import { NgxSitemapGeneratorModule } from 'ngx-sitemap-generator'; @NgModule({ imports: [ NgxSitemapGeneratorModule.forRoot('https://example.com') ] }) export class AppModule { } The forRoot() method takes the base URL of your website.
  3. In your component, import the SitemapService and call its generateSitemap() method to generate the sitemap:import { Component } from '@angular/core'; import { SitemapService } from 'ngx-sitemap-generator'; @Component({ selector: 'app-root', template: '<h1>Hello, World!</h1>' }) export class AppComponent { constructor(private sitemapService: SitemapService) {} ngOnInit() { this.sitemapService.generateSitemap(); } } You can call the generateSitemap() method whenever you want to update the sitemap.
  4. Finally, you need to add the sitemap URL to your robots.txt file:Sitemap: https://example.com/sitemap.xml


That's it! Now, whenever you run your Angular app, the sitemap will be generated dynamically and the robots.txt file will include the sitemap URL.

Member

by delpha , 6 months ago

@emelie 

Please note that the ngx-sitemap-generator library is specifically designed for server-side rendering (SSR) in Angular applications. If you are not using SSR, this library may not be suitable for your needs.


For more advanced and dynamic sitemap generation in Angular, you can follow these steps:

  1. Install the xmlbuilder2 library, which helps in generating XML content: npm install xmlbuilder2
  2. Create a new service for sitemap generation. You can use the Angular CLI to generate a new service using the following command: ng generate service Sitemap
  3. In the sitemap.service.ts file, import the required dependencies and create a method to generate the sitemap:
 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
import { Injectable } from '@angular/core';
import { SitemapStream } from 'sitemap';
import { createWriteStream } from 'fs';
import { join } from 'path';
import { DOCUMENT, ɵgetDOM } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class SitemapService {
  constructor(@Inject(DOCUMENT) private document: any) {}

  generateSitemap(): void {
    const smStream = new SitemapStream({ hostname: 'https://example.com' });
    const path = join(this.document.location.origin, '/sitemap.xml');
    const writeStream = createWriteStream(path);

    smStream.pipe(writeStream);

    // Add URLs to your dynamic routes
    smStream.write({ url: '/' });
    smStream.write({ url: '/about' });
    // Add more URLs as required

    smStream.end();
    writeStream.on('finish', () => {
      console.log('Sitemap generated!');
    });
  }
}


This example shows how to generate a sitemap with two static routes (/ and /about). You can add more routes as required.

  1. In your component or in your root AppModule, import and inject the SitemapService. Call the generateSitemap method to generate the sitemap. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import { Component } from '@angular/core';
import { SitemapService } from './sitemap.service';

@Component({
  selector: 'app-root',
  template: '<h1>Hello, World!</h1>'
})
export class AppComponent {
  constructor(private sitemapService: SitemapService) {}

  ngOnInit() {
    this.sitemapService.generateSitemap();
  }
}


  1. Run your Angular application. The sitemap will be created and saved as sitemap.xml in the root directory of your application.


Remember to update your robots.txt file to include the URL of the dynamic sitemap:

1
Sitemap: https://example.com/sitemap.xml


By following these steps, you can generate a sitemap dynamically in Angular 12 without relying on external libraries.