Nextjs with next-i18next: How to generate a sitemap for every domain?

669 views Asked by At

I am building a Next.js project that supports multiple domains through the next-i18next package (without using Sub-path Routing). Now, I need to create different sitemaps for the various domains that I support. I plan to statically generate these sitemaps and have them accessible as pages within their respective domains. Generating the sitemaps for the different domains won't be an issue. I can create a script and run this with:

"export": "next build && node ./src/scripts/generate-sitemap",

However, I'm uncertain about where to place these files and how to make them available across the different domains my site is running on. I would like to have them accessible on just example.com/sitemap.xml, example.de/sitemap.xml, etc. The sites will rely heavily on SEO, so it is crucial for me to get this right and do it the best way possible (SEO-wise).

For instance, when building the project, I see a folder .next is created. This folder contains a /server/pages directory. In there I can see the different locales I currently support. Wouldn't it be best to push the sitemap.xml files to these folders respectively?

1

There are 1 answers

3
Matt On

So the best way i found was to use rewrites with api routes.

This way you can control exactly which pages get shown in the sitemap ect.

I also used this for my robots.txt

async rewrites() {
return [
    {
        source: '/robots.txt',
        destination: '/api/robots'
    },
    {
      source: '/sitemap.xml',
      destination: '/api/sitemap'
    }
];

}