Nextjs Dynamic Sitemap and Sitemap Index

Hi, i´m looking for a way to do two things.

  1. Generate a Sitemap Index for my list of sitemaps.

  2. We are an ecommerce, and we have a use case that make us generate multiple sitemaps (by store and lang).

Nextjs give us the ability to create sitemaps in any part of the tree of urls. But it don’t give us any way of access the current route params of the url.

for example /[store]/[lang]/sitemap.xml don’t have any way to access the store and lang params.

Any help with this?

1 Like

Hey @oljimenez,

Before I go into potential solutions, could you please provide a bit more background on your current tech stack? I see that you’re using Next.js for the frontend, but it would be helpful to know which version of Next.js you are using, as well as whether you are utilizing App Routing or Pages Routing.

This information will help community members, including myself, better understand your problem so we can share relevant solutions.

Looking forward to your response!

Thanks,
Chintan

2 Likes

Hi.

We’re using Nextjs App Router 15.3.1 currently.

1 Like

Thanks for reply, @oljimenez

Please use next-sitemap for indexed sitemap configuration.

Package link: next-sitemap - npm

Note: Make sure all your requested pages are generated through generateStaticParams.

For example,

blog/[slug]/page.tsx

export async function generateStaticParams() {
  const result = await sanityFetch<BLOG_POSTResult[]>({
    query: BLOG_POSTS,
    perspective: "published",
    stega: false,
  });

  if (!result.data) {
    return [];
  }

  return result.data.map((post) => ({
    slug: post?.slug?.current,
  }));
}

next-sitemap.config.js

/** @type {import('next-sitemap').IConfig} */

module.exports = {
  siteUrl: process.env.NEXT_SITE_URL,
  generateRobotsTxt: true,
  generateIndexSitemap: false, // HERE YOU HAVE TO MARK AS TRUE, IF YOU NEED INDEXED SM
  exclude: [
    "/manifest.json",
    "/robots.txt",
    "/sitemap.xml",
    "/feed.xml",
    "/_next/static/**/*",
    "/static/**/*",
  ],
  robotsTxtOptions: {
    policies: [
      {
        userAgent: "*",
        disallow: ["/_next/static/**/*", "/static/**/*"],
        allow: "/",
      },
    ],
  },
};
2 Likes

Hi, this is not a solution at all. Even more, this is wrong. The next-sitemap package it´s deprecated and doesn´t work with Nextjs App Router.

Also it doesn´t fix the problem i have.

Hey @oljimenez,

I’ve been using the same package for the last two years with Next.js versions 13, 14, and now 15, and I haven’t encountered any issues. Could you please clarify what’s wrong? How did you determine that it’s deprecated? It’s still available on the npm store.

Please provide more specific details about your problem.

Thanks,
Chintan