Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to generate a sitemap xml file dynamically?

discord user avatar
seanzubrickas
11 months ago
7

Hey

@615584606346805278

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
  • default discord avatar
    anonymous630211 months ago

    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?

    @364562745447940099
  • default discord avatar
    ritsu045511 months ago
    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
    }
  • discord user avatar
    seanzubrickas
    11 months ago

    thank you for jumping in here!

  • default discord avatar
    anonymous630211 months ago

    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

  • default discord avatar
    ritsu045511 months ago

    this is a screenshot from Next.js docs, so you should put this into

    app/sitemap.ts
  • default discord avatar
    anonymous630211 months ago

    Worked!



    Thank you so much!

  • default discord avatar
    lusinhoalagunhalast month

    Is there any plugin for handling it?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.