generateSitemaps root location
Unanswered
Black Caiman posted this in #help-forum
Black CaimanOP
im dynamically generating sitemaps for products on my website:
and if it put sitemap.ts in /item, then i properly get sitemaps generated at /item/sitemap/1.xml, /item/sitemap/2.xml, etc.. lets say i have 50k items, and im putting 1000 entries per page, so i have 50 pages. how do i reference the pages in the google console? would i have to submit every single sitemap page in the google console?
export async function generateSitemaps() {
const sitemapR = await getItemSitemap(1);
if (!sitemapR.ok) {
console.error(sitemapR.error);
return [];
}
const sitemapConfig = sitemapR.value.config;
return Array.from({ length: sitemapConfig.totalPages }, (_, pageZeroIndex) => ({
id: pageZeroIndex + 1,
}));
}
export default async function sitemap({
id: page,
}: {
id: number;
}): Promise<MetadataRoute.Sitemap> {
const sitemapR = await getItemSitemap(page);
if (!sitemapR.ok) {
console.error(sitemapR.error);
return [];
}
const sitemapResults = sitemapR.value.results;
return sitemapResults.map((result) => ({
...result,
changeFrequency: "daily",
priority: 0.7,
}));
}
and if it put sitemap.ts in /item, then i properly get sitemaps generated at /item/sitemap/1.xml, /item/sitemap/2.xml, etc.. lets say i have 50k items, and im putting 1000 entries per page, so i have 50 pages. how do i reference the pages in the google console? would i have to submit every single sitemap page in the google console?
12 Replies
Holland Lop
I don't know whether the approach I'm gonna say is the proper one. Even thought I would recommend you to try generate sitemap using a single API endpoint, which means a single sitemap in the root of the project rather than generating for individual pages.
Holland Lop
Sitemap contains just URLs, so for example:
If your item link is like: https://example.com/items/1
Where
If your item link is like: https://example.com/items/1
Where
1
is the :item_id
and you just create an endpoint to get all items ids from an API endpoint and use mapping to create sitemap.Black CaimanOP
@Holland Lop hmm theres this nextjs documentation https://nextjs.org/docs/app/api-reference/functions/generate-sitemaps. the thing is, if you have more than 50k url's per sitemap i dont think google allows it. so if i have a bunch of products i want to split it into separate sitemaps. but in the google search console you have to specify your sitemaps, and if i have like 50 pages of item sitemaps, id have to specify every single one.
for a little context, i have a background job updating my sitemap configuration in a redis cache every day, and the code i posted above is just hitting a backend api endpoint which is fetching all the sitemap urls from the redis cache (that way im not reading through my entire database on every single sitemap call).
but im mainly wondering if i would have to put every single sitemap into the google search console
for a little context, i have a background job updating my sitemap configuration in a redis cache every day, and the code i posted above is just hitting a backend api endpoint which is fetching all the sitemap urls from the redis cache (that way im not reading through my entire database on every single sitemap call).
but im mainly wondering if i would have to put every single sitemap into the google search console
Holland Lop
export default async function sitemap():
Promise<MetadataRoute.Sitemap> {
const response = await fetch("https://example.com/items");
const { items } = await response.json();
const items: MetadataRoute.Sitemap = items.map(({ id }) => ({
url: `${process.env.NEXT_PUBLIC_BASE_URL}/items/${id}`,
return [
...items,
];
}
You can nest sitemaps.
For example:
https://hackernoon.com/sitemap.xml
https://hackernoon.com/sitemap.xml
Black CaimanOP
ahhhhh, like i can reference another sitemap from my main sitemap?
Holland Lop
Exactly!
So you only have to give the root sitemap in Google search console.
Black CaimanOP
okay thats a huge help, thank you very much sir, i think i understand
Holland Lop
Glad to help! Thanks to you!