How to create sitemap for dynamic routes?
Answered
Satin Angora posted this in #help-forum
Satin AngoraOP
I have some routes like
[user]/[xyz]
that can have potentially large amounts of pages. How does one create a sitemap for this and stay within Google's 50MB limit?Answered by B33fb0n3
you can generate sitemaps like this ([read more](https://nextjs.org/docs/app/api-reference/functions/generate-sitemaps#example)):
Like that you stay within google limits and be able to place everything correctly in the sitemaps
import { BASE_URL } from '@/app/lib/constants'
export async function generateSitemaps() {
// Fetch the total number of products and calculate the number of sitemaps needed
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}
export default async function sitemap({
id,
}: {
id: number
}): Promise<MetadataRoute.Sitemap> {
// Google's limit is 50,000 URLs per sitemap
const start = id * 50000
const end = start + 50000
const products = await getProducts(
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
)
return products.map((product) => ({
url: `${BASE_URL}/product/${product.id}`,
lastModified: product.date,
}))
}
Like that you stay within google limits and be able to place everything correctly in the sitemaps
12 Replies
@Satin Angora I have some routes like `[user]/[xyz]` that can have potentially large amounts of pages. How does one create a sitemap for this and stay within Google's 50MB limit?
you can generate sitemaps like this ([read more](https://nextjs.org/docs/app/api-reference/functions/generate-sitemaps#example)):
Like that you stay within google limits and be able to place everything correctly in the sitemaps
import { BASE_URL } from '@/app/lib/constants'
export async function generateSitemaps() {
// Fetch the total number of products and calculate the number of sitemaps needed
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}
export default async function sitemap({
id,
}: {
id: number
}): Promise<MetadataRoute.Sitemap> {
// Google's limit is 50,000 URLs per sitemap
const start = id * 50000
const end = start + 50000
const products = await getProducts(
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
)
return products.map((product) => ({
url: `${BASE_URL}/product/${product.id}`,
lastModified: product.date,
}))
}
Like that you stay within google limits and be able to place everything correctly in the sitemaps
Answer
@B33fb0n3 you can generate sitemaps like this ([read more](https://nextjs.org/docs/app/api-reference/functions/generate-sitemaps#example)):
tsx
import { BASE_URL } from '@/app/lib/constants'
export async function generateSitemaps() {
// Fetch the total number of products and calculate the number of sitemaps needed
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}
export default async function sitemap({
id,
}: {
id: number
}): Promise<MetadataRoute.Sitemap> {
// Google's limit is 50,000 URLs per sitemap
const start = id * 50000
const end = start + 50000
const products = await getProducts(
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
)
return products.map((product) => ({
url: `${BASE_URL}/product/${product.id}`,
lastModified: product.date,
}))
}
Like that you stay within google limits and be able to place everything correctly in the sitemaps
Satin AngoraOP
By only fetching the 50,000 most recent, do I rely on Google's crawler having indexed (at an earlier time) the previous URLs that didn't make the cut?
@Satin Angora By only fetching the 50,000 most recent, do I rely on Google's crawler having indexed (at an earlier time) the previous URLs that didn't make the cut?
I wouldn't trust google and would build always all the sitemaps
@B33fb0n3 I wouldn't trust google and would build always all the sitemaps
Satin AngoraOP
How do I avoid hitting the limit (even if compressed)? It's a dynamic route and can have as many pages as I can store in the database table.
@Satin Angora How do I avoid hitting the limit (even if compressed)? It's a dynamic route and can have as many pages as I can store in the database table.
you can select from where to where you want to build your sitemap paths. Like that its easy for you, to build all your routes
@B33fb0n3 you can select from where to where you want to build your sitemap paths. Like that its easy for you, to build all your routes
Satin AngoraOP
According to https://developers.google.com/search/blog/2006/10/multiple-sitemaps-in-same-directory I can have a sitemap1, sitemap2,..., sitemapN, and just have them linked in the root site_map. Is my reasoning correct?
@Satin Angora According to https://developers.google.com/search/blog/2006/10/multiple-sitemaps-in-same-directory I can have a sitemap1, sitemap2,..., sitemapN, and just have them linked in the root site_map. Is my reasoning correct?
yea, generate the sitemaps like I mentioned and create a sitemap index to index the sitemaps
@Satin Angora solved?
Satin AngoraOP
Is there somewhere I need to mark as solved or accept an answer?
At the moment I don't think I'll fill up even a single sitemap but I'm thinking or worst case scenarios and trying to write code that I don't need to touch again
@Satin Angora Is there somewhere I need to mark as solved or accept an answer?
no worries, I marked the correct message. Happy to help
Spectacled bear
Hello