loading...

Sitemap for dynamic routes in NuxtJS

andynoir profile image Andrey Gaisinskii ・6 min read

Motivation

As for the date of writing this post it is impossible to dynamically generate sitemap.xml in NuxtJS for dynamic routes using @nuxtjs/sitemap.

As the documentation for the module says, you have manually type all your dynamic routes into the routes array of sitemap object in nuxt.config.js.

// nuxt.config.js
{
  sitemap: {
    hostname: 'https://example.com',
    gzip: true,
    exclude: [
      '/secret',
      '/admin/**'
    ],
    routes: [
      'dynamic-route-one',
      'dynamic-route-two',
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

There is also another way, where you have to create a script that will make all the API calls to your backend server then will generate an array of routes that the sitemap object in the nuxt.config.js will then consume. More on that you can search for yourself or take a look at this article.

But I was very curious if there is another way, because it can be a pain the in the ass to type in all the dynamic routes manually or to make a lot of the API calls.

Disclaimer

After reading this post you might think that this way of generating
sitemap for dynamic routes is a bit hacky. And you are absolutely true. There is only one official way of doing it. Use at your own risk.

Watered down theory

Since Nuxt v2.13 a new option in nuxt.config.js is available:

// nuxt.config.js
export default {
  generate: {
    crawler: false // default - true
  }
}
Enter fullscreen mode Exit fullscreen mode

A quote from the documentation:

As of Nuxt >= v2.13 Nuxt.js comes with a crawler installed that will crawl your relative links and generate your dynamic links based on these links. If you want to disable this feature you can set the value to false.

I have created a repository which has both dynamic and static routes and if we run npm run generate in the terminal we can see our dynamic routes being generated as individual html files:

Alt Text

But also you can see that the path to the dynamic route being printed out into the terminal, eg. - Generated route "/users/10". So if it is printed out, maybe it is stored somewhere and we can obtain it or we can capture it while it is being printed out and we can store it.

Coding part

Lets quickly install @nuxtjs/sitemap module by typing in the terminal:

npm install @nuxtjs/sitemap
or
yarn add @nuxtjs/sitemap

then add it to nuxt.config.js like this:

// nuxt.config.js
modules: [
    // your other modules
    '@nuxtjs/sitemap'
  ],
Enter fullscreen mode Exit fullscreen mode

and configure it like this:

// nuxt.config.js
sitemap: {
    hostname: 'https://my-host.com',
    gzip: true,
    exclude: [
      '/exclude-one',
      '/exclude-two'
    ],
    defaults: {
      changefreq: 'daily',
      priority: 1,
      lastmod: new Date()
    }
  },
Enter fullscreen mode Exit fullscreen mode

By running npm run generate again the in terminal we can ensure that dynamic routes are being generated but not being added to the sitemap.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <url>
      <loc>https://my-host.com/albums</loc>
      <lastmod>2020-10-13T11:19:36.882Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/todos</loc>
      <lastmod>2020-10-13T11:19:36.882Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users</loc>
      <lastmod>2020-10-13T11:19:36.882Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/</loc>
      <lastmod>2020-10-13T11:19:36.882Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
</urlset>
Enter fullscreen mode Exit fullscreen mode

In order to obtain those generated routes paths we will use NuxtJS hooks which can be used in the NuxtJS modules.

To create a module just make a modules folder in your project directory with a file inside called generator.ts

Note: I am using TypeScript here, if you are using vanilla JavaScript it should be generator.js

Register your generator.ts in the nuxt.config.js

// nuxt.config.js
 buildModules: [
    // other build modules
    '@/modules/generator'
  ],
Enter fullscreen mode Exit fullscreen mode

Inside the generator.ts paste the following code and let's reflect on it a little bit.

import { Module } from '@nuxt/types'

const generator: Module = function () {
  this.nuxt.hook('generate:done', async (context: any) => {
    const routesToExclude: string[] =
    process.env.NUXT_ENV_EXCLUDE_ROUTES
      ? process.env.NUXT_ENV_EXCLUDE_ROUTES.split(',') : []
    const allRoutes: string[] = await Array.from(context.generatedRoutes)
    const routes: string[] = await allRoutes.filter((route: string) => !routesToExclude.includes(route))
    this.nuxt.options.sitemap.routes = await [...routes]
  })
}

export default generator
Enter fullscreen mode Exit fullscreen mode

1) We have defined a generator function that is exported and will be injected into NuxtJS.
2) We have subscribed to a generate:done hook and upon hook completion the code inside the function will be executed.
3) If you take a look here you will see that a certain context will be returned by the hook. If you console.log that context inside our module, you will see a generatedRoutes Set
4) Inside routesToExclude I use a ternary operator to ensure that I do have some data in my NUXT_ENV_EXCLUDE_ROUTES environment variable:

// .env
NUXT_ENV_EXCLUDE_ROUTES = '/exclude-one,/exclude-two'
Enter fullscreen mode Exit fullscreen mode

Then I divide my string into substrings to become an array of strings by using .split method.
5) Inside allRoutes I transform Set into an Array by using Array.from method.
6) I filter out all the routes I want to exclude by using filter method in routes
7) And lastly i spread my filtered routes into the routes property of the sitemap object: this.nuxt.options.sitemap.routes = await [...routes]

Now if you run npm run generate again you will see dynamic routes in the sitemap.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <url>
      <loc>https://my-host.com/albums</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/todos</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/1</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/2</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/3</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/4</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/5</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/6</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/7</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/8</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/9</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>https://my-host.com/users/10</loc>
      <lastmod>2020-10-13T12:09:44.775Z</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
   </url>
</urlset>
Enter fullscreen mode Exit fullscreen mode

For those who are not familiar with TypeScript

Leave a comment below or DM me, i will try to help you out.

Links

Live preview can be found here - https://andynoir.github.io/article-nuxt-dynamic-sitemap/
Sitemap here https://andynoir.github.io/article-nuxt-dynamic-sitemap/sitemap.xml
GitHub repo here - https://github.com/andynoir/article-nuxt-dynamic-sitemap

Discussion

pic
Editor guide
Collapse
djmenkveld profile image
Dirk Menkveld

Great article, thanks. Question, is there way to set the hostname in nuxt.config.js dynamically running with server.js? Example, this would be helpful in case of multiple domains my-host.com and my-host-b.com are pointing to the same nuxt app.

Collapse
andynoir profile image
Andrey Gaisinskii Author

Hi!

Not quite sure what do you mean, but you can access nuxt config inside your module by this.nuxt.options.sitemap.hostname. Also you can use different envs.

Collapse
djmenkveld profile image
Dirk Menkveld

Hey men, thanks for replying.

Well, the host needs to be dynamic, where I've different data connected to each host.

Example domains:
tourzomer.com
fantaciclo.it
grandtourfantasy.com

All domains are for different purposes and have dynamics path for teams and others. So, in the nuxt config or sitemap config I call call for example teams per domain from my backend. But, in order to do i need to know which domain it is. So, i set a client-host to my call, but for some reason i do not know which domain the app is.

Does this make sense?

Thread Thread
andynoir profile image
Andrey Gaisinskii Author

Still don't get it:)

But maybe you can add a custom header to your API calls. And each of your websites will have a custom header, for e.g. x-platform: tourzomer, x-platform: fantaciclo and so on.

Thread Thread
djmenkveld profile image
Dirk Menkveld

Hi, yeah that's the solution, but the problem is that I can't access the hostname tourzomer or fantaciclo in nuxt config or the utils helper I have to get the routes.

Collapse
takburgerap profile image
takburger

I wondered why the generate was able to display everything I needed for my sitemap and the sitemap module couldn't. Seems counterintuitive.
That's a great tutorial.
Could you add the bit of typescript but in JS ? I'm too poorly skilled to unpack your TS and try to put it in my project. I also tried to add TS to my nuxt project but it throws an error and I'm not too kin to add 3 packages just for this.

Collapse
andynoir profile image
Andrey Gaisinskii Author

Regarding your question about sitemap module not generating dynamic routes: github.com/nuxt-community/sitemap-... i think that maintainers of the module filter out the dynamic routes.

And here is none-typescript version of the project - github.com/andynoir/artcile-nuxt-d...

Collapse
takburgerap profile image
takburger

Thank you so much

Collapse
bacodekiller profile image
Quang Hiep

can use that for website has pagination? tks

Collapse
andynoir profile image
Andrey Gaisinskii Author

Can't quite understand what do you mean. Sitemap is not related to pagination by any means.

Collapse
pastorsi profile image
Simon Measures

I'm trying to implement this but having a problem. When I run my Nuxt project in dev ($ npm run dev), I get:
Error: Cannot find module '@/modules/generator'
Do have any suggestions?

Collapse
andynoir profile image
Andrey Gaisinskii Author

Hi!

Which version of Nuxt are you using ? A quote from the docs:

If you are going to offer using buildModules please mention that this feature is only available since Nuxt v2.9. Older users should upgrade Nuxt or use the modules section.

Also check out for typos and file extensions.