Sitemaps - Is there something fundamental i'm misunderstanding about sitemaps?

Hello! I'm currently exploring how to best generate dynamic sitemaps for a Next.js-powered website. I stumbled upon the next-sitemap package (https://www.npmjs.com/package/next-sitemap), which seems like a robust solution for my needs. I can either use this package or handcraft the XML, but I'm leaning towards the former for convenience. Here's a snippet of what my sitemap generation looks like:
// pages/server-sitemap.xml/index.tsx
import { getServerSideSitemapLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

const fields = [
{
loc: 'https://example.com', // Absolute url
lastmod: new Date().toISOString(),
// changefreq
// priority
},
{
loc: 'https://example.com/dynamic-path-2', // Absolute url
lastmod: new Date().toISOString(),
// changefreq
// priority
},
]

return getServerSideSitemapLegacy(ctx, fields)
}

// Default export to prevent next.js errors
export default function Sitemap() {}
// pages/server-sitemap.xml/index.tsx
import { getServerSideSitemapLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

const fields = [
{
loc: 'https://example.com', // Absolute url
lastmod: new Date().toISOString(),
// changefreq
// priority
},
{
loc: 'https://example.com/dynamic-path-2', // Absolute url
lastmod: new Date().toISOString(),
// changefreq
// priority
},
]

return getServerSideSitemapLegacy(ctx, fields)
}

// Default export to prevent next.js errors
export default function Sitemap() {}
Could someone clarify when this code is actually executed? I know getServerSideProps runs server-side on each request, but does this mean that a user or a search engine bot needs to navigate to /server-sitemap-index.xml for the sitemap to be generated? What if that never happens or takes a long time? I am a bit confused on how this'll work in practice. Appreciate any insights!
0 Replies
No replies yetBe the first to reply to this messageJoin