Next.js Discord

Discord Forum

How to route sitemaps in multi-tenant application

Answered
Western thatching ant posted this in #help-forum
Open in Discord
Western thatching antOP
Hello, I have a multi-tenant application The routes are setup like app/(example1)/example/

and app/(example2)/[dynamicexample]/

example 2 renders 50 domains dynamically previously the sitemap that worked lived at app/sitemap.xml/route.ts

but now that i introduced a new domain group (example1) I've had a horrible time figuring out where to put the sitemaps and to get them to work on both local and prod. I've also tried using middleware to rewrite the path but unsuccesfully, can anyone help?
In dev, i was able to do app/(example1)/sitemap.ts and that worked by going to localhost:3000/sitemap.xml

and then i had app/(example2)/(sitemapgroup)/sitemap.xml/route.ts and that actually worked locally by doing like localhost:3000/example2domain.com/sitemap.xml

but when i pushed to prod i kept getting a prerender error for the example 1. Ended up deleting it and now when i test out example2.com/sitemap.xml it's giving a 500 error.
Answered by Western thatching ant
Came up with a solution if anyone has this problem in the future :

create 1 sitemap at at
app/sitemap.ts


within there do this:
  export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
    const headersList = headers();

  const domain = headersList.get("host");


  if (domain === "www.example1.com") {
--- Generate sitemap here, view nextjs docs for example---
} else {
--- Generate sitemap for other domain(s) here ---
}
}
View full answer

1 Reply

Western thatching antOP
Came up with a solution if anyone has this problem in the future :

create 1 sitemap at at
app/sitemap.ts


within there do this:
  export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
    const headersList = headers();

  const domain = headersList.get("host");


  if (domain === "www.example1.com") {
--- Generate sitemap here, view nextjs docs for example---
} else {
--- Generate sitemap for other domain(s) here ---
}
}
Answer