DEV Community

Sitemap for dynamic routes in NuxtJS

Andrey Gaisinskii on October 13, 2020

Motivation As for the date of writing this post it is impossible to dynamically generate sitemap.xml in NuxtJS for dynamic routes using ...
Collapse
 
djmenkveld profile image
Dirk Menkveld • Edited

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
 
gaisinskii profile image
Andrey Gaisinskii

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
 
gaisinskii profile image
Andrey Gaisinskii

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.

Thread Thread
 
mehattabi profile image
MeHattabi

Have you found a solution? I'm in the same situatien and going crazy here..

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
 
gaisinskii profile image
Andrey Gaisinskii

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
 
sushiljic profile image
Sushil G.C.

Hi Andrey

Great Post. Loved it.
I was wondering what could be setup for SSR type webapp.
For example,
I usually deploy nuxt SSR webapp using npm run build in production and I would be very helpful If can use above method.

I tried looking for hook with build:done something but couldnot get any in Link.
nuxtlink

Also Since you have generate all dynamic 10 pages of users but for realtime siite,
We will have dynamic new content that are being generated after generate nuxt command so what can be the best solution?

❤️ from Nepal

Collapse
 
gaisinskii profile image
Andrey Gaisinskii

Sorry for the late reply, but I think there is no best solution in this case.

In my case I am using SSG and I don't have such problem because my routes and sitemap files are FIXED in some way or another, if someone want to add a new page or other content the entire app has to be rebuild hence the sitemap will be rebuilt too.

In your case the content is always dynamic, but the sitemap will be generated only after deploy if I understand correctly. I think it is better to address this problem to the maintainer of the library, because I remember that he was going to add such feature.

Collapse
 
pedroslvieira profile image
Pedro Soares

Hey Sushil! Have you found a solution for this problem? I have to solve the same issue and would like to understand how to serve the sitemap in production when new content is added to the website.

cheers!

Collapse
 
scoch5 profile image
Daniele Scotti

This article is very useful, thank you very much! ✌️
I believe that the best way to exclude the routes is not to use the env variable, but read the routes to exclude in the configuration of the sitemap module from this.nuxt.options.sitemap.exclude

Collapse
 
luisjoserivera profile image
Luis Jose Rivera • Edited

Or simply avoid the whole module and TS thing by adding this to nuxt.config.js

hooks: {
generate: {
async done(context) {
const allRoutes = await Array.from(context.generatedRoutes);
context.options.sitemap.routes = await [...allRoutes];
},
},
},

Collapse
 
filippolcr profile image
Wonderman

Ehi Andrey, great point here. Thanks

Do you know if it's possible to get head meta tags after nuxt generate is done?

I want to exclude some routes from sitemap it a certain meta tag is properly generated.

Collapse
 
gaisinskii profile image
Andrey Gaisinskii

Sorry for the late reply, I'm not quite sure what do you mean. Do you want to prevent some routes to be included into sitemap if your meta tag meets some criteria ? If yes, unfortunately I don't have an answer for you, it would be just a brute force console logging for me to answer your question. Take a look here, it might help you.

Collapse
 
simonholdorf profile image
Simon Holdorf

This helped me a lot, thanks!

Collapse
 
bacodekiller profile image
Quang Hiep • Edited

can use that for website has pagination? tks

Collapse
 
gaisinskii profile image
Andrey Gaisinskii

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

Collapse
 
jakobbaranowski profile image
JakobBaranowski

Hi there,

I am having issues because i can see the dynamic routes being generated but it doesn't add them to the sitemap.xml. I am having the exact same setup as you have. Any ideas?

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
 
gaisinskii profile image
Andrey Gaisinskii

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.

Collapse
 
imlwi profile image
Louis Dickinson

Awesome Tutorial 👍👍👍👍👍👍

Collapse
 
meetguillaume profile image
Guillaume Ducuing

Thanks it works !