0

I'm guessing some of you have some experience with generating Sitemap for your project. I'm having a Nuxt ( SSR full static ) project. I'm using @nuxtjs/prismic and @nuxtjs/sitemap module.

The sitemap module does not generate dynamic pages therefor we need to asynchronously retrieve all the pages we have on the website and process the data into a format required by the sitemap module.

I'm guessing that I need to write some build module where I would have access to prismic and then generate and write the desired data format.

Help me please to understand how to import or access prismic from module level so I can write the desired logic.

UPDATE I created build module and registered it in nuxt.config.js. Successfully retrieving data from prismic query. In the code bellow In my loop I transform prismic data in to data format that is required by the sitemap module.

I pass the data to sitemap module with this.nuxt. Right now this code passes the correct data however the module overrides the static pages and the only correct data left is for the dynamic pages. It looks that I need to disable the automatic generate by the module and only to work with my data.

// ... all dynamic pages generated correctly
<url>
<loc>https://danica-dev.netlify.app/blog/arrival-of-ikea-in-ukraine</loc>
<lastmod>2021-05-01T14:00:04.000Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>https://danica-dev.netlify.app/en</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/kontakt</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/blog</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/contact</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/projects</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/blog</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/kontakt</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/projects</loc>
</url>

// modules/sitemap.js
import prismic from '@prismicio/client'

const apiEndpoint = 'https://danica.cdn.prismic.io/api/v2'
const client = prismic.client(apiEndpoint)

export default async function Sitemap() {
    const data = await client.query('', { pageSize: 100 })
    const pages = []

    data.results.forEach((page) => {
        switch (page.type) {
            case 'blog_post':
                pages.push({
                    url: `blog/${page.uid}`,
                    changefreq: 'weekly',
                    priority: 0.25,
                    lastmod: page.last_publication_date,
                })
                break
            case 'project_post':
                pages.push({
                    url: `projects/${page.uid}`,
                    changefreq: 'monthly',
                    priority: 0.5,
                    lastmod: page.last_publication_date,
                })
                break
      //...
            default:
                break
        }
    })

    this.nuxt.options.sitemap.routes = pages
}
mart cube
  • 633
  • 1
  • 15
  • 30
  • What did you tried so far? We're here to help you with a technical issue, not for a tutorial as to say. – kissu May 29 '21 at 19:52
  • Hi @kissu, thanks for your time, I updated my post. I hope it helps to understand the situation better – mart cube May 29 '21 at 20:16
  • Should it be `sitemap` rather than `Sitemap`? Also, shouldn't you return an array at the end? Is it empty? – kissu May 29 '21 at 23:44
  • I loop trough the array of data.results where I transform prismic data in to data format required by the sitemap module. Then with this.nuxt I try to pass the array to the sitemap module – mart cube May 30 '21 at 11:08
  • Do you have what is expected in the final array? – kissu May 30 '21 at 12:38
  • yes i do, but it seems that I do not know to pass it properly to the sitemap module – mart cube May 30 '21 at 12:46

0 Answers0