February 9, 2018

Angular CLI: Add Sitemap.xml and Robots.txt File

As a developer you may get the task adding a Sitemap and a robot.txt to your Angular project. This is quite easy if it is being built with Angular CLI.

Copy the files next to the favicon.ico  into the /src  folder of your Angular project.

 

Now open up the angular.json file in the projects root folder with an editor and add the two files inside the assets section, as follows:

If you have jq installed on your system you can achieve the same by executing the following one-liner without using an editor:

Restart your Angular build and the files should be served directly from the root path.

If you still use version 1.x of the  @angular/cli then change  .angular-cli.json instead of angular.json accordingly:

 

Another not very known feature of the CLI version 6.x is to relocate those assets. So alternatively you can leave your files beneath the /src/assets folder and relocate them using the following configuration:

But keep in mind that these two files may be duplicated using this setting. Both files will be then  available in the root and in the assets folder. If you would like to learn more about this topic, we recommend reading one of the original Angular CLI stories.

Dragan Zuvic
Developer at thecodecampus </>


4 responses to “Angular CLI: Add Sitemap.xml and Robots.txt File”

  1. Alik says:

    thank you

  2. Libellula says:

    If I want to read a file.txt in angular cli project. How can I do? I’ve tried with FileReader. But FileReader open any file from Local. It doesn’t open a file .txt stored in assets. And how can I bind in a template a file.txt stored in assets?

  3. Mikiyas Belachew says:

    How do you generate the sitemap.xml in the first place. I can not find it in the folder where the favicon.ico is located.

  4. Can Kattwinkel says:

    Hello Mikiyas,

    usually in an Angular CLI project the favicon is located under project-folder/src/favicon.ico.
    You have to create the file yourself.

Leave a Reply

Add code to your comment in Markdown syntax.
Like this:
`inline example`

```
code block
example
```

Your email address will not be published.