Next.js Discord

Discord Forum

Generating sitemap for a site with a blog

Answered
British Shorthair posted this in #help-forum
Open in Discord
British ShorthairOP
Hi there, I have a website with about 60-70 pages and looking to create a sitemap.ts which adds all the links. Any idea how to go about this?
Answered by British Shorthair
Thanks @Boston Terrier!

I didn't really want to create seperate sitemaps.

I just followed this and worked like a charm: https://github.com/vercel/platforms/blob/main/app/sitemap.ts

Appreciate your response nonetheless! 😄
View full answer

4 Replies

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,
  }))
}
British ShorthairOP
Thanks @Boston Terrier!

I didn't really want to create seperate sitemaps.

I just followed this and worked like a charm: https://github.com/vercel/platforms/blob/main/app/sitemap.ts

Appreciate your response nonetheless! 😄
Answer