Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
#How to generate a sitemap xml file dynamically?
1 messages · Page 1 of 1 (latest)
Hey @reef vortex I suggest you take a look at this discussion and see if this helps in your case—
https://github.com/payloadcms/payload/discussions/4920#discussioncomment-9097440
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'
Can you help me with this? @vivid tiger
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
}
thank you for jumping in here!
Where should I put this piece of code? Obviously, I have to edit it based on my requirement, but, where should it reside in the project? And how can I access the sitemap once its been generated.
I already referred to this docs but didnt help much, as it doesn't incorporate payload
this is a screenshot from Next.js docs, so you should put this into app/sitemap.ts
Glad your issue was resolved! :tada: If you want to help make payload better, please give us a :star: on GitHub and review us - It helps us a lot.
🌟 Star Us on GitHub
👍 Review Us