Member-only story
NestJS — Generating a dynamic sitemap
Having a sitemap is critical for your SEO. If your website is a static website, you can just make your sitemap with text editor and serve it from your server file system. However, if your website is dynamic, such as forum, e-shop, etc, you cannot do that as the website content is unknown prior to the runtime. In order to deal with this issue, you need to generate your sitemap on demand. Here is a quick guide about how you can generate dynamic sitemap in NestJS, which will grow with your content.
Prerequisite
- There are many packages capable of generating a dynamic sitemap. My favorite one is the sitemap package as it is very light weighted and its coupling with other module is very loose. Thus, this guide is going to use this package.
- You are supposed to have a workable NestJS server before reading this guide.
- You are supposed to have knowledge about the sitemap format. This guide will not try to explain the sitemap XML attributes.
First things first, install the package:
npm install sitemap@5
Setting up GET request for your sitemap
The logic is very simple. What you need to do is just setting up an async GET request, which will trigger the generation of sitemap and…