#How to create sitemap for dynamic routes?
1 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
you can generate sitemaps like this (read more):
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
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
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
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
@azure coral solved?
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
This question has been marked as answered! If you have any other questions, feel free to create another post
[Click here](#1345705621097549905 message)
no worries, I marked the correct message. Happy to help
Hello
@red hamlet how does one create sitemaps for routes like /[user]/[project]? The Next.js example has product before a slug, whereas in my case I have /slug/slug
it's the same here: use the generateSitemaps function. Provide the ids there (you can also add mulitple values) and build from that your sitemaps
I saw you discussed about that in #off-topic . However, I didnt read through the messages there. You may be able to tell me a conclusion about the messages there?
It just seems like too much magic involved, making it difficult for a first timer to implement. But I'll give this function another try.
I've used generateSitemaps() in my root directory and found them available at http://localhost:3000/sitemap/1.xml. Doesn't this URL convention violate the rule laid out in https://www.sitemaps.org/protocol.html#location? These sitemaps (1.xml 2.xml...) can only point to URLs within the /sitemap route