#How to generate a sitemap xml file dynamically?

1 messages · Page 1 of 1 (latest)

vivid tiger
reef vortex
#

According to this, I need to add something to the docker file. I am not sure what I should add.

#

I added this to the app directory.

#

the content of route.ts is:

export const dynamic = 'force-dynamic'

reef vortex
#

Can you help me with this? @vivid tiger

unique flameBOT
plucky plover
#

https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generating-a-sitemap-using-code-js-ts
it's not specific to Payload, you need to generate sitemap using this Next.js guide.
You can get the data for sitemap with REST / GraphQL or Local API if using 3.0
It could look like this

import type { MetadataRoute } from 'next'

const serverUrl = 'https://domain.com'

export default async function sitemap(): MetadataRoute.Sitemap {
  const { docs: pages } = await fetch(`${serverUrl}/api/pages?limit=0`).then((res) => res.json())

  const { docs: posts } = await fetch(`${serverUrl}/api/posts?limit=0`).then((res) =>
    res.json(),
  )

  const sitemap: MetadataRoute.Sitemap = []

  for (const page of pages) {
    sitemap.push({
      changeFrequency: 'monthly',
      lastModified: page.updatedAt,
      priority: 1,
      url: `${serverUrl}/${page.slug === 'home' ? '' : page.slug}`,
    })
  }

  for (const post of posts) {
    sitemap.push({
      changeFrequency: 'monthly',
      lastModified: post.updatedAt,
      priority: 1,
      url: `${serverUrl}/posts/${post.slug}`,
    })
  }

  return sitemap
}

API Reference for the sitemap.xml file.

reef vortex
#

I already referred to this docs but didnt help much, as it doesn't incorporate payload

plucky plover
reef vortex
#

Worked!

#

Thank you so much!

unique flameBOT