Next.js Discord

Discord Forum

Understanding sitemap.js

Answered
hu2299 posted this in #help-forum
Open in Discord
1. If i have the following code within app/product/sitemap.js , does this mean every time someone loads this subdomain (www.domain.com/products) the script run? If so how can I prevent this, I just want to create this sitemap to update google crawlers once in a while. I don't want to make unsessery reqeusts to my db
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 }) {
  // 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/${id}`,
    lastModified: product.date,
  }))
}


2. My root sitemap and robots.txt look like this. Will the products sitemap be crawled automatically or do i need to specifically point to it?
export default function sitemap() {
    return [
      {
        url: 'https://domain.com/products',
        lastModified: new Date(),
        changeFrequency: 'monthly',
        priority: 1,
      },
...
]

User-Agent: *
Disallow: /admin
Disallow: /api/*


Sitemap: https://domain.com/sitemap.xml
Answered by Yi Lon Ma
something similar to this, the icon(that lamda and dot) will tell you
View full answer

7 Replies

to find out if it runs one every request or only once during build, check the build output
there will be indicators
something similar to this, the icon(that lamda and dot) will tell you
Answer
@Yi Lon Ma something similar to this, the icon(that lamda and dot) will tell you
Looks like this, it says this means SSG, so does that mean whenever I upload a new version of my site to something like vercel for example It only runs once on build?
● /editor/sitemap/[__metadata_id__]    0 B                0 B
├   ├ /editor/sitemap/0.xml
├   ├ /editor/sitemap/1.xml
├   ├ /editor/sitemap/2.xml
├   └ /editor/sitemap/3.xml
@Yi Lon Ma yes
Perfect, thank you. Do you also know about the second question?
I think it will be crawled automatically